Reputation: 58
SORRY IF THIS IS CONSIDERED A REPOST, I DID DELETE THIS FROM THE PROGRAMMER STACK.
I am usually a silent user of this website. Picking bits and pieces of your code to better educate myself on the different ways of programming things.
I have run into a wall with VBScript, and I'm not sure how to word my issue.
I am trying to write the IP addresses of a computer (all of them) to a text file.
If you run the code below, it will output your computer's IP addresses (ipv6 and ipv4, or whatever you have active).
I want this to be written to a text file, but when I use a Scripting.FileSystemObject
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\" & SN.SerialNumber & ".txt",2,true)
I cannot simply do
objFileToWrite(strIP)
Any thoughts? This is still a work in progress, so I know my code is sloppy, so please be nice on that. This is kind of a mashup of 3-4 different code sources, and I have little idea of how VBScript works yet. I am trying to learn though!
Any help would be greatly appreciated.
'************Where I left off. Issue is converting object to String?
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems = objWMIService.ExecQuery( strQuery, "WQL", 48 )
For Each objItem In colItems
If IsArray( objItem.IPAddress ) Then
If UBound( objItem.IPAddress ) = 0 Then
strIP = "IP Address: " & objItem.IPAddress(0)
Else
strIP = "IPv4 and IPv6 Addresses: " & vbCrLf & Join(objItem.IPAddress, vbCrLf )
End If
End If
Next
WScript.Echo(strIP)
Update: @Noodles Apparently I don't have enough Rep to add pics?
Upvotes: 0
Views: 8306
Reputation: 58
Edit: @Noodles for credit. Stupid mistake on my part.
I figured it out I guess. I am not sure why it wasn't working last night (probably because I was trying to do this at 2 am) But here is the working block of code I have.
Sorry to waste your time guys, thanks for the input!
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//" & ""
'WScript.Echo winmgmt1
Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
dim WMI: set WMI = GetObject("winmgmts:\\.\root\cimv2")
dim Nads: set Nads = WMI.ExecQuery("Select * from Win32_NetworkAdapter where physicaladapter=true")
dim nad
dim strIP
'This is to grab the Directory of this script. It is stored in "CurrentDirectory"
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
dim CurrentDirectory
CurrentDirectory = fso.GetAbsolutePathName(".")
' Text box output.
for each SN in SNSet
MsgBox "Your serial number is: " & SN.SerialNumber & vbCrLf & vbCrLf & "SN saved to: " & CurrentDirectory & "\Computers by Sn\" & SN.SerialNumber & ".txt"
' Creates file.
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\Computers by SN\" & SN.SerialNumber & ".txt",2,true)
' Writes serial number to text file.
objFileToWrite.WriteLine("Your serial number is: " & SN.SerialNumber)
objFileToWrite.WriteLine("")
'*************************** Convert WScript to String?
Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
dim ComputerName
ComputerName = WshNetwork.ComputerName
'dim compName: set compName = CreateObject("Scripting.FileSystemObject")
'objFileToWrite("Computer Name: " & ComputerName)
'objFileToWrite(WshNetwork.ComputerName) <<<<<<<<<<<<<<<<<Not working. Tried CType(ComputerName, String)
'*************************** Not working properly. Needs to be in text file rather than Echo
' Writes MAC address to text file.
for each Nad in Nads
if not isnull(Nad.MACAddress) then objFileToWrite.WriteLine(Nad.description & ": " & Nad.MACAddress)
next
strQuery = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE MACAddress > ''"
'***********************************Where I left off. Issue is converting object to String
Set objWMIService = GetObject( "winmgmts://./root/CIMV2" )
Set colItems = objWMIService.ExecQuery( strQuery, "WQL", 48 )
For Each objItem In colItems
If IsArray( objItem.IPAddress ) Then
If UBound( objItem.IPAddress ) = 0 Then
strIP = "IP Address: " & objItem.IPAddress(0)
Else
strIP = "IPv4 and IPv6 Addresses: " & vbCrLf & Join(objItem.IPAddress, vbCrLf )
End If
End If
Next
WScript.Echo(strIP)
objFileToWrite.WriteLine(strIP)
'test = CType(strIP, String)
'objFileToWrite(test)
'***********************************Where I left off.
' Closes text file.
objFileToWrite.Close
Set objFileToWrite = Nothing
Next
Upvotes: 1
Reputation: 4836
The VBScript TextFile object's default method isn't WriteLine. I would also avoid your object creation/method chain and create each object via a Set
before calling methods on it:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileToWrite = objFSO.OpenTextFile("C:\Temp\test.txt",2,true)
objFileToWrite.WriteLine("Information to write - i.e. the IP address")
objFileToWrite.WriteLine()
objFileToWrite.Close
Upvotes: 0