如何将注册表编辑脚本与exe执行与VBScript集成

时间:2017-08-22 09:49:12

标签: windows powershell remote-server

我正在尝试编写应该更改远程服务器的注册表设置(WSUS服务器名称)的脚本。一旦成功脚本应该执行exe和存储在服务器上安装补丁的远程服务器上的VBScript文件。以下是我的进展。

  1. 更改服务器的WSUS设置。这样做了,我能够在远程服务器上的注册表中更改WSUS服务器并将结果以CSV文件的形式获得。
  2. 一旦上述步骤成功,我的下面的脚本应该能够在远程服务器上执行上述命令,这是当前没有发生的。
  3. 脚本:

    #defined variables to use in script as below.##
    $Computers = Get-Content "C:\serverlist.txt"
    $Path      = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
    $property  = "WUServer"
    $value     = "http://serverFQDN"
    
    $results = foreach ($computer in $Computers) {
        if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
            try {
                Set-ItemProperty -Path $path -Name $Property -Value $Value -ErrorAction 'Stop'
                $status = "WSUS server has been set to http://serverFQDN"                          
                foreach ($computer in $Computers) {
                    # below step is not getting executed on remote servers. I do not
                    # get any error but above script do not execute any command on
                    # remote servers.
                    foreach ($computer in $computers) {
                        Invoke-Command -ScriptBlock {
                            C:\test\Patcher > cscript.exe -nologo test_v1.03.vbs /anlyze:false /reboot:false | Out-Null
                        }
                    }
                }
            } catch {
                $status = "Failed"
            }
        } else {   
            $status = "Server is not reachable, please check connection and try again"
        }
    
        New-Object -TypeName PSObject -Property @{
            'Computer' = $computer
            'Status'   = $status
        }
    }
    
    $results | Export-Csv -NoTypeInformation -Path "./out.csv"
    

1 个答案:

答案 0 :(得分:0)

这不是有效的语法:

C:\test\Patcher > cscript.exe -nologo test_v1.03.vbs ...

该语句将(尝试)运行位于目录Patcher中的命令C:\test,并将其输出重定向到当前工作目录中的文件cscript.exe

您可能希望将工作目录更改为C:\test\Patcher并从该目录运行test_v1.03.vbs。为此,请将上述行更改为:

Set-Location 'C:\test\Patcher'
& cscript.exe //NoLogo test_v1.03.vbs /anlyze:false /reboot:false | Out-Null