locked
Format of script RRS feed

  • Question

  • Morning all.  I was offered a solution to a previous problem on this forum, and now that created another head scratcher issue.  

    Using the steps shown:

    $pc=read-host " Enter PC Canis " 
    Enter-PSSession -computername $pc
    Get-WmiObject win32_userprofile|select localpath
    $pcc=read-host -prompt " Press enter when finished " 
    exit

    If I run each step from the powershell command prompt, I get the expected results from the remote computer.  If I run these exact sames steps as a script,  the steps occur as expected, but the outcome receive ends up being from the computer I am sitting at, not the remote PC. 

    So I am not clear if maybe the time it takes to make the remote session is to long, or if the script throws away the info before going to the "Press enter when finished " step,  and I don't see it, or if another wait style command is needed to get the info retrieved from the remote system and retain it on screen, before continuing on.  

    In summary, the " Get-WmiObject win32_userprofile|select localpath " command processes after the " $pcc=read-host -prompt " Press enter when finished " command. 

    What is the mystery of this sequence? 

    Thanks in advance...

    Wednesday, March 29, 2017 3:51 PM

Answers

  • This is because when you type the Enter-PSSession at a command prompt, the next prompt is on the remote computer. If you run it from a script, you're entering the remote session, and the subsequent commands in the script do not execute until after you exit the remote session.

    The lesson here is that Enter-PSSession is for interactive use only.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 29, 2017 4:31 PM