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

    Powershell Script Repetitive Process

    IT Discussion
    3
    9
    125
    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.
    • Grey
      Grey last edited by

      If I have a set of tasks that I usually call as a foreach($x in $y){stuff} and I want to call that same section later in the script, can I? I don't want to have to copy/paste it several times since it just looks messy and barely elevates the ps1 from a bat file at that point.

      1 Reply Last reply Reply Quote 0
      • Danp
        Danp last edited by

        Have you looked into moving that repetitive code into a function?

        https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7

        Grey 1 Reply Last reply Reply Quote 0
        • Grey
          Grey @Danp last edited by

          @Danp said in Powershell Script Repetitive Process:

          Have you looked into moving that repetitive code into a function?

          https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7

          I did. I'm not sure if the other aspects of the script get passed in to the function. Environment settings, etc.

          Obsolesce 1 Reply Last reply Reply Quote 0
          • Obsolesce
            Obsolesce @Grey last edited by Obsolesce

            @Grey said in Powershell Script Repetitive Process:

            @Danp said in Powershell Script Repetitive Process:

            Have you looked into moving that repetitive code into a function?

            https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7

            I did. I'm not sure if the other aspects of the script get passed in to the function. Environment settings, etc.

            It depends on how you feed the function. Bring the stuff in as a parameter, then run the fuction by storing the output in a variable like $variableName = Invoke-MyFunction -Parameter1 "stuff" -Parameter2 $object or something, you get the idea... then you can have the output of the function output an input object you specify which will be $variableName that you can use.

            1 Reply Last reply Reply Quote 0
            • Obsolesce
              Obsolesce last edited by

              Here's an example:

              68d9f205-5903-416c-a32d-3a0d2a4c536c-image.png

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

                @Obsolesce said in Powershell Script Repetitive Process:

                Here's an example:

                68d9f205-5903-416c-a32d-3a0d2a4c536c-image.png

                Neat. How would I call call that function from within the script itself; just & do-function?

                Obsolesce 1 Reply Last reply Reply Quote 0
                • Obsolesce
                  Obsolesce @Grey last edited by

                  @Grey said in Powershell Script Repetitive Process:

                  @Obsolesce said in Powershell Script Repetitive Process:

                  Here's an example:

                  68d9f205-5903-416c-a32d-3a0d2a4c536c-image.png

                  Neat. How would I call call that function from within the script itself; just & do-function?

                  The same way I did on line 24.

                  Grey 1 Reply Last reply Reply Quote 0
                  • Grey
                    Grey @Obsolesce last edited by

                    @Obsolesce said in Powershell Script Repetitive Process:

                    @Grey said in Powershell Script Repetitive Process:

                    @Obsolesce said in Powershell Script Repetitive Process:

                    Here's an example:

                    68d9f205-5903-416c-a32d-3a0d2a4c536c-image.png

                    Neat. How would I call call that function from within the script itself; just & do-function?

                    The same way I did on line 24.

                    Ok, I have to test this.

                    Obsolesce 1 Reply Last reply Reply Quote 0
                    • Obsolesce
                      Obsolesce @Grey last edited by

                      @Grey said in Powershell Script Repetitive Process:

                      @Obsolesce said in Powershell Script Repetitive Process:

                      @Grey said in Powershell Script Repetitive Process:

                      @Obsolesce said in Powershell Script Repetitive Process:

                      Here's an example:

                      68d9f205-5903-416c-a32d-3a0d2a4c536c-image.png

                      Neat. How would I call call that function from within the script itself; just & do-function?

                      The same way I did on line 24.

                      Ok, I have to test this.

                      I just noticed, that on line 16, the $timeDate... variable is supposed to be $Something

                      I did it too quick and didn't catch it when I put it in at the end.

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