Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Test-TcpPortConnection {
- # Parameters to pass in when setting up the closure for return
- param($hostname,$port)
- # Closure Block
- {
- # Parameter the user can pass in when invoking the closure
- param($something)
- Write-Host $something
- try {
- $tcp = New-Object System.Net.Sockets.TcpClient
- $tcp.Connect($hostname,$port)
- $tcp.Close()
- return New-Object PSObject -Property @{
- Pass=$true;
- }
- }
- catch [System.Net.Sockets.SocketException] {
- return New-Object PSObject -Property @{
- Pass=$false;
- Exception=$_;
- ExceptionType="Socket";
- }
- }
- catch {
- return New-Object PSObject -Property @{
- Pass=$false;
- Exception=$_;
- ExceptionType="General";
- }
- }
- }.GetNewClosure()
- }
- $test = (Test-TcpPortConnection "www.google.com" 80)
- $result = & $test "Closure Parameter"
- if($result.Pass) {
- Write-Host 'Success'
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement