Send variable list of computernames to New-PSSession

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Richard Siddaway Richard Siddaway 1 year ago.

  • Author
  • #30295
    Profile photo of Nakago .
    Nakago .

    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
    Profile photo of Richard Siddaway
    Richard Siddaway

    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'

You must be logged in to reply to this topic.