Given:
PowerShell 5.1
Azure DevOps 2020
Windows Server 2016 Standard
Is there a way I can output message for each computer that successfully updates the LocalUser?
Invoke-Command -ComputerName Computer1,Computer2 -Credential $credential -ScriptBlock {
$securePassword = ConvertTo-SecureString -String 'P@$$w0rd123' -AsPlainText -Force
Set-LocalUser -Name User1 -Password $securePassword -Verbose
}
>Solution :
Since the cmdlet produces no output you can use a Try Catch statement:
Invoke-Command -ComputerName Computer1,Computer2 -Credential $credential -ScriptBlock {
$securePassword = ConvertTo-SecureString -String 'P@$$w0rd123' -AsPlainText -Force
try {
Set-LocalUser -Name User1 -Password $securePassword -Verbose -ErrorAction Stop
$status = 'Success'
}
catch {
$status = 'Fail'
$errmsg = $_.Exception.Message
}
[pscustomobject]@{
Computer = $env:COMPUTERNAME
Status = $status
Error = $errmsg
}
} -HideComputerName | Select-Object * -ExcludeProperty RunspaceId