PS Month of Lunches 2nd ed; CH 20 Sessions

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Jeff Taylor Jeff Taylor 5 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37267
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    I've been playing around with New-PSSession for several computerNames and setting them up in a variable ($DCs)

    I can retrieve those Sessions fine with Get-Sessions and then, wanted to remove those sessions altogether (close and then free up the variable) so I used Remove-PSSessions $DCs. It seemed to work.

    But when I repeat the New-PSSession for the same variable, I get a whole new set of session IDs (incremented sequentially)

    So, then I thought I would just clear and/or remove the variable $DCs altogether and that doesn't seem to work. I get no error with with either clear-variable $DCs or remove-variable $DCs yet, $DCs persists within my PS ISE admin console.

    1) I wish to generate new PS-Sessions using the same earlier session IDs (helps me understand PSSession)
    2) I wish to understand why I can't get rid of a manually defined variable name that persists (get-variable $DCs) without closing the shell.

    thanks

    #37270
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    When you run scripts by pressing the "Run" button (or pressing F5) in the ISE, it behaves a lot like you had dot-sourced a script at the powershell.exe prompt. Variables in the script's top scope will persist in the global session.

    You'll probably wind up with new session IDs every time you call New-PSSession; it shouldn't matter if they match whatever you had on a previous execution.

    #37273
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    I was using ISE's lower console, not running a script. Just running one liners in the book essentially and I like doing it in ISE

    #37275
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah. Same thing, then. If you assign something to a variable at the console, then it's going to stick around.

    #37278
    Profile photo of Jeff Taylor
    Jeff Taylor
    Participant

    thanks Dave: noted.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.