Why use external arguments for PowerShell scripts? External parameters allow for options to be selected that correspond to functions or actions within the script. For example, if you wanted to trigger a HTML report for output, maybe we would specify a parameter of ‘Report’.
Example 1
Run from PowerShell:
[sourcecode language=”powershell”]
Get-ExchangeHealth.ps1 Office365
[/sourcecode]
By running this
[sourcecode language=”powershell”]
$Switch1 = $args[0]
[/sourcecode]
Later in the script we can then use the parameter to make a choice:
[sourcecode language=”powershell”]
If (Switch1 -ne 'Office365) {
(Get-ExchangeServer).Name
} Else {
Write-Verbose 'Connected to Exchange Online, no Exchange Servers to query'
}
[/sourcecode]
Example 2
Run from PowerShell:
[sourcecode language=”powershell”]
Get-ExchangeHealth.ps1 Office365 MailboxStats
[/sourcecode]
Now not only do we get to connect to Office 365, but we can also query mailbox stats in the same run.
Code in the script:
[sourcecode language=”powershell”]
param(
[Parameter(Mandatory=$false,ParameterSetName="Office365")] [switch]$Office365,
[Parameter(Mandatory=$false,ParameterSetName="MailboxStats")] [switch]$MailboxStats
)
[/sourcecode]
Later in the script we would have two code blocks to handle these requests: (not a complete sample)
[sourcecode language=”powershell”]
If ($Environment -eq 'Office365') { Write-host ' ' Write-host ' *** ' -ForegroundColor White -NoNewline Write-host 'Script will examine Exchange Online now.' -ForegroundColor Red -NoNewline Write-host ' *** ' -ForegroundColor White Write-host ' ' $ExOMenu = { Write-Host " ***********************************" -ForegroundColor White Write-Host " Exchange Online Health Check Menu" -ForegroundColor Cyan Write-Host " ***********************************" -ForegroundColor White
[/sourcecode]
… and so on… Then, code for the mailbox stats options:
[sourcecode language=”powershell”]
If ($MailboxStats) {
Get-mailbox | Get-MailboxStatistics | select-object DisplayName, {$_.TotalItemSize.Value.ToMB()}
}
[/sourcecode]
Example 3
Run from PowerShell:
[sourcecode language=”powershell”]
Get-ExchangeHealth.Ps1 SecurityComplianceCenter
[/sourcecode]
Then we have this code in the script to look for this switch:
[sourcecode language=”powershell”]
$SCC = $args[0]
[/sourcecode]
Then later in the script, we can check to see if the parameter populated this variable and if so, execute some code (sample below):
[sourcecode language=”powershell”]
If ($SCC) {
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Get-RetentionComplianceTag
}
[/sourcecode]
Thus we can use parameters to have our script handle additional tasks when executing. The above examples are simplistic, but do provide an introductory view into how they can be utilized.
PowerShell Parameters (Basic)
Categories: