Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

PowerShell: Write-Error inside Do/While

$
0
0
Powershell
Do{$DC=(Get-ADDomainController-Filter{IsReadOnly-eq$false}|sortname-Descending|select-Skip$DCIterator-First1).nameIf(!(Get-ADUser$SamAccountName-Server$DC-ErrorAction'SilentlyContinue')){Write-Error-Message"Some error message"}Else{$DCSelected=$DC}$DCIterator++}While($DCSelected-eq$null)
I use this piece of code to identify the proper DC to target when updating/altering a newly-created AD object that may not have replicated to all DCs yet.

The above code contains two additions to what runs in production. 1) "-ErrorAction 'SilentlyContinue'"; and 2) "Write-Error -Message "Some error message""

In testing, I attempted many permutations of code trying to achieve the effect one would expect from the above. However, what I get are standard error messages of not finding the account, but never my custom...


Viewing all articles
Browse latest Browse all 15370

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>