none
Wait for command prompt - Enter-PSSession RRS feed

  • Question

  • Enter-PSSession -ComputerName ServerName
    Restart-Service DNS -PassThru
    Exit-PSSession

    I am trying to restart the DNS Server service on several remote machines using a foreach loop.  The problem is the the script does not wait until the command prompt shows up after executing the "Enter-PSSession -ComputerName ServerName".  I have not been able to fine a way to tell the script to wait for the prompt before continuing the script.   Thank you for any assistance that can be provided.

    Thursday, December 4, 2014 9:35 PM

Answers

  • Both of these options will work; however, you don't need to put the Invoke-Command inside a foreach. Its -ComputerName parameter will take a string of computer names. There's benefits to both ways, but regardless it's good to know about this option as well.

    $Servers = 'dc01','dc02'
    Invoke-Command -ComputerName $Servers -ScriptBlock {Restart-Service DNS}
    While I would definitely recommend Invoke-Command and PSRemoting, you can also do with with the Get-Service and Restart-Service cmdlets. This does not rely on PSRemoting and its -ComputerName parameter also accepts a string of computer names.
    $Servers = 'dc01','dc02'
    Get-Service -ComputerName $Servers -Name DNS | Restart-Service





    • Edited by tommymaynard Thursday, December 4, 2014 10:02 PM
    • Proposed as answer by jrv Friday, December 5, 2014 9:42 AM
    • Marked as answer by m1975Michael Friday, December 5, 2014 4:15 PM
    Thursday, December 4, 2014 10:01 PM

All replies

  • You cannot use Enter-PSSession in a script.  It can only be used interactively.

    Use Invoke-Command and specify the server you want to run it on in the -ComputerName parameter


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, December 4, 2014 9:42 PM
    Moderator
  • You can use Invoke-Command rather than entering a PS-Session.

    Something like this may do what you need:

    $servers = @("Server1.fqdn.example.com","Server2.fqdn.example.com","Server3.fqdn.example.com")
    $scriptblock = "Restart-Service DNS"
    
    foreach ($server in $servers) {
    	Invoke-Command -ScriptBlock $scriptblock -ComputerName $server
    }

    Replace the server names in $servers with your actual server names. This assumes of course you have PSRemoting enabled which you would need for the PSSessions.


    Thursday, December 4, 2014 9:52 PM
  • Both of these options will work; however, you don't need to put the Invoke-Command inside a foreach. Its -ComputerName parameter will take a string of computer names. There's benefits to both ways, but regardless it's good to know about this option as well.

    $Servers = 'dc01','dc02'
    Invoke-Command -ComputerName $Servers -ScriptBlock {Restart-Service DNS}
    While I would definitely recommend Invoke-Command and PSRemoting, you can also do with with the Get-Service and Restart-Service cmdlets. This does not rely on PSRemoting and its -ComputerName parameter also accepts a string of computer names.
    $Servers = 'dc01','dc02'
    Get-Service -ComputerName $Servers -Name DNS | Restart-Service





    • Edited by tommymaynard Thursday, December 4, 2014 10:02 PM
    • Proposed as answer by jrv Friday, December 5, 2014 9:42 AM
    • Marked as answer by m1975Michael Friday, December 5, 2014 4:15 PM
    Thursday, December 4, 2014 10:01 PM
  • Cool, I didn't know about passing an array of server names! Great info! Thank you. :D

    Thursday, December 4, 2014 11:26 PM
  • I would like to thank everyone for their assistance.
    Friday, December 5, 2014 4:18 PM