Powershell Script Repetitive Process
-
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.
-
Have you looked into moving that repetitive code into a function?
-
@Danp said in Powershell Script Repetitive Process:
Have you looked into moving that repetitive code into a function?
I did. I'm not sure if the other aspects of the script get passed in to the function. Environment settings, etc.
-
@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?
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. -
Here's an example:
-
@Obsolesce said in Powershell Script Repetitive Process:
Here's an example:
Neat. How would I call call that function from within the script itself; just & do-function?
-
@Grey said in Powershell Script Repetitive Process:
@Obsolesce said in Powershell Script Repetitive Process:
Here's an example:
Neat. How would I call call that function from within the script itself; just & do-function?
The same way I did on line 24.
-
@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:
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.
-
@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:
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.