使用ping响应重新启动电源shell脚本

时间:2018-03-09 19:50:06

标签: powershell windows-defender

下午好!

我有以下脚本来自动运行Update-MpSignature,然后使用Start-MpWDOScan运行防御程序脱机扫描。我使用ping 120来运行短期ping以验证计算机是否已关闭但必须等待它,或者中断并重新运行脚本以获得打开备份的提示。有没有办法在接收到超时重启脚本的请求时使用IF函数,以便我可以输入下一台机器?

由于

param (
[string]$compname = $( Read-Host "Input computer name, please" )
    )
    Write-Output "Update-MpSignature"
    Update-MpSignature -CimSession $compname
    Write-Output "Start-MpWDOScan"
    Start-MpWDOScan -CimSession $compname
    Write-Output "Ping "$compname" for two minutes"
    ping $compname -n 120

1 个答案:

答案 0 :(得分:0)

不要使用Ping,因为它是外部的,请使用PowerShell cmdlet Test-Connection查看服务器是否已关闭,并且您可以使用Write-Progress来跟踪它所处的状态

param (
[string]$compname = $( Read-Host "Input computer name, please" )
    )
    Write-Output "Update-MpSignature"
    Update-MpSignature -CimSession $compname
    Write-Output "Start-MpWDOScan"
    Start-MpWDOScan -CimSession $compname
    Write-Output "Ping "$compname" for two minutes"
    While(Test-Connection $compname -Quiet -Count 1){
        Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to shut down."
        Start-Sleep -sec 1
    }
    While(!(Test-Connection $compname -Quiet -Count 1)){
        Write-Progress -Activity "Rebooting $compname" -Status "Waiting for $compname to come back up."
        Start-Sleep -sec 1
    }

编辑:从文件中读取服务器很简单,只需使用Get-Content读取文件,然后使用ForEach循环遍历列表。您需要一个简单的文本文件,每行一个服务器名称。在我的示例中,我将使用位于用户桌面上的名为servers.txt的文件。您可能希望像这样构建文件:

SQLServerA
FileServerA
WebServerA
SQLServerB
FileServerB
WebServerB

您可以使用默认位置对文件的路径进行硬编码,也可以将参数更改为指向文件。然后,您可以读取该文件并将内容存储在如下变量中:

$complist = Get-Content $listpath

这会将$complist设置为字符串数组,其中数组中的每个项目都是文本文件中的一行。接下来,您将使用ForEach循环遍历它:

ForEach($compname in $complist){
   <code to do stuff>
}

所以最后整个事情看起来像这样:

param (
    $listpath = "$home\desktop\servers.txt"
)

#Import server list
$complist = Get-Content $listpath

#Loop through the list of servers
ForEach($compname in $complist){
    Write-Progress -Activity "Processing $compname" -CurrentOperation "Updating Signature on $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
    Update-MpSignature -CimSession $compname
    Write-Progress -Activity "Processing $compname" -CurrentOperation "Initializing offline scan of $compname." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
    Start-MpWDOScan -CimSession $compname
    While(Test-Connection $compname -Quiet -Count 1){
        Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to go offline." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
        Start-Sleep -sec 1
    }
    While(!(Test-Connection $compname -Quiet -Count 1)){
        Write-Progress -Activity "Processing $compname" -CurrentOperation "Waiting for $compname to come back up." -Status "Server $($complist.IndexOf($compname) + 1) of $($complist.count)"
        Start-Sleep -sec 1
    }
}

然后,只要你的桌面上有servers.txt,就可以运行脚本,它会给你一个状态栏,说明它正在做什么,它在做什么服务器,并给'服务器X Y'的地位,所以你知道你有多远。