powershellactive-directoryinvoke-commandforeach-object

PowerShell-script with invoke-command gets stuck on specific computers


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. The script will be run as a weekly task. When I run the script, all goes well until it gets to one specific server on which it just loads infinitely. The AD-Server (Windows Server_2012_R2) is inside a normal OU and other servers in this OU work fine.

I believe that there are a few others on which the problem occures as well but I'm not sure how many exactly (about one in 150).

It does not produce an error.

Do you know what could be the Problem here?

Code:


$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 | Sort-Object PSComputerName | Select-Object "PSComputerName", "PSChildName", "Path", "Group", @{ Name = "AccessToString"; Expression = { $_.AccessToString -replace("268435456", "FullControl") -replace("-1610612736", "ReadAndExecute")}} | export-csv -path C:\PowerShell\Shares\shares.csv -NoTypeInformation -delimiter ";"

$errors.Exception.Message | Set-Content $error_logfile -Encoding Unicode

Solution

  • Alright I now found the problem. Two servers where frozen and that resulted in the never ending PowerShell script. Now I think I will configure some Sort of timeout, does anyone of you know how I could do that the best way? Thank you all for your help.