如果这被认为是一个存在,那我就把它从程序员堆栈中删除了。
我通常是这个网站的沉默用户。选择代码中的一些部分,以便更好地了解不同的编程方式。
我遇到了VBScript的问题,我不确定如何解释我的问题。
我正在尝试将计算机的IP地址(所有这些)写入文本文件。
如果您运行下面的代码,它将输出您的计算机的IP地址(ipv6和ipv4,或任何您有效的)。
我希望将其写入文本文件,但是当我使用Scripting.FileSystemObject
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\" & SN.SerialNumber & ".txt",2,true)
我不能简单地做
objFileToWrite(strIP)
有什么想法?这仍然是一项正在进行的工作,所以我知道我的代码很草率,所以请你好好处理。这是一个由3-4种不同代码源组成的混搭,我对VBScript的工作方式一无所知。我试着学习!
非常感谢任何帮助。
'************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)
更新:@Noodles Apparently I don't have enough Rep to add pics?
答案 0 :(得分:1)
编辑:@Noodles获得信用。我这个愚蠢的错误。
我猜想我猜对了。我不知道为什么它昨晚没有工作(可能是因为我试图在凌晨2点这样做)但这里是我的代码工作块。
很抱歉浪费你的时间,谢谢你的意见!
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
答案 1 :(得分:0)
VBScript TextFile对象的默认方法不是WriteLine。我还会避免你的对象创建/方法链,并在调用方法之前通过Set
创建每个对象:
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