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
}