我有一个用VBS编写的impersonat脚本,它恳求输入计算机名称,如何使它从txt列表中提取计算机名称?

时间:2019-05-13 18:52:52

标签: vbscript

我有这个用VBS编写的模拟脚本。当前它要求输入计算机名称。如何使其从文本列表中提取计算机名称?我不希望输入计算机名称,而是希望它自动从文本文件中读取每个计算机名称。

我已经尝试过了,但是无论我怎么写都失败了。

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
strList = "computers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
arrComputers = Split(objList.ReadAll, vbCrLf)
objList.Close

For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objProcess = objWMIService.Get("Win32_Process")

    strCommand = "sc config lanmanserver start= demand"
    strCommand2 = "Net start lanmanserver"

    errReturn = objProcess.Create(strCommand, null, null, intProcessID)

    If errReturn = 0 Then
        WScript.Echo "Lanmanserver was changed to manual: " & intProcessID
    Else
        WScript.Echo "WinRM could not be started due to error: " & errReturn
    End If

    WScript.Sleep 5000 '5 seconds

    errReturn = objProcess.Create(strCommand2, Null, Null, intProcessID)

    If errReturn = 0 Then
        WScript.Echo "The Server Service was started with a process ID: " & intProcessID
    Else
        WScript.Echo "The Server Service could not be started due to error: " & errReturn

这是原始代码:

Set objShell = WScript.CreateObject("WScript.Shell")

objcomputer = InputBox("Please Enter a Computer Name to Start the Server Service on.")

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & objComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")

strCommand = "sc config lanmanserver start= demand"
strCommand2 = "Net start lanmanserver"

errReturn = objProcess.Create(strCommand, Null, Null, intProcessID)

If errReturn = 0 Then
    WScript.Echo "Lanmanserver was changed to manual: " & intProcessID
Else
    WScript.Echo "WinRM could not be started due to error: " & errReturn
End If

WScript.Sleep 5000 '5 seconds

errReturn = objProcess.Create(strCommand2, null, null, intProcessID)

If errReturn = 0 Then
    WScript.Echo "The Server Service was started with a process ID: " & intProcessID
Else
    WScript.Echo "The Server Service could not be started due to error: " & errReturn
End If

我需要它能够按文本文件插入计算机名称并为每个计算机名称重复

1 个答案:

答案 0 :(得分:0)

看起来Ansgar是对的,我不知道我在想什么!!!我尝试了下面的代码,它起作用了。谢谢!

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
strList = "computers.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objList = objFSO.OpenTextFile(strList, ForReading, False, TriStateUseDefault)
arrComputers = Split(objList.ReadAll, vbCrLf)
objList.Close

For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")


strCommand = "sc config lanmanserver start= demand"
strCommand2 = "Net start lanmanserver"

errReturn = objProcess.Create(strCommand, null, null, intProcessID)

 If errReturn = 0 Then
     Wscript.Echo "Lanmanserver was changed to manual: " & intProcessID
 Else
     Wscript.Echo "WinRM could not be started due to error: " & errReturn
 End If

wscript.sleep 5000 '5 seconds

errReturn = objProcess.Create(strCommand2, null, null, intProcessID)

 If errReturn = 0 Then
     Wscript.Echo "The Server Service was started with a process ID: " & intProcessID
 Else
     Wscript.Echo "The Server Service could not be started due to error: " & errReturn
end if
next