Ich habe ein PowerShell-Skript, das eine Konfigurationsdatei von einem Server liest, einige Daten analysiert und dann Pings an Geräte sendet, um festzustellen, ob sie online oder offline sind. Es funktioniert 100% wie erwartet - aber es dauert über 8 Stunden zu laufen. Ich frage mich nur, ob etwas in meiner Logik das verursacht. Ich weiß, dass es mit anderen Themen zusammenhängen könnte, aber ich wollte das ausschließen. Kann irgendjemand einen Fehler in der Logik sehen, der dazu führen würde, dass er länger dauert, d. H. Wenn er in einer Schleife irgendeiner Art usw. steckenbleibt?PowerShell-Skript funktioniert einwandfrei, dauert aber mehr als 8 Stunden
Hier ist mein Code:
$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!"
}
}
Was ist der Teil, der am längsten dauert? AD-Abfrage oder die Pings? – 4c74356b41
Wie viele Geräte? 'Test-Connection' ist synchron und lässt Sie das Timeout nicht kontrollieren. – briantist
Ich schlage vor, dass Sie ein paar Echos an Stellen platzieren, die das Problem verursachen können, sehen Sie, wie oft es zum Beispiel durch die Schleife iteriert. – briansrls