Created
November 7, 2017 00:58
-
-
Save rodmhgl/f5ad878a7933ce901ae5ae3ef0698efd to your computer and use it in GitHub Desktop.
Just demoing Begin, Process, End with some pseudo-code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Give-UserLicense ($User) { | |
#return $true | |
} | |
Function Add-UserLicenses { | |
param( | |
[Parameter( | |
Mandatory = $true, | |
ValueFromPipelineByPropertyName = $true, | |
ValueFromPipeline = $true | |
)][String[]]$Username | |
) | |
Begin { | |
# Create Session to O365 / Remote Exchange / Skype / SharePoint Session | |
# This may involve prompting for / loading credentials or extended time to import modules | |
Write-Verbose "Begin" | |
} | |
Process { | |
foreach ($User in $Username) { | |
try { | |
Give-UserLicense -Username $User | |
$User # Output back to pipeline | |
} catch { | |
# Log Error | |
} | |
} | |
} | |
End { | |
# Cleanup Session to O365 / Remote Exchange / Skype / SharePoint Session | |
Write-Verbose "END" | |
} | |
} | |
$SimpleArray = @('1', '2', '3', '4') | |
$ObjectArray = @( | |
[PSCustomObject] @{'Office' = 'Los Angeles'; 'Username' = 'UserOne'}, | |
[PSCustomObject] @{'Office' = 'New York'; 'Username' = 'UserTwo'} | |
) | |
Function Add-UserLicensesWithoutBlocks { | |
param( | |
[Parameter( | |
Mandatory = $true, | |
ValueFromPipelineByPropertyName = $true, | |
ValueFromPipeline = $true | |
)][String[]]$Username | |
) | |
foreach ($User in $Username) { | |
try { | |
Give-UserLicense -Username $User | |
$User # Output back to pipeline | |
} catch { | |
# Log Error | |
} | |
} | |
} | |
Write-Verbose "Calling function that does have process block" | |
Add-UserLicenses -Username $SimpleArray -Verbose | |
$SimpleArray | Add-UserLicenses -Verbose | |
$ObjectArray | Add-UserLicenses -Verbose | |
Write-Verbose "Calling function that does not have process block" | |
Add-UserLicensesWithoutBlocks -Username $SimpleArray -Verbose | |
$SimpleArray | Add-UserLicensesWithoutBlocks -Verbose | |
$ObjectArray | Add-UserLicensesWithoutBlocks -Verbose | |
#Outputs: | |
# VERBOSE: Calling function that does have process block | |
# VERBOSE: Begin <-- notice the Begin and End blocks only occur once per call | |
# 1 | |
# 2 | |
# 3 | |
# 4 | |
# VERBOSE: END | |
# VERBOSE: Begin | |
# 1 | |
# 2 | |
# 3 | |
# 4 | |
# VERBOSE: END | |
# VERBOSE: Begin | |
# UserOne | |
# UserTwo | |
# VERBOSE: END | |
# VERBOSE: Calling function that does not have process block | |
# 1 | |
# 2 | |
# 3 | |
# 4 | |
# 4 <-- In this call, it only works on the last element of the collection | |
# UserTwo <-- Same here, it only works on the last element of the collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment