在 powershell 脚本中测试一台 PC 是否可以通过网络访问,如果可以则 运行 一个任务
Test if a PC is reachable over network and run a task if it is, in a powershell script
我有一个网络中的机器列表,想知道一台机器是否可以通过网络访问。我想 运行 每台机器的命令,但如果机器不可访问,我会得到一个错误,我想知道我是否可以 运行 在脚本上的每台 PC 上执行 ping 命令并捕获真实或如果 PC 在网络中则为 false
伪代码
-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 上安装最新的知识库更新
您要查找的是 Test-Connection
cmdlet。
您可以设置一个变量并测试它是对还是错。例如:
if $testConnection 'do something'
编辑:如果您使用 Quiet
参数,它 returns 每个测试连接的 System.Boolean 对象中的布尔值。
我有一个网络中的机器列表,想知道一台机器是否可以通过网络访问。我想 运行 每台机器的命令,但如果机器不可访问,我会得到一个错误,我想知道我是否可以 运行 在脚本上的每台 PC 上执行 ping 命令并捕获真实或如果 PC 在网络中则为 false
伪代码
-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 上安装最新的知识库更新
您要查找的是 Test-Connection
cmdlet。
您可以设置一个变量并测试它是对还是错。例如:
if $testConnection 'do something'
编辑:如果您使用 Quiet
参数,它 returns 每个测试连接的 System.Boolean 对象中的布尔值。