Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I show progress for each computer where the password was changed successfully

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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading