Help to learn how to start writing PowerShell tools

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Ronnie Jorgensen Ronnie Jorgensen 5 months ago.

  • Author
    Posts
  • #45997
    Profile photo of Ronnie Jorgensen
    Ronnie Jorgensen
    Participant

    Hi all,

    New member here in this forum but would like to take the opportunity to ask for some help/learning. More and more often now I am starting to want to share my scripts with colleagues at work with less experience in PowerShell and it got me thinking that normally my scripts are run from a PowerShell console like this: .\Run-Script.ps1 -Parameter1 "something" -Parameter2 "funny" or just like .\Run-Script.ps1 and I will use Read-Host to ask the user for an input.

    But i am sort of feeling this is a very crude way of doing it and there must be a much better way for me to learn. The problem is i am not entirely sure what that method / better way is. Hoping someone in the forum can help me out, guiding me to some learning material.

    One example i have is that recently i wrote a script that if not parameters was specified it would use Read-Host for various inputs. Then my script would validate that data ensuring the correct format of data plus it would check against Active Directory if a new group object was already present. Obviously if input data is in wrong format or if an Active Directory group is already present or the wanted email address already used it would stop the script.
    Once everything was checked and validated then we create the new Active Directory Group.

    But what i started to think about is that my script was obviously made so a user could run this and create 1 group. i never took into account if i needed to create 10 groups. Could I change the script perhaps so that the user could choose between creating 1 group or create multiple. In case of multiple would that be input via CSV file or would i simple ask for how many groups to be created and then keep adding user input via Read-Host into an array so later on in the script i can run through them one by one.

    So that is my problem really. What is it I need to learn to move to the next level in PowerShell scripting. I keep seeing people talking about functions and make re-usable code but then i am not quite sure how this fits together. For example a function to create a new Active Directory group. but again i would like that to take 1 group to be created or multiple. so how do I get my input into the function and regardless if only 1 or 10 groups it will keep go through them one by one.

    Thank you in advance for reading this and hoping someone can help a relative newbie to become a bit better in PowerShell

  • #46001
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Would you consider "Learn PowerShell Toolmaking in a Month of Lunches?" It's mission is pretty much precisely what you're asking ;).

    • #46015
      Profile photo of Ronnie Jorgensen
      Ronnie Jorgensen
      Participant

      Hi Don,

      Funny enough i am just sat looking at it on Amazon with a view to buy it 🙂 why reinvent the wheel by telling me what already is out there on the internet 🙂 and even better yet, written by yourself 🙂

      Now i have a follow up question that i am really dying to find out what other more experienced PowerShell scripters are doing.

      So earlier today i came across a script that said it needed another script as a dependency. now the script called this script like this:

      # Requires the "Get-Link script":http://poshcode.org/2493
      $lnk = Get-Link $Path
      

      And I thought to myself, how on earth am i going to do this. because all i had was a script named Install-Solarized.ps1 and i was guessing it had to be run like .\Install-Solarized but then how did the command above

      $lnk = Get-Link $Path

      work?

      All i know of is either i can dot source a ps1 file in my PowerShell profile so i can use functions from that sourced ps1 file. or i could try to dot source that Get-Link.ps1 file inside the first script.

      Basically i was lost and had to contact the author that sold me to add the script folder to the $Env:Path so that i could call the second script like $lnk = Get-Link $Path.

      But now obviously i am interested in what do the experts do? I am thinking there is a few options, some better than others perhaps?

      So my question is, how does the more experienced PowerShell scripters set up their configuration in the PowerShell console

      – . source files in their profile to use functions?
      – . source files in their scripts to use functions?
      – Adding script folder path to the environment PATH in the profile
      – Adding script folder path to the environment PATH in the script itself?

      I guess in the end what i want to learn is how do i make life easier for myself? I came across this script https://jkeohan.wordpress.com/2012/02/10/powershell-adding-directories-to-path-statement/ to add to the environment PATH but then i started thinking about why make this a function? was he going to make it availble in the PS console and call at will when needed? would he be going running the script manually from PS console or ISE? what is it i am not seeing basically

      Again thank for the advice 🙂

  • #46003
    Profile photo of Trevor Freedland
    Trevor Freedland
    Participant

    I second the "Learn PowerShell Toolmaking in a Month of Lunches" recommendation and also suggest the Microsoft Virtual Academy course: 'Advanced Tools & Scripting with PowerShell 3.0 Jump Start'.

  • #46017
    Profile photo of Ronnie Jorgensen
    Ronnie Jorgensen
    Participant

    Hi Don & Trevor

    Thank you for that, I am just looking at that book on Amazon and will get that shipped to me. Will definitely also watch that MVA video. thank you Trevor.

    Now can i ask another follow up question in this "thread" or is a new one better? sort of related in the fact to how i learn and evolve into a better PS scripter.

  • #46019
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Ask away.

  • #46023
    Profile photo of Ronnie Jorgensen
    Ronnie Jorgensen
    Participant

You must be logged in to reply to this topic.