Krausmueller.de

PowerShell Scripts with WhatIf

Sometimes you want to simulate the execution of a PowerShell script. Instead of actually doing the things defined in the script you just want to see what would have been done. Many Cmdlets understand the parameter -WhatIf. If you use it e.g. when creating a directory it actually will not be created. Instead you get a message describing what would have been done:

1
New-Item -ItemType Directory -Name "test" -Path "c:\temp" -WhatIf
PS C:\> New-Item -ItemType Directory -Name "test" -Path "c:\temp" -WhatIf
What if: Performing the operation "Create Directory" on target "Destination: C:\temp\test".

Read more →

Passwords in PowerShell

Sometimes you have to use passwords in PowerShell. For example if you want to shut down a remote computer and have to authorize with another user than the current. You can prompt for the username and password while running the script with Get-Credential:

12
$Credential = Get-Credential
Stop-Computer -Computer 192.168.2.10 -Credential $Credential -WhatIf

Read more →

Validate PowerShell parameters

When you use parameters in your PowerShell script you might want to check the values for valid data. Of course you can do these checks with some if statements. But you can also specify valid values when defining the parameters.

 1 2 3 4 5 6 7 8 91011121314151617181920212223242526272829303132
# definition of parameters
Param(
    [Parameter(Mandatory=$True)]
    [string]$StringParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateSet("String1","String2")]
    [string]$StringParameter2,

    [Parameter(Mandatory=$True)]
    [int]$IntegerParameter1,

    [Parameter(Mandatory=$True)]
    [ValidateRange(0, 5)]
    [int]$IntegerParameter2
)

# own validation of parameters
if (-not($StringParameter1 -eq "String1" -or $StringParameter1 -eq "String2")) {
    Write-Output "Error! Valid values for StringParameter1 are ""String1"" and ""String2""."
    exit
}
if ($IntegerParameter1 -lt 0 -or $IntegerParameter1 -gt 5) {
    Write-Output "Error! Value of IntegerParameter1 must be in range from 0 to 5."
    exit
}

# output of the parameter values
Write-Output "StringParameter1: $StringParameter1"
Write-Output "StringParameter2: $StringParameter2"
Write-Output "IntegerParameter1: $IntegerParameter1"
Write-Output "IntegerParameter2: $IntegerParameter2"

Read more →

Add parameters to PowerShell Script

If you want to add parameters to your PowerShell Script you have to define them. In this defintion you also specify the data type. A simple example to get the idea:

 1 2 3 4 5 6 7 8 910111213
# definition of parameters
Param(
    [string]$StringParameter,
    [bool]$BooleanParameter = $False,
    [int]$IntegerParameter = 10,
    [switch]$SwitchParameter
)

# output of the parameter values
Write-host "StringParameter: $StringParameter"
Write-host "BooleanParameter: $BooleanParameter"
Write-host "IntegerParameter: $IntegerParameter"
Write-host "SwitchParameter: $SwitchParameter"

Read more →