If you do a break in the outer loop (or mainline) of a process{} block, you will exit the process block.
Using a named break or continue is a good solution with low overhead. From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On Behalf Of Webster Sent: Tuesday, November 15, 2016 5:42 PM To: powershell@lists.myitforum.com Subject: [powershell] RE: Breaking Process { } Block: Here is something MBS showed me a little while ago. See if it might give you an idea. Function GetBadStreamingIPAddresses { Param([string]$ComputerName) #loop through the configured streaming ip address and compare to the physical configured ip addresses #if a streaming ip address is not in the list of physical ip addresses, it is a bad streaming ip address ForEach ($Stream in ($Script:StreamingIPAddresses | Where {$_.Servername -eq $ComputerName})) { $exists = $false :outerLoop ForEach ($ServerNIC in $Script:NICIPAddresses.Item($ComputerName)) { ForEach ($IP in $ServerNIC) { # there could be more than one IP If ($Stream.IPAddress -eq $IP) { $Exists = $true break :outerLoop } } } if (!$exists) { $obj1 = New-Object -TypeName PSObject $obj1 | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName $obj1 | Add-Member -MemberType NoteProperty -Name IPAddress -Value $Stream.IPAddress $Script:BadIPs += $obj1 } } } Thanks Carl Webster Citrix Technology Professional http://www.CarlWebster.com<http://t.sidekickopen01.com/e1t/c/5/f18dQhb0S7lC8dDMPbW2n0x6l2B9nMJN7t5XYgdV8QRW2zWLDn4XrdjzW7fK3rs56dwxZf67wwsR02?t=http%3A%2F%2Fwww.carlwebster.com%2F&si=6012126861197312&pi=4311b7b1-332d-4242-8585-36954b184dc7> The Accidental Citrix Admin From: listsad...@lists.myitforum.com<mailto:listsad...@lists.myitforum.com> [mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey Sent: Tuesday, November 15, 2016 4:30 PM To: powershell@lists.myitforum.com<mailto:powershell@lists.myitforum.com> Subject: [powershell] Breaking Process { } Block: I am doing some testing and not sure the best way to accomplish the desired outcome. I have a script with several functions. One advanced function will take a CSV file and generate AD users. I have a sub-function to validate names: function Validate-Name ([string]$field) { if ($field -notmatch "[^\p{L}^\p{Pd}\s/']") { return $false } else { return $true } } During the Process{} block, I'd like to attempt to validate the first/last name and bail out for just the one user and continue attempting for any subsequent entries in the CSV file. My main question is there a best practice, or agreed general guideline for handling this? Do you wrap everything in an If statement before proceeding? Or do you have a Break/Return/Exit within the process block? I thought Exit/Return/Break end the entire function, but reading up I'm not sure if using Break to exit the Process{} block would also exit the parent function. I'm interested in how others handle this. I'd like to be able to break out of processing that one row, then continue processing the CSV file: Process { If (Validate-Name $FirstName) { # Log failure (TBD) Break } } Process { If (Validate-Name $FirstName) { # Log failure (TBD) Exit } } Process { If (Validate-Name $FirstName) { $failed = $true } If ($failed) { # Log failure (TBD) } Else { #Process user Add } } Confidentiality Notice: This is a transmission from Community Hospital of the Monterey Peninsula. This message and any attached documents may be confidential and contain information protected by state and federal medical privacy statutes. They are intended only for the use of the addressee. If you are not the intended recipient, any disclosure, copying, or distribution of this information is strictly prohibited. If you received this transmission in error, please accept our apologies and notify the sender. Thank you.