I have a PS script with invoke-command. I want to update the value if I haven't retrieved anything for that machine before.
This command takes long time. We have about 10000 computers inside computers CSV file.
Invoke-Command -ComputerName accepts an array of computers
How can I speed up the this script?
Here is my script :
$computers = Import-Csv "C:\temp\computers.csv"
$AllResults = foreach ($computer in $computers) {
try {
if (($computer.Appx -eq 'Port closed or computer unreachable') -Or ($null -eq $computer.Appx)){
Invoke-Command -ComputerName $computer.name -ScriptBlock { Get-AppxPackage -AllUsers } -ErrorAction 'Stop' | ForEach-Object {
[pscustomobject]@{
"ComputerName" = $computer.Name
"Appx" = $_.Name -join ";"
}
}
}
}
catch {
[pscustomobject]@{
"ComputerName" = $computer.Name
"Appx" = "Port closed or computer unreachable"
}
}
}
$AllResults | Export-Csv -Path "C:\Temp\MSStoreReport.csv" -NoTypeInformation -Encoding 'UTF8'