I'm writing a PowerShell-Script that reads all shares from all AD-Servers and outputs them into a csv-file. At the same time the script is saving all occuring errors and outputs them into an error-log. Can you guys see any possibility that could fasten up the whole process, because right now it takes quite a long time.
My code:
function output1 {
Get-Content C:\PowerShell\Shares\serverlist.txt | Where-Object { $_.name -like "*" } | ForEach-Object {
$ErrorActionPreference = 'silentlycontinue'
$name = $_ ".domain.com"
$pathnotnull = $_.Path -ne ""
invoke-command -ComputerName $name -ArgumentList $name, $pathnotnull -ScriptBlock {
param($name, $pathnotnull)
Get-SmbShare | where-object { $_.Path -ne '' } | Select-Object -Property "Name", "Path" | get-acl | Select-Object -Property "PSChildName", "Path", "Group", "AccessToString"
}
if (!$?) {
if ($error[0].exception.message) { Write-Host "Access to $_ failed!" -ForegroundColor Red }
$error | Set-Content $error_logfile -Encoding Unicode
}
else {
Write-Host "Access to $_ successful!" -ForegroundColor Green
return $i
}
}
}
$i = output1
$i | Out-Null
$ErrorActionPreference = 'silentlycontinue'
if ($i -ne $null) {
$i | export-csv -path C:\PowerShell\Shares\shares.csv -NoTypeInformation -delimiter ";"
}
else {
""
write-host "No server could be contacted!" -ForegroundColor Red
""
openerrorlog1
}
CodePudding user response:
Your code could be reduced to this I believe, as mklement0 suggests, Invoke-Command
can take an array of computers and invoke the same script block in parallel which will make your script run exponentially faster. I also don't see a need for a function, in this case. As for, capturing the errors from Invoke-Command
you could redirect them (2>&1
) to the success stream and then you can filter by -is [ErrorRecord]
:
$ErrorActionPreference = 'Continue'
$computers = (Get-Content C:\PowerShell\Shares\serverlist.txt).ForEach({
if(-not [string]::IsNullOrWhiteSpace($_))
{
"$_.domain.com"
}
})
$remoteCode = {
Get-SmbShare | Where-Object Path | Get-Acl |
Select-Object -Property "PSChildName", "Path", "Group", "AccessToString"
}
$results = Invoke-Command -ComputerName $computers -ScriptBlock $remoteCode 2>&1
$errors, $good = $results.Where({$_ -is [System.Management.Automation.ErrorRecord]}, 'Split')
$good | Export-Csv .....
# Here are the Errros
$errors.Exception.Message