我经常使用PowerShell进行自动批量修补。我通常必须得到一个机器列表,ping测试,获取该文件的输出并将其放入PowerShell,移动文件,然后我使用PSEXEC
来执行bat文件。 (我无法使用PowerShell进行远程执行,WinRM在我的世界中被阻止了。)
我找到了一个很好的脚本,用于从ping组中移除用户,如果没有响应,继续前进,但如果有,则执行命令并移至下一个。我尝试根据自己的用途来调整,移动文件,如果没有响应,请继续。它看起来像现在一样,脚本正在ping,移动,如果没有响应,它会错误地显示"网络路径未找到"。
我的PS脚本如下:
#Install Cisco AnyConnect Core VPN and ISE Posture Installation
#Author: Erik R. Little
#Creation Date: 1 November 2017
#This script will do the following actions:
#1.- Get a computer list from a TXT file.
#2.- Do a ping to every computer on the list, if the computer is offline it will skip it and pass to the next one.
#3.- If the computer answers the ping it will push the AnyConnect installation files to the remote machine.
#4.- Creates a log with all the transactions.
# Log Time Variables
$Date = Get-Date -UFormat %b-%m-%Y
$Hour = (Get-Date).Hour
$Minuntes = (Get-Date).Minute
$Log = "C:\support\cisco\Log Files\AnyConnect Install-" + $Date + "-" + $Hour + "-" + $Minuntes + ".log"
#Creates a log file for this process.
Start-Transcript -Path $Log -Force
#List of computers to be check
$ComputerNames = Get-Content "C:\support\cisco\Computers.txt"
#Ping the computers on the list
foreach ($ComputerName in $ComputerNames) {
if (-not (Test-Connection $ComputerName -Quiet -Count 1 -ErrorAction Continue)) {
#If theres no ping answer pass to the next one
Write-Output "Computer $ComputerName not reachable (PING) - Skipping this computer..."
} else {
#If computer does answer the ping
Write-Output "Computer $ComputerName is online"
}
}
foreach ($ComputerName in $ComputerNames) {
Write-Host $ComputerName
if ((Test-Path "\\$ComputerName\c$\support\cisco") -eq 0) {
mkdir "\\$ComputerName\c$\support\cisco"
robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
} elseif ((Test-Path "\\$ComputerName\c$\support\cisco") -eq 1) {
robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
}
}
}
}
Stop-Transcript
总而言之:如果没有响应,我希望脚本ping并继续前进,但是如果有ping,请移动文件,然后移动到下一个。
答案 0 :(得分:4)
使用单个foreach
。
我还更新了Test-Connection
和Test-path
的逻辑,因此代码更清晰。
#Creates a log file for this process.
Start-Transcript -Path $Log -Force
#List of computers to be check
$ComputerNames = Get-Content "C:\support\cisco\Computers.txt"
foreach ($ComputerName in $ComputerNames) {
if (Test-Connection $ComputerName -Quiet -Count 1 -ErrorAction Continue ) {
Write-Output "Computer $ComputerName is online"
if (Test-path "\\$ComputerName\c$\support\cisco") {
robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
}
else {
mkdir "\\$ComputerName\c$\support\cisco"
robocopy /mir "C:\support\cisco" "\\$ComputerName\c$\support\cisco"
}
}
Else {
Write-Output "Computer $ComputerName not reachable (PING) - Skipping this computer..."
}
}
Stop-Transcript
修改强>
删除所有重复的代码和值将为您提供更简洁的foreach
循环:
foreach ($Computer in $ComputerNames) {
$source = "C:\support\cisco"
$destination = "\\$Computer\c$\support\cisco"
if (Test-Connection $Computer -Quiet -Count 1) {
Write-Output "Computer $Computer is online"
if (-not (Test-path $destination)) {New-Item -Path $destination -ItemType Directory}
robocopy /mir $source $destination
}
else {
Write-Output "Computer $Computer not reachable (PING) - Skipping this computer..."
}
}