Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Need to check a Socket but with a source IP

$
0
0

This should do the job:

# Running this script on Computer A
    $Account = "domain\user-with-eoungh-permissions-on-ComputerB"
    $ComputerB = "ComputerB_Name" # Source computer where we initiate the ip/port check
    $ComputerC = "10.224.34.1" # Target computer that we're checking ip/port on
    $Port = 636 # Target port
#    $ComputerC = "google.com" 
#    $Port = 80
    #
    if (!(Test-Path -Path ".\Cred.txt")) {
        Write-Output "Error: missing encrypted pwd file .\Cred.txt, enter the pwd to be encrypted and saved to .\Cred.txt for future script use:" 
        Read-Host 'Enter the pwd to be encrypted and saved to .\Cred.txt for future script use:' -AsSecureString | ConvertFrom-SecureString | Out-File .\Cred.txt
    }
    $Pwd = Get-Content .\Cred.txt | ConvertTo-SecureString 
    $Cred = New-Object System.Management.Automation.PSCredential($Account,$Pwd)
    $ComputerCdata = Invoke-Command -ComputerName $ComputerB -Credential $Cred -ScriptBlock { param($ComputerC,$Port) 
        $socketC = New-object Net.Sockets.TcpClient
        $socketC.Connect($ComputerC,$Port) 
        $data = $socketC.Connected
        return $data
    } -ArgumentList $ComputerC,$Port
    if ($ComputerCdata) {Write-Output "Computer $ComputerC socket $Port connected successfully from computer $ComputerB"}
        else {Write-Output "Failed to connect to Computer $ComputerC socket $Port from computer $ComputerB"}

Viewing all articles
Browse latest Browse all 13067

Trending Articles