SOLVED

Working with functions

%3CLINGO-SUB%20id%3D%22lingo-sub-3040566%22%20slang%3D%22en-US%22%3EWorking%20with%20functions%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3040566%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20script%20that%20has%20three%20processes%20and%20I%20want%20to%20create%20a%20function%20for%20each%20process%20and%20then%20from%20the%20same%20script%20call%20each%20function.%20Is%20that%20possible%3F%20How%20do%20I%20do%20it%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24computers%3D%40('a'%2C'b'%2C'c'%2C'd'%2C'e')%0A%0AForEach%20(%24pc%20in%20%24computers)%0A%7B%0AClossingApps%20-ComputerName%20%24pc%0ARemovingApps%20-ComputerName%20%24pc%0AInstallingApps%20-ComputerName%20%24pc%0A%7D%0A%0Afunction%20ClossingApps%20%7B%0Aparam%20(%0A%24ComputerName%0A)%0A%0AWrite-Output%20%22Closing%22%0A%7D%0A%0Afunction%20RemovingApps%20%7B%0Aparam%20(%0A%24ComputerName%0A)%0AWrite-Output%20%22Removing%22%0A%7D%0A%0Afunction%20InstallingApps%20%7B%0Aparam%20(%0A%24ComputerName%0A)%0AWrite-Output%20%22Installing%22%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3040566%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Contributor

Hello everyone!

 

I have a script that has three processes and I want to create a function for each process and then from the same script call each function. Is that possible? How do I do it?

 

Example:

 

$computers=@('a','b','c','d','e')

ForEach ($pc in $computers)
{
ClossingApps -ComputerName $pc
RemovingApps -ComputerName $pc
InstallingApps -ComputerName $pc
}

function ClossingApps {
param (
$ComputerName
)

Write-Output "Closing"
}

function RemovingApps {
param (
$ComputerName
)
Write-Output "Removing"
}

function InstallingApps {
param (
$ComputerName
)
Write-Output "Installing"
}

 

Thank you

1 Reply
best response confirmed by ADumith (Contributor)
Solution

Hi @ADumith 

 

don't forget to call the functions, because RATM, you just defined them.

https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/09-functions?view=powershell-7.2

 

function Start-Calculator{
start calculator.exe
}
start-calulator #The actual function start

Best regards Schnittlauch