测试PC是否可以通过网络访问,如果可以,则在powershell脚本中运行任务
我有一个网络中的机器列表,我想知道一台机器是否可以通过网络访问。我想在每台机器上运行一个命令,但如果机器不可访问,我将得到一个错误,我想知道我是否可以在脚本上的每台电脑上运行一个ping命令,如果电脑在网络中,是否可以捕获一个true或false 伪码测试PC是否可以通过网络访问,如果可以,则在powershell脚本中运行任务,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我有一个网络中的机器列表,我想知道一台机器是否可以通过网络访问。我想在每台机器上运行一个命令,但如果机器不可访问,我将得到一个错误,我想知道我是否可以在脚本上的每台电脑上运行一个ping命令,如果电脑在网络中,是否可以捕获一个true或false 伪码 -Get file name by opening a Windows file search menu. -Get file with all hosts to run command on -Iterate over host
-Get file name by opening a Windows file search menu.
-Get file with all hosts to run command on
-Iterate over hosts
-run if statement ( if PC pingable )
-run main action command
-if PC not pingable
skip and repeat ..
有没有一种方法可以从ping中捕捉到真假
到目前为止,如果列表中的每台电脑都可以访问,我的代码就可以正常工作
Start-Transcript -Path "$(Get-location)\Client-log-$(Get-date -Format "yyyyMMddTHHmmss").log"
Function Get-FileName{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = Get-Location
$OpenFileDialog.filter = “All files (*.*)| *.*”
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$Importfile = Get-FileName
$Clients = Get-Content $Importfile
Foreach($Client in $Clients){
#inser conditional to check if ping is good or bad
(Get-HotFix -ComputerName $Clients | Sort-Object -Property InstalledOn)[-1]
}
Stop-Transcript
此脚本的基本思想是在PC上安装最新的KB更新您要查找的是
测试连接
cmdlet
您可以设置一个变量并测试它是否为真。例:
if $testConnection 'do something'
编辑:如果使用
Quiet
参数,它将在System.Boolean对象中为每个测试连接返回一个布尔值。查看获取帮助测试连接。[咧嘴笑]谢谢!这很有帮助。我用这本书做了我需要做的事情,非常欢迎!很高兴能帮上一点忙。。。[咧嘴笑]@Lee_Dailey嘿,我还有一个问题,关于这件事,有没有更有效的方法?我的意思是,它现在可以工作了。但是数据回溯需要很长时间,在网络上运行查询总是很慢的。[grin]我会使用Invoke命令
在目标系统上运行Get HotFix
。。。让I-C
呼叫处理无应答者。查看-ErrorVariable
参数,了解如何灵活处理错误。