Afficher un message
Vieux 09/10/2007, 20h15   #2
dveit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Output PC Name, Serial #, IP, and , OS to CSV

For previous tasks like this I usually just build out the file contents
using a CSV format, create the text file with a .csv file extension, then
write the contents to the file. Sample:

Const ForWriting = 2

Dim strFileHeader, strFileContents, strSomeLineDataCSVFormatted
Dim strFilePath

strFilePath = "c:\testfile.csv"
strFileHeader = "MachineName,OS,SerialNumber"
strFileContents = strFileHeader & vbCrLf
strFileContents = strFileContents & strSomeDataCSVFormatted & vbCrLf
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileCreate = objFSO.CreateTextFile(strFilePath)
objFileCreate.Close
Set objFileWrite = objFSO.OpenTextFile(strFilePath, ForWriting)
objFileWrite.Write strFileContents
objFileWrite.Close



"Hickory" <Hickory@discussions.microsoft.com> wrote in message
news:BF721B4D-D5AD-45D3-81EF-8B0F19DF2344@microsoft.com...
> Hi,
> I am trying to make my script do what the subject says: Output PC Name,
> Serial #, IP, and , OS to CSV using vbs.
>
> The script pings a range of ip addresses and then spits back the SN and OS
> version. I would really perfer to not have to write down all the
> information
> it gives me. A CSV file would be ideal
>
> Here is my code thus far:
> (No, I did not write is all by myself)
> --------------------------
>
> On Error Resume Next
>
> intStartingAddress = 1
> intEndingAddress = 255
> strSubnet = "192.168.90."
>
> Const WbemAuthenticationLevelPktPrivacy = 6
>
> strCredentials = InputBox _
> ("Please enter the user name, a blank space, and then the password:", _
> "Enter User Credentials")
>
> If strCredentials = "" Then
> Wscript.Quit
> End If
>
> arrCredentials = Split(strCredentials," ")
> strUser = arrCredentials(0)
> strPassword = arrCredentials(1)
> strNamespace = "root\cimv2"
>
> For i = intStartingAddress to intEndingAddress
> strComputer = strSubnet & i
>
> Set objShell = CreateObject("WScript.Shell")
> strCommand = "%comspec% /c ping -n 2 -w 300 " & strComputer & ""
> Set objExecObject = objShell.Exec(strCommand)
>
> Do While Not objExecObject.StdOut.AtEndOfStream
> strText = objExecObject.StdOut.ReadAll()
> If Instr(strText, "Reply") > 0 Then
>
> Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
> Set objWMIService = objwbemLocator.ConnectServer _
> (strComputer, strNamespace, strUser, strPassword)
> objWMIService.Security_.authenticationLevel =
> WbemAuthenticationLevelPktPrivacy
>
>
> '
> ================================================== ===================
> winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& strComputer &""
> Set SerialN = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
>
> For each Serial in SerialN
> WScript.Echo "Serial Number: " & Serial.SerialNumber
> Next
>
> ' Constants for FileSystemObject
> Const FOR_READING = 1
> Const FOR_WRITING = 2
> Const FOR_APPENDING = 8
>
> strFileOutput = "c:\scripts\PCInfo.csv"
>
> ' Create a Script Runtime FileSystemObject.
> Set objFSO = CreateObject(" Scripting.FileSystemObject")
>
> ' Check to see if the output file exists. If so, open it for writing or
> appending.
> ' If not, create it and open it for writing.
>
> If objFSO.FileExists(strFileOutput) Then
> Set objOutputFile = objFSO.OpenTextFile (strFileOutput, FOR_WRITING)
> Else
> Set objOutputFile = objFSO.CreateTextFile(strFileOutput)
> End If
> If Err <> 0 Then
> Wscript.Echo "Unable to open " & strFileOutput & " for output."
> WScript.Quit
> End If
>
> ' Write header for file. CUrrent date and header, and LINE space
> objOutputFile.WriteLine "Date and Time" & _
> VbCrLf & "Taken " & Now & VbCrLf & VbCrLf & String(120, "-") & VbCrLf
>
> 'Create Headers for Host, NIC, IP and SubNet Mask
> objOutputFile.Writeline "Host Name, Network Card, IP Address, Subnet Mask"
>
>
> arrComputers = Array ( strcomputer )
>
> For Each strComputer In arrComputers
>
> ' Ping remote computer. If inaccessible, display error message.
> Set objShell = CreateObject("WScript.Shell")
> Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
> strPingResults = LCase(objScriptExec.StdOut.ReadAll)
> If InStr(strPingResults, "reply from") Then
>
> objOutputFile.WriteLine VbCrLf & "Host Name:," & VbCrLf & " " &
> strComputer
>
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
> ' Test for success in binding to WMI.
> If Err = 0 Then
> Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM " & _
> "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
> 'Echo to screen, or write to file
> For Each objNicConfig In colNicConfigs
> objOutputFile.WriteLine VbCrLf & "Network Adapter " &
> objNicConfig.Index
> objOutputFile.WriteLine " " & objNicConfig.Description & VbCrlF
> objOutputFile.WriteLine "IP Address(es):"
> For Each strIPAddress In objNicConfig.IPAddress
> objOutputFile.WriteLine " " & strIPAddress
> Next
> objOutputFile.WriteLine "Subnet Mask(s):"
> For Each strIPSubnet In objNicConfig.IPSubnet
> objOutputFile.WriteLine " " & strIPSubnet
> Next
> Next
> Else
> objOutputFile.WriteLine VbCrLf & "Error: Unable to connect to WMI." &
> VbCrLf & _
> "Error Number: " & Err.Number & VbCrLf & _
> "Error Source: " & Err.Source & VbCrLf & _
> "Error Description: " & Err.Description
> Err.Clear
> End If
>
> Else
>
> objOutputFile.WriteLine VbCrLf & "Host Name: " & strComputer & VbCrLf &
> _
> " Unable to connect."
>
> End If
>
> Next
>
> '
> ================================================== ===================
>
> Set colItems = objWMIService.ExecQuery _
> ("Select * From Win32_OperatingSystem")
> For Each objItem in ColItems
> Wscript.Echo strComputer & ": " & objItem.Caption
> Next
>
> '
> ================================================== ===================
> ' End
> '
> ================================================== ===================
>
> Else
> Wscript.Echo strComputer & " could be not reached."
> End If
> Loop
> Next
>
>
> ---------------------------------------------------------------
> I Don't care for the network card info. All I want to be put in the CSV is
> the PC Name, Serial Number, IP, OS, and if it pings but can't connect to
> the
> WMI then have it put the IP in the IP column and have it say "Not a
> computer"
> Or something along those lines, in another column.
>
> I need this for a really big project and this script will save me DAYS
> worth
> of work as compared to a couple of hours.
>
> Thank you all in advance just for reading all the way down to this bottom
> line!!!!!!



  Réponse avec citation
 
Page generated in 0,09568 seconds with 9 queries