ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Unsolved Need a good example of getting powershell arguments

    IT Discussion
    powershell scripting parameters
    3
    3
    679
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by

      I'll hit the google later, because I am on other things, but I found that something I touched today could very easily be improved if I can add parameter handling to the powershell script.

      Now, the basics are easy as it is all in the $ARGS variable/object.

      But I want to have some safety checking. because it is easier to do things right the first time.

      Example: I want a parameter to note if I should make the thing being done the default.

      I can pass a 1 like dothing.ps1 1 and I can simply code something to check $ARG[0] eq "1" but that is not very explanatory to the person using the script.

      This is more explanatory dothing.ps1 -default for a command.

      So has anyone seen a good example of parameter handling that I can put into my dothing.ps1 script?

      ObsolesceO 1 Reply Last reply Reply Quote 1
      • dafyreD
        dafyre
        last edited by dafyre

        Something like this near the top of your script:

        
        param(
        [Parameter(Mandatory=$false)] [string] $adGroup = "Users",
        [Parameter(Mandatory=$false)] [string] $adServer = "MyDomain.Local",
        [Parameter(Mandatory=$false)] [switch] $noTimeStamp, #False if not provided, true if it is
        [Parameter(Mandatory=$true)] [pscredential] $creds=$null
        )
        
        

        For instance, in the snippet above, $adGroup has a default value of "Users" ... and $adServer is "mydomain.local"

        If you want the parameter to be required, set Mandatory = $true (last item above).

        Is that what you're looking for?

        Example scripts:

        https://gitlab.com/dafyre/powershell-utils/raw/master/ActiveDirectory/export-ADGroup.ps1 (a variation of the above snippet comes from here)

        https://gitlab.com/dafyre/powershell-utils/raw/master/Office365/O365-MailTrafficReports.ps1 -- a more complicated example, but a good one anyhow.

        1 Reply Last reply Reply Quote 2
        • ObsolesceO
          Obsolesce @JaredBusch
          last edited by Obsolesce

          @JaredBusch said in Need a good example of getting powershell arguments:

          I'll hit the google later, because I am on other things, but I found that something I touched today could very easily be improved if I can add parameter handling to the powershell script.

          Now, the basics are easy as it is all in the $ARGS variable/object.

          But I want to have some safety checking. because it is easier to do things right the first time.

          Example: I want a parameter to note if I should make the thing being done the default.

          I can pass a 1 like dothing.ps1 1 and I can simply code something to check $ARG[0] eq "1" but that is not very explanatory to the person using the script.

          This is more explanatory dothing.ps1 -default for a command.

          So has anyone seen a good example of parameter handling that I can put into my dothing.ps1 script?

          I'm not sure I understand exactly what you mean.

          Taking a guess here, but how I understand is that you'd want to add this at the top of your script:

          [cmdletbinding()]
          param (
                  [Parameter()]
                  [Switch]$Default
              )
          
          if ($Default) {
              Write-Host "The -Default parameter was specified."
          } else {
              Write-Host "The -Default parameter was NOT specified."
          }
          

          Doing that will give you the following output:

          PS > .\JBTest.ps1 -Default
          The -Default parameter was specified.
          PS > .\JBTest.ps1
          The -Default parameter was NOT specified.
          

          If you want to accept input from a pipeline to work with, let me know.

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post