I was asked today about the Begin{} Process{} End{} blocks and why was Foreach needed inside the Process block. I have seen it with and without but to be honest I could not give a good answer to the question.
Example 1 -
Function Get-ServerStuff
[CmdletBinding()]
param(
[Parameter(Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullorEmpty()]
[string[]]$ComputerName = $env:COMPUTERNAME,
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty
)
BEGIN {
$Date = (get-date).ToString('MM-dd-yy-HHmm')
$Opt = New-CimSessionOption -Protocol Dcom
$CS = $null
$SessionParams = @{
ErrorAction = 'Stop'
}
If ($PSBoundParameters['Credential']) {
$SessionParams.Credential = $Credential
}
}
PROCESS {
$ServerInfoReport = @()
ForEach ($Computer in $ComputerName){
# Checking connection to Server If cannot connect drop this server and go to the next one.
Try {
$Check1 = Test-Connection $Computer -Count 1 -ErrorAction Stop | Select-Object Address,IPV4Address
Write-Host 'Test-Connection successful for Server ' $Check1.Address $Check1.IPV4Address -ForegroundColor Yellow
"`r`n"
} # End Try
Catch [System.Net.NetworkInformation.PingException]{
$ErrorMessage = $Computer + ' [CATCH] ' + $Check1.IPV4Address + ' ' + $_.Exception.Message
"`r`n"
Write-Warning -Message $ErrorMessage
$ErrorMessage | Out-File "C:\Temp\ServerInfo_Errors $Date.txt" -Append
#Write-Warning -Message "[CATCH] Unable to connect to $Computer. Verify $Computer is online and try again."
"`r`n"
Start-Sleep -s 5
Continue
} # End Catch
Example 2 - Where I am piping extension numbers from a CSV file…
Import-CSV C:\Users\tbolton\Temp\ProvRange.csv | Get-ExtensionCSV
Function Get-ExtensionCSV {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$true)]
[string]$Extension
)
Begin {
$CSU=Get-CSuser -Filter {LineURI -ne $Null} | Select DisplayName,LineURI
$GU=Get-User -ResultSize Unlimited -Filter {Phone -ne $Null} | Select DisplayName,Phone
$GetOTP=Get-User -ResultSize Unlimited -Filter {OtherTelePhone -ne $Null} | Select DisplayName,OtherTelePhone
$GetOF=Get-User -ResultSize Unlimited -Filter {OtherFax -ne $Null} | Select DisplayName,OtherFax
$GetC=Get-Contact -Filter {Phone -ne $Null} | Select DisplayName,Phone
$GetExC=Get-CsExUmContact -Filter {LineURI -ne $Null} | Select DisplayName,DisplayNumber
$GUM=Get-UMMailbox -ResultSize Unlimited -Filter {EmailAddresses -ne $Null} | Select DisplayName,EmailAddresses
$GRG=Get-CsRgsWorkflow | Select Name,LineURI
$GetA=Get-csanalogdevice -Filter {LineURI -ne $Null} | Select DisplayName,LineURI
$GetCO=Get-CsCommonAreaPhone -Filter {LineURI -ne $Null} | Select DisplayName,LineURI
$GetCf=Get-CsDialInConferencingAccessNumber -Filter {LineURI -ne $Null} | Select DisplayName,LineURI
}
Process {
$CSUser = $CSU | ?{$_.LineURI -like "*$Extension*"} # Lync
$GetUser = $GU | ?{$_.Phone -like "*$Extension*"} # Active Directoy
$GetOtherTelePhone = $GetOTP | ?{$_.OtherTelePhone -Like "*$Extension*"} # Active Directoy '*$Extension*'"
$GetOtherFax = $GetOF | ?{$_.OtherFax -Like "$Extension*"} # Active Directoy '*$Extension*'"
$GetContact = $GetC | ?{$_.Phone -Like "*$Extension*"}
$GetExUmContact = $GetExC | ?{$_.LineURI -Like "*$Extension"}
$GetUM = $GUM | ?{$_.EmailAddresses -Like "EUM:$Extension*"} # Exchange
$GetRG = $GRG | ?{$_.LineURI -like "*$Extension"}
$GetAnalog = $GetA | ?{$_.LineURI -like "*$Extension"} # GGet-CsAnalogDevice -Filter {LineUri -like "tel:+1425555*"}
$GetCommon = $GetCO | ?{$_.LineURI -like "*$Extension"} # Get-CsCommonAreaPhone -Filter {LineUri -eq "tel:+14255551234"}
$GetConf = $GetCF | ?{$_.LineURI -like "*$Extension"} # Get-CsDialInConferencingAccessNumber -Filter {LineUri -like "tel:+1*$Extension"}
$NotFound = $Extension + " - N/A"
# Creating new Objects
$obj = New-Object -TypeName PSObject
# If statments - If found will show information, if not will show Extension N/A.
If ($CSUser -ne $null)
{
$obj | Add-Member -MemberType NoteProperty -Name "Lync URI" -Value ($CSUser.DisplayName)
} Else {
$obj | Add-Member -MemberType NoteProperty -Name "Lync URI" -Value ($NotFound)
}
Etc…
What is the rule as to when Foreach is needed? I was under the impression that Process {} acted like Foreach…?