If loop works fine when running on ISE's F5/F8 but not when executed on console

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 11 months ago.

  • Author
    Posts
  • #21360
    Profile photo of GJ
    GJ
    Participant

    Hi Guys,

    I am a powershell newbie , learning on PS3.

    I have the below script which fails(if loop not functioning properly) when executed as a script in the console. But if I run it with ISE's F5/F8 it works fine. Am I doing something wrong here?

    Script –

    $server = 'SERVER1'
    if ( New-PSDrive -Name dummy -PSProvider FileSystem -Root \\$server\C$ -ErrorAction SilentlyContinue) 
    {
        write success
    }
    else 
    {
        write failed
    }
    

    [i]SERVER1 is a remote host, it is online, but I do not have admin rights on this host. [/i]

    Sample output ran with F5/F8/Console

    When run with ISE's F8 – works as expected.

    
    
    PS C:\Scripts\POSH> $server = 'SERVER1'
    if ( New-PSDrive -Name dummy -PSProvider FileSystem -Root \\$server\C$ -ErrorAction SilentlyContinue) 
    {
        write success
    }
    else 
    {
        write failed
    }
    failed
    

    When run with ISE's F5 (Run Script)- works as expected.

    PS C:\Scripts\POSH> C:\Scripts\POSH\draft.ps1
    failed
    

    [b]When executed as script in ISE's Console or Powershell's console. IT FAILS HERE. [/b]

    
    PS C:\Scripts\POSH> .\draft.ps1
    success
    
    
  • #21363
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, if you have no admin rights but you're conndcting to the admin share, you'd expect it to fail, right? You should read The Big Book of PowerShell Error Handling (free ebook). Your approach for detecting the error isn't reliable.

  • #21366
    Profile photo of GJ
    GJ
    Participant

    Thanks for the response Don, May be my description was not clear in the question. My error handling approach may be wrong, but my question is different.

    As you rightly said, If I do not have admin rights on the remote machine, the IF condition must fail and it should go into the ELSE loop. Right? This is not happening when I run it in the console.

    [b]The results of the script contradict when run with ISE's Run script[F5]/Run selection[F8] and on console. WHY and how can I fix it?[/b]

  • #21534
    Profile photo of Jack Neff
    Jack Neff
    Participant

    Every shell has it's own independent environmental variables (PSDrives).

    The script isn't saying "Failed" because you don't have access to the remote file system it's saying "Failed" because a PSDrive called 'dummy' already exists in that environment. Any new console you open will not have a PSDrive called 'dummy' and the first time you run the script in that console (ISE or powershell) it will say "Success" because the 'New-PSDrive' command doesn't care if you have permission to file system you're mapping a name to unless you use the -Credentials parameter. It's not until you try to open that PSDrive is when you get the error that you're trying to test for with your if statements.

    To verify what I'm saying close and reopen the ISE or run this command:

    Get-PSDrive dummy | Remove-PSDrive

    Then run your script again and I'll bet it'll say "Success" the first time as in "I successfully created a PSDrive called 'dummy' but have no idea if I can open it yet!" Then when you run it a second time it will say "Failed".

    I think that covers the discrepancy between what you are trying to figure out and what Don said but you should definitely check out the "Try, Catch, Finally" construct and as an additional word of advice if you're ever trying to trap an error you do not want to use -SilentlyContinue.

  • #21565
    Profile photo of GJ
    GJ
    Participant

    Thank you Jack. Yes, I read the error handling ebook referred by Don and it was very helpful.

  • #21570
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    On a side note, when you run a script in the ISE using the F5 key (or by clicking the "Run Script" toolbar button), it has the effect of running the script out in the global scope, the same as if you had dot-sourced it at the console. When you run the script by typing its name manually in the ISE's console pane, or by doing the same at a powershell.exe prompt, then the script gets its own child scope under Global.

    This changes the behavior of your New-PSDrive command, since the default scope is Local and not Global. When you dot-source the script or run it with F5, the "local" scope IS the global scope, and the PSDrive will persist past the end of the script's execution, all the way until you close the host. When you run the script in its own scope, the PSDrive is removed as soon as the script is finished.

    This ISE behavior can be a little confusing sometimes, but it can have some advantages. Because the script is run out in the global scope, any variables that were set by the script can be examined in the script pane, and any functions that were defined by the script can be run directly as well. This can be helpful while you're working on developing and testing code.

  • #21573
    Profile photo of GJ
    GJ
    Participant

    Thank you Dave. Yes, I understood it after watching the Powershell Jumpstart Scripting and toolmaking session which is available in MVA.

    Also I must thank you for your book on Error handling. It was very helpful and very clear to understand the error handling process and I fell much more comfortable with PS scripting.

  • #21574
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    No problem, glad it helped! 🙂

You must be logged in to reply to this topic.