Send variable list of computernames to New-PSSession

Welcome Forums General PowerShell Q&A Send variable list of computernames to New-PSSession

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 6 months ago.

  • Author
  • #30295

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    I'm probably just making a very simple mistake here but the following line works:
    $PSSessionList = New-PSSession -ComputerName CACDC01,CACDC03,CACDS01,CACFPS -Credential (Get-Credential -UserName "$env:USERDOMAIN\$env:USERNAME" -Message "Please enter credentials for $env:USERDOMAIN")

    However, when I try to do the following it fails:
    $ComputerList = "CACDC01,CACDC03,CACDS01,CACFPS"

    $PSSessionList = New-PSSession -ComputerName $ComputerList -Credential (Get-Credential -UserName "$env:USERDOMAIN\$env:USERNAME" -Message "Please enter credentials for $env:USERDOMAIN")

    How can I get New-PSSession to take a list I build up dynamically and then open a possession to them?

    In the end I will check what computers on the network has been discovered, open a session to them and run a bunch of commands on them.

  • #30298

    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

    You are nearly right. Your problem is you've created a string containing multiple computer names instead of a collection strings each containing a computer name

    This works
    PS> $computers = 'W12R2SUS', 'W12R2DSC'
    PS> $ps = New-PSSession -ComputerName $computers
    PS> $ps

    Id Name ComputerName State ConfigurationName Availability
    — —- ———— —– —————– ————
    2 Session2 W12R2DSC Opened Microsoft.PowerShell Available
    1 Session1 W12R2SUS Opened Microsoft.PowerShell Available

    You need to change

    $ComputerList = "CACDC01,CACDC03,CACDS01,CACFPS"


    $ComputerList = 'CACDC01', 'CACDC03', 'CACDS01', 'CACFPS'

The topic ‘Send variable list of computernames to New-PSSession’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort