What should be in all scripts? 'Best practices'?

This topic contains 6 replies, has 4 voices, and was last updated by  Olaf Soyk 8 months, 4 weeks ago.

  • Author
    Posts
  • #62227

    Charlie Spencer
    Participant

    I'm grass green and looking for suggestions for what kind of stuff people think should be in every script. I'm talking about 'minimum functionality', 'best practice', 'fit and finish' kind of things, the stuff you include in almost every script you write, or that you'd put in a template as the basis for starting new scripts.

    For example, the two lines below will accomplish what I need – ask the user for his credentials, start IE, and use the creds when opening OWA. The intent is to put this on factory floor computers so a user won't have to log into his AD account to check his mail.

    $Credential = Get-Credential
    Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -ArgumentList https:\\mail.ansaldo-sts.us -Credential $Credential
    

    But it lacks a lot of niceties.

    – When I want to start the script, I have to right-click it and select 'Run with PowerShell'. Short of changing the file association for .PS1, is there a better way to start a script?
    – When the script starts, the PS window is visible. Is there a way to run the script with this window hidden?
    – Say the user hits the 'Cancel' button in the Get-Credential window. That cancels the user input, but the Start-Process line tries to execute anyway. Obviously, it fails since the $Credential variable has no value. Is there a way to terminate when Get-Credential is canceled by the user? If I use an IF statement, how do I specify a null value?

    Are there basic preliminary pre-processing cmdlets, commands, or steps you consider 'best practices'? Thanks for any constructive feedback!

  • #62286

    Olaf Soyk
    Participant

    I'm grass green and looking for suggestions for what kind of stuff people think should be in every script. I'm talking about 'minimum functionality', 'best practice', 'fit and finish' kind of things, the stuff you include in almost every script you write, or that you'd put in a template as the basis for starting new scripts.

    -> Comment Based Help!!! parameters

    For example, the two lines below will accomplish what I need – ask the user for his credentials, start IE, and use the creds when opening OWA. The intent is to put this on factory floor computers so a user won't have to log into his AD account to check his mail.

    -> IMHO that's not what Powershell is made for

    – When I want to start the script, I have to right-click it and select 'Run with PowerShell'. Short of changing the file association for .PS1, is there a better way to start a script?

    -> NO! That's feature by design ... a security feature. If you need anyway you can create a shortcut and use command line arguments

    – When the script starts, the PS window is visible. Is there a way to run the script with this window hidden?

    -> Take a look at the help (command line arguments) Powershell.exe /?

    – Say the user hits the 'Cancel' button in the Get-Credential window. That cancels the user input, but the Start-Process line tries to execute anyway. Obviously, it fails since the $Credential variable has no value. Is there a way to terminate when Get-Credential is canceled by the user? If I use an IF statement, how do I specify a null value?

    -> Avoid user interaction. 😉 If you wnat to do anyway you could use loops to "re-prompt" if the result wasn't the expected. .. or mandatory parameters

    Are there basic preliminary pre-processing cmdlets, commands, or steps you consider 'best practices'? Thanks for any constructive feedback!

    -> That's pretty clear – It depends. 🙂 ... on the task you have to accomplish, on the environment, on the skill of the user using the scripts, on your preferences ....

    • #62430

      Charlie Spencer
      Participant

      I'm interested in why you think this isn't what PowerShell is for. I've been under the impression that not only can it do everything I used to do with command prompt batch files, but that it's also the preferred tool. I'd also like to know why you think I should avoid user interaction. PS has tools for prompting for responses; why not use them? Without interaction, wouldn't I have to hard code the values for variables each time a script is run?

    • #62482

      Olaf Soyk
      Participant

      I thought you're asking about personal opinions and that's mine. 😉 I consider myself more as a server administrator than an end user support guy. And I used to think that if there's a chance to fail it will fail – especially when the interaction is with inexperienced users. I consider end user interaction as SPOF. My "server collegues" can deal with text interfaces. But when I'm asked to do more than just a confirmation dialog box I usually recommend to ask someone with programming skills.

  • #62418

    Mark Prior
    Participant

    – Say the user hits the 'Cancel' button in the Get-Credential window. That cancels the user input, but the Start-Process line tries to execute anyway. Obviously, it fails since the $Credential variable has no value. Is there a way to terminate when Get-Credential is canceled by the user? If I use an IF statement, how do I specify a null value?

    $Credential = Get-Credential
    
    ###Checks if $credential holds value i.e not null###
    if ($Credential){
    
    Start-Process -FilePath "C:\Program Files\Internet Explorer\iexplore.exe" -ArgumentList https:\\mail.ansaldo-sts.us -Credential
    
    }
    
    Else {
    
    Exit
    
    }
    
    

    Agree with Olaf, not really what PS if for, however i also think powershell should be used to develop bespoke solutions that cater to the need, if this is your need then .... 🙂

    • #62433

      Charlie Spencer
      Participant

      Thanks. I didn't know 'IF ($variable_name)' would check for the presence of a value.

      I'm interested in why you think this isn't what PowerShell is for. I've been under the impression that not only can it do everything I used to do with command prompt batch files, but that it's also the preferred tool.

  • #62436

    Ron
    Participant

    I get an error if I hit cancel, so Try/Catch should work.

    If you ignore the error, the value of variable is not changed, so it may not be null. It's whatever it was before the assignment.

You must be logged in to reply to this topic.