PowerShell passing of variables (param)

 

At the beginning of a PowerShell script parameters can be defined in the function Param().

Param - passing variables to a script.

Variables inside "param" can be passed to the script when the script is called.

param (
[String]$var1="1",
[String]$var2="2"
)

A default value can be assigned in the param block, which is overwritten with another value when the script is called and the parameter is specified:

File types

Which file types are allowed as parameters is defined with [datatype]$variablename. A list of the possible data types can be found here: PowerShell variables and data types

If "switch" is used as data type, no values must be specified for the parameters:

CmdletBinding

With the addition [CmdletBinding()] PowerShell scripts or functions are converted into cmdlets. PowerShell attaches additional parameters to the function:

Powershell ISE provides a snippet template for this: cmdlet (advanced function), see also: Powershell ISE - Snippet

Mandatory

Variables with the addition Mandatory are mandatory for execution

Parameters with multiple values

By separating the values with a comma "," multiple variables can be passed. These can be handled in a foreach loop

function folders
{
    [CmdletBinding()]
        Param(
        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true)]
        $Folders=(
            "d:",
            "e:",
            "c:")
        )

        foreach ($Folder in $Folders){
            write-host "folder:$($Folder)"
        }
}

Call:

PS C:\temp> .\parameter.ps1
False
False
PS C:\temp>

Parameters without value: switch

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$false)]
   [switch]$Option,
	
   [Parameter(Mandatory=$false)]
   [switch]$2ndOption
)
write-host $Option
write-host $2ndOption

When called without parameters, the two optional parameters are returned with $false:

PS C:\temp> .\parameter.ps1
False
False
PS C:\temp>

if the options are specified in the call, the switch parameter is set to $true:

PS C:\temp> .\parameter.ps1 -Option
True
False
PS C:\temp>

Read the possible parameters

PS C:\temp> (get-command .\parameter.ps1).parameters

Key                 Value                                         
---                 -----                                         
Option              System.Management.Automation.ParameterMetadata
2ndOption           System.Management.Automation.ParameterMetadata
Verbose             System.Management.Automation.ParameterMetadata
Debug               System.Management.Automation.ParameterMetadata
ErrorAction         System.Management.Automation.ParameterMetadata
WarningAction       System.Management.Automation.ParameterMetadata
InformationAction   System.Management.Automation.ParameterMetadata
ErrorVariable       System.Management.Automation.ParameterMetadata
WarningVariable     System.Management.Automation.ParameterMetadata
InformationVariable System.Management.Automation.ParameterMetadata
OutVariable         System.Management.Automation.ParameterMetadata
OutBuffer           System.Management.Automation.ParameterMetadata
PipelineVariable    System.Management.Automation.ParameterMetadata
positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

created by Bernhard | published: 2022-05-02 Übersetzung Deutsch |🔔 | Comments:0

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details