我经常使用PowerShell进行自动批处理修补。我通常需要获得一个机器列表,ping测试,获取该文件的输出并将其放入PowerShell,移动文件,然后使用PSEXEC执行bat文件。(我不能使用PowerShell远程执行,WinRM在我的世界中被阻塞)。
我找到了一个很好的脚本,用于从pings组中移除用户,如果没有响应,请继续,但如果有,则执行命令并移到下一个。我正在尝试将它用于我自己的用途,使之适合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,则移动文件,然后移动到下一个。
发布于 2017-11-02 13:06:29
使用单个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..."
}
}https://stackoverflow.com/questions/47075426
复制相似问题