Advertisement
cwprogram

Closure Param Passing Notes

Jul 11th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Test-TcpPortConnection {
  2.     # Parameters to pass in when setting up the closure for return
  3.     param($hostname,$port)
  4.     # Closure Block
  5.     {
  6.         # Parameter the user can pass in when invoking the closure
  7.         param($something)
  8.         Write-Host $something
  9.         try {
  10.             $tcp = New-Object System.Net.Sockets.TcpClient
  11.             $tcp.Connect($hostname,$port)
  12.             $tcp.Close()
  13.            
  14.             return New-Object PSObject -Property @{
  15.                 Pass=$true;
  16.             }
  17.         }
  18.         catch [System.Net.Sockets.SocketException] {
  19.             return New-Object PSObject -Property @{
  20.                 Pass=$false;
  21.                 Exception=$_;
  22.                 ExceptionType="Socket";
  23.             }        
  24.         }    
  25.         catch {
  26.             return New-Object PSObject -Property @{
  27.                 Pass=$false;
  28.                 Exception=$_;
  29.                 ExceptionType="General";
  30.             }    
  31.         }
  32.     }.GetNewClosure()
  33. }
  34.  
  35. $test = (Test-TcpPortConnection "www.google.com" 80)
  36. $result = & $test "Closure Parameter"
  37. if($result.Pass) {
  38.     Write-Host 'Success'
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement