2016-09-16 246 views
0

如果這被認爲是一個REPOST,我從程序堆棧中刪除了這個。VBScript將對象轉換爲字符串?

我通常是這個網站的無聲用戶。爲了更好地教導自己對事物編程的不同方式,選擇你的代碼片段。

我已經遇到了VBScript的牆,我不知道如何說出我的問題。

我想寫一臺計算機的IP地址(所有這些)到一個文本文件。

如果您運行下面的代碼,它會輸出您的計算機的IP地址(ipv6和ipv4,或任何您已激活的)。

我想這是寫入到一個文本文件,但是當我使用一個Scripting.FileSystemObject

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentDirectory & "\" & SN.SerialNumber & ".txt",2,true) 

我不能簡單地做

objFileToWrite(strIP) 

有什麼想法?這仍然是一個正在進行的工作,所以我知道我的代碼是sl,的,所以請對此很好。這是一種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

'objFileToWrite.writeline objItem.IPAddress(0)' – 2016-09-16 18:34:12

+0

@Noodles不成功。作爲編輯附加錯誤。 –

+0

@BillyKleinhomer請不要將錯誤發佈爲圖片,而應使用簡單的塊引用'>'請參閱[如何使用Markdown或HTML格式化我的帖子](http://stackoverflow.com/help/formatting)。 – Lankymart

回答

0

VBScript的文本文件對象的默認方法不是的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 
0

編輯:@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