Functions

Quite often, as the usage of SQL-PowerShell increases, some efficiencies are gained by using functions. Functions are especially useful when you are creating things that are done on a regular basis either directly in the console or in a script.

Using the earlier example of comparing two strings, this example writes a function named test, so if future comparisons are required, the typing requirements will be greatly reduced:

PS>Function test {>> param($user1,$user2)>> if("$user1" -eq "$user2")>> {>>   Write-Host "Equals">> }>> else>> {>>   Write-Host "Not equal">> }>> }>>PS>test "userA" "userB"Not equalPS>test "userA" "userA"EqualsPS>

Note

Execute the Get-Help about_function command in PowerShell for more ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.