PowerShell脚本工作正常,但需要8+;运行时间
我有一个PowerShell脚本,它从服务器上获取配置文件,解析一些数据,然后ping设备以查看它们是在线还是离线。它的工作100%符合预期,但运行时间超过8小时。我只是想知道我的逻辑中是否有什么东西导致了这种情况的发生。我确实意识到这可能与其他问题有关,但我想排除这种可能性。任何人都能看到逻辑中的错误,导致它花费更长的时间,例如,如果它卡在某种循环中,等等 这是我的密码:PowerShell脚本工作正常,但需要8+;运行时间,powershell,Powershell,我有一个PowerShell脚本,它从服务器上获取配置文件,解析一些数据,然后ping设备以查看它们是在线还是离线。它的工作100%符合预期,但运行时间超过8小时。我只是想知道我的逻辑中是否有什么东西导致了这种情况的发生。我确实意识到这可能与其他问题有关,但我想排除这种可能性。任何人都能看到逻辑中的错误,导致它花费更长的时间,例如,如果它卡在某种循环中,等等 这是我的密码: $logfile = "D:\Logs\MPOSPrinterPingLog.txt" $offlineprinters
$logfile = "D:\Logs\MPOSPrinterPingLog.txt"
$offlineprinters = "D:\Reports\MPOS\MPOSOfflinePrinters.txt"
if (Test-Path $logfile) {Remove-Item $logfile}
if (Test-Path $offlineprinters) {Remove-Item $offlineprinters}
Add-Content $logfile "Setting local path"
$localPath = "C:\Temp\MPOS"
Add-Content $logfile "Gathering server list"
$serverList = (Get-ADComputer -Filter "Name -like 'Q0*P30' -or Name -like 'Q0*P32'" -SearchBase "OU=Print,OU=Prod,OU=POS,DC=COMPANY,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSPrintServers.txt
$serverListPath = "C:\Temp\MPOS\MPOSPrintServers.txt"
#Retrieve a list of MPOS Print servers from text file and set to $serverNames
Add-Content $logfile "Compiling text file"
$serverNames = Get-Content -Path $serverListPath
#Iterate through each of the server names
foreach ($serverName in $serverNames) {
Add-Content $logfile "Processing $serverName"
#Check if the server is online before doing the remote command
if (Test-Connection -ComputerName $serverName -Quiet -count 1) {
Add-Content $logfile "$serverName is online"
#copy config file from MPOS print to local server for processing
$timestamp1 = (Get-Date -Format g)
Add-Content $logfile "$timestamp1 - Copying xml file from server to local path"
$configPath = "\\$($serverName)\C$\ProgramData\Microsoft\Point Of Service\Configuration\Configuration.xml"
Copy-Item $configPath $localPath
#process xml file to parse IP addresses for ping
$timestamp2 = (Get-Date -Format g)
Add-Content $logfile "$timestamp2 - Processing xml file from $serverName to parse data to csv"
$xml = [xml](Get-Content C:\Temp\MPOS\Configuration.xml)
$PrinterNames = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{LogicalName=$_.LogicalName.Name}}
$PrinterIPs = $xml.selectNodes('//PointOfServiceConfig/ServiceObject/Device') | foreach {New-Object -TypeName psobject -Property @{HardwarePath=$_.HardwarePath}}
foreach ($PrinterIP in $PrinterIPs) {
$pingableIP = $PrinterIP.HardwarePath
if (Test-Connection $pingableIP -Quiet -Count 1) {
$timestamp3 = (Get-Date -Format g)
Add-Content $logfile "$timestamp3 - $serverName, $pingableIP is online and pingable"
} else {
$timestamp3 = (Get-Date -Format g)
Add-Content $offlineprinters "$timestamp3 - $serverName, $pingableIP is offline!"
}
}
} else {
Add-Content $logfile "$serverName is offline!"
}
}
所以我相信你正面临着ping带来的问题,我不认为广告查询占用了大部分时间,所以你应该做的是,并行ping。这将是一个很好的开始
foreach ($serverName in $serverNames) {
start-rsjob -Name $_ -ScriptBlock {
# your code in the loop goes here #
}
}
Get-RSjob | Receive-RSJob
或者您可以编写自己的运行空间包装器;)
如果您感兴趣的话,我有一个脚本解析文本文件,它需要6-7分钟才能完成,在我实现了运行空间来并行处理之后,它开始在5毫秒内完成。这有多疯狂?xD哪一部分耗时最长?广告查询或ping?有多少设备<代码>测试连接是同步的,不允许您控制超时。我建议您在可能导致问题的位置放置一些echo,例如,查看它在循环中迭代了多少次。我认为这也是问题所在,而PoshRSJob是一个很棒的模块。我将尝试这个,非常感谢您的帮助和好意!