将文本文件中的数据格式化为CSV文件。

huangapple go评论140阅读模式
英文:

Format the data from Textfile to a CSV file

问题

我明白了你的问题。你想要修改你的VB脚本以确保"operatingSystem"、"operatingSystemVersion"、"whenCreated"和"whenChanged"数据被放在它们自己的列中,而不是混在"distinguishedName"列中。你可以尝试以下修改:

Option Explicit

Dim FSO, TextPath, CSVPath
Dim Textline, oText, oCSV
Dim CN, OU, i

Set FSO = CreateObject("Scripting.FileSystemObject")

TextPath = "fullq.txt"
Set oText = FSO.OpenTextFile(TextPath, 1)

CSVPath = "fullq.csv"
Set oCSV = FSO.CreateTextFile(CSVPath, 2, False)

oCSV.WriteLine("Name,DistinguishedName,OperatingSystem,OperatingSystemVersion,whenChanged,whenCreated")
oCSV.WriteLine

oText.SkipLine
Do Until oText.AtEndOfStream
    Textline = oText.ReadLine()

    OU = Split(TextLine, ",")
    CN = Mid(OU(0), 5)

    oCSV.Write CN & ","
    oCSV.Write OU(1) & "," ' DistinguishedName
    oCSV.Write OU(2) & "," ' OperatingSystem
    oCSV.Write OU(3) & "," ' OperatingSystemVersion
    oCSV.Write OU(4) & "," ' whenChanged
    oCSV.Write OU(5) ' whenCreated
    oCSV.WriteLine
Loop

oCSV.WriteLine
oCSV.Close

这个修改后的脚本应该能够将"operatingSystem"、"operatingSystemVersion"、"whenCreated"和"whenChanged"数据放在它们自己的列中,而不是混在"distinguishedName"列中。

英文:

I'm writing a script in VB Script to format Active directory data. I am using a Bat Script to produce the output in txt and I want to make a VB Script to format the data with headers.

My Batch script is

dsquery * -limit 0 -filter (objectclass=computer) -attr Name distinguishedName operatingSystem operatingSystemVersion whenChanged whenCreated > fullq.txt

The VB Script that I have is this currently

Option Explicit

Dim FSO, TextPath, CSVPath
Dim Textline, oText, oCSV
Dim CN, OU, i

Set FSO = CreateObject("Scripting.FileSystemObject")

TextPath = "fullq.txt"
Set oText = FSO.OpenTextFile(TextPath,1)

CSVPath = "fullq.csv"
Set oCSV = FSO.CreateTextFile(CSVPath, 2 ,False)

oCSV.WriteLine("Name,DistinguishedName,OperatingSystem,OperatingSystemVersion,whenChanged,whenCreated")
oCSV.WriteLine

oText.SkipLine
Do Until oText.AtEndOfStream
	Textline = oText.ReadLine()

	OU = Split(TextLine, ",")
	CN = Mid(OU(0), 5)

	oCSV.Write CN & ","
	For i = 1 To UBound(OU)
		oCSV.Write OU(i)
		If i < UBound(OU) Then
			oCSV.Write("/")
		End If
	Next
	oCSV.Writeline
Loop

oCSV.WriteLine
oCSV.Close

The problem i have is the operatingSystem, operatingSystemVersion, whenCreated and whenChanged data is in distinguishedName column. I want it to be in their own respective column. The code above is mainly for DistinguishedName. I added new attribute which are operatingSystem, operatingSystemVersion, whenChanged, whenCreated. For distinguishedName, i would like to replaces the comma with "/" whereas the rest of the attributes I would like it to be written in their own respective column. I cant use PowerShell as the company blocked it.

The Output in the CSV look like this:

Name,DistinguishedName,OperatingSystem,OperatingSystemVersion,whenChanged,WhenCreated

PC1,OU=1/OU=2/OU=3/DC=4/DC=COM    Window    123    12/12/12 10:10:10 12/12/12 10:10:10 , , ,
  • Note that the comma represent new column

As written above, i tried this VB Script but the operatingSystem, operatingSystemVersion, whenCreated and whenChanged data is in distinguishedName column. I want it to be in their own respective column.

This is how the the fullq.txt would look like:

Name        DistinguishedName              OperatingSystem        OperatingSystemVersion   whenChanged        WhenCreated
PC1         OU=1/OU=2/OU=3/DC=4/DC=COM     Window                    123 
      12/12/12 10:10:10     12/12/12 10:10:10 

答案1

得分: 1

给定提供的fullq.txt样本,可以将输入按" "(两个空格)分割,然后忽略空条目并修整结果:

Const Excel = True

Set oFSO = CreateObject("Scripting.FileSystemObject")

TextPath = "fullq.txt"
Set oText = oFSO.OpenTextFile(TextPath,1)

CSVPath = "fullq.csv"
Set oCSV = oFSO.CreateTextFile(CSVPath,2,True)

If Excel Then oCSV.WriteLine "SEP=,"
oCSV.WriteLine "Name,DistinguishedName,OperatingSystem,OperatingSystemVersion,whenChanged,whenCreated"

oText.SkipLine

Do Until oText.AtEndOfStream
    NewLine = ""
    Textline = oText.ReadLine
    aTextLine = Split(Textline,"  ")
    For i = 0 To UBound(aTextLine)
      If aTextLine(i)<>"" Then
        If NewLine<>"" Then NewLine = NewLine & ","
        NewLine = NewLine & Trim(aTextLine(i))
      End If
    Next
    oCSV.Writeline NewLine
Loop

oText.Close
oCSV.Close

请注意,示例输入不包含逗号,因此我删除了按逗号分割的代码。

英文:

Given the fullq.txt sample provided, the input can be split on " " (two spaces) and then ignore empty entries and trim results:

Const Excel = True

Set oFSO = CreateObject("Scripting.FileSystemObject")

TextPath = "fullq.txt"
Set oText = oFSO.OpenTextFile(TextPath,1)

CSVPath = "fullq.csv"
Set oCSV = oFSO.CreateTextFile(CSVPath,2,True)

If Excel Then oCSV.WriteLine "SEP=,"
oCSV.WriteLine "Name,DistinguishedName,OperatingSystem,OperatingSystemVersion,whenChanged,whenCreated"

oText.SkipLine

Do Until oText.AtEndOfStream
    NewLine = ""
    Textline = oText.ReadLine
    aTextLine = Split(Textline,"  ")
    For i = 0 To UBound(aTextLine)
      If aTextLine(i)<>"" Then
        If NewLine<>"" Then NewLine = NewLine & ","
        NewLine = NewLine & Trim(aTextLine(i))
      End If
    Next
    oCSV.Writeline NewLine
Loop

oText.Close
oCSV.Close

Note that the sample input contained no commas, so I removed the code that splits on commas.

huangapple
  • 本文由 发表于 2023年4月13日 15:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定