2012-03-09 54 views
0

如果一台计算机在周三运行该脚本,我希望它在网络地图中创建一个%computername * .txt文件。在批处理脚本中,我可以使用%computername%.txt来创建它,我可以将哪些代码用于VBS脚本?Computername写入到txt文件(%computername%)


Dim WshShell 
Set WshShell = CreateObject("WScript.Shell") 

If DatePart("W",Date) = 4 Then 
    'Today is Wednesday, so run the virus scan 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.CreateTextFile("\\server\Avast Scan Log\%COMPUTERNAME%.txt") 
    WshShell.Run """C:\Program Files\Alwil Software\Avast4\aswcmd"" M: /m /*", , True 
End If 

回答

1
>> WScript.Echo CreateObject("WScript.Shell").ExpandEnvironmentStrings("%COMPUTERNAME%") 
>> 
WINXPSP3 
>> WScript.Echo CreateObject("WScript.Network").Computername 
>> 
WINXPSP3 
+0

嗨,Tnx为您的答复。它不起作用。它只是在窗口中显示Computername。我只是希望它在网络位置创建%computername%.txt文件。我们在这里有50台电脑。每台计算机都必须将自己的计算机名写入自己的txt文件。像com1.txt,com2.txt,com3.txt等 – e2e2 2012-03-09 14:49:54

+3

来吧,如何将返回的计算机名加入到你的脚本中,方法是将它加载到一个变量中,并在使用'objFSO.CreateTextFile'创建的文件名中使用该变量?我们在这里为您提供帮助,而不是在代码运行之前详细说明每一行代码。 – AutomatedChaos 2012-03-09 15:16:24

1

使用WshNetwork对象来获取计算机名称的建议。它看起来像这样。

Dim WshShell 
Set WshShell = CreateObject("WScript.Shell") 
Set WshNetwork = CreateObject("WScript.Network") 
strComputer = WshNetwork.ComputerName 

If DatePart("W",Date) = 4 Then 
    'Today is Wednesday, so run the virus scan 
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFile = objFSO.CreateTextFile("\\server\Avast Scan Log\" & strComputer & ".txt") 
    WshShell.Run """C:\Program Files\Alwil Software\Avast4\aswcmd"" M: /m /*", , True 
End If 
相关问题