Credential in Invoke-Command

Welcome Forums General PowerShell Q&A Credential in Invoke-Command

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #105569

    Participant
    Points: 0
    Rank: Member

    I am using an invoke command to pull out the WSUS service from list of server. However, for 1 server I need to use a different credential. Wondering how I can get this accomplished.

    sample as below:-

    # Input file
    import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
    Import-Module ActiveDirectory

    Set-Location ABC:

    $computername=(Get-CMSite).ServerName

    foreach ($C in $computername)
    {
    $userdata = (Invoke-Command -ComputerName $c -ScriptBlock {Get-service WsusService| select displayname, Status| sort-Object Status})

  • #105589

    Keymaster
    Points: 1,638
    Helping HandTeam Member
    Rank: Community Hero

    You would use the -Credential parameter of Invoke-Command.

  • #105592

    Participant
    Points: 7
    Rank: Member

    Since you have a foreach loop that iterates through the list of computers, something like this perhaps:

    #creds for separate server
    $creds = Get-Credential -Credential "domain\username"
    
    foreach ($C in $computername)
    {
        if ($c -match "name of server with own credentials")
        {
           Invoke-Command -Credential $creds
        }
        else
        {
            Invoke-Command
        }
    }
    
  • #105635

    Participant
    Points: 0
    Rank: Member

    Thanks Richard, but will this prompt for the password. I need to set this as Schedule task. I know, I am asking very basic thing here, but trying to get the hold of it.

The topic ‘Credential in Invoke-Command’ is closed to new replies.