- 1 year ago
Powershell calling a function if script has mandatory parameters
Comment
1 / 1
In PowerShell, you can create a function that has mandatory parameters and then call another function if the mandatory parameters are provided. Here's an example:
powershellfunction MyFunction {
param(
[Parameter(Mandatory=$true)]
[string]$Param1,
[Parameter(Mandatory=$true)]
[string]$Param2
)
Write-Host "Param1: $Param1"
Write-Host "Param2: $Param2"
}
function MyCallerFunction {
param(
[Parameter(Mandatory=$true)]
[string]$Param1,
[Parameter(Mandatory=$true)]
[string]$Param2
)
MyFunction -Param1 $Param1 -Param2 $Param2
}
MyCallerFunction -Param1 "Value1" -Param2 "Value2"
In this example, we have two functions: MyFunction
and MyCallerFunction
. Both functions have mandatory parameters, and MyCallerFunction
calls MyFunction
with the provided parameters.
When you call MyCallerFunction
with the required parameters, it will invoke MyFunction
with those parameters. If you don't provide the required parameters, PowerShell will prompt you to provide the missing parameters.
You can modify the parameter names, types, and the logic within the functions to suit your specific use case.