如何解决在Powershell脚本中测试PC是否可以通过网络访问并运行任务如果可以
我有一个网络中的计算机列表,想知道一台计算机是否可以通过网络访问。我想在每台计算机上运行命令,但是如果无法访问计算机,则会出现错误,我想知道是否可以在脚本上的每台PC上运行ping命令,并且如果PC在网络中则捕获true或false。 / p>
伪代码
-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捕获真假?
到目前为止,如果列表中的每台PC都可以访问,则代码可以正常工作
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更新
解决方法
您正在寻找的是Test-Connection
cmdlet。
您可以设置一个变量并测试这是对还是错。例如:
if $testConnection 'do something'
编辑:如果您使用Quiet
参数,它将在System.Boolean对象中为每个测试的连接返回一个布尔值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。