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

    Powershell Script Repetitive Process

    IT Discussion
    3
    9
    309
    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.
    • GreyG
      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
      • DanpD
        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

        GreyG 1 Reply Last reply Reply Quote 0
        • GreyG
          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.

          ObsolesceO 1 Reply Last reply Reply Quote 0
          • ObsolesceO
            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
            • ObsolesceO
              Obsolesce
              last edited by

              Here's an example:

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

              GreyG 1 Reply Last reply Reply Quote 2
              • GreyG
                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?

                ObsolesceO 1 Reply Last reply Reply Quote 0
                • ObsolesceO
                  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.

                  GreyG 1 Reply Last reply Reply Quote 0
                  • GreyG
                    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.

                    ObsolesceO 1 Reply Last reply Reply Quote 0
                    • ObsolesceO
                      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
                      • 1 / 1
                      • First post
                        Last post