How do I get PowerShell 4 cmdlets such as Test-NetConnection to work on Windows 7?

后端 未结 7 803
刺人心
刺人心 2021-02-07 05:27

The situation. On a Windows 7 SP1 machine, I have updated with Windows6.1-KB2819745-x64-MultiPkg.msu. Furthermore, in PowerShell $PSVersionTable now reports ‘PSVersion 4.0’.

7条回答
  •  野的像风
    2021-02-07 06:03

    I see several responses which assert portability, and my testing confirms their assertions:

    Import all of the required modules, either from file, or via a PSSession to a host which has the required modules. The architecture of PowerShell Console (x86 or x64) you run will determine which module architecture you import.

    For those who are:

    • still unable to make this work AND
    • do need a reliable TCP test, but may not need everything else provided by Test-NetConnection AND
    • Need it all to work even on PowerShell v2.0

    You may wish to try this.

    # Create a TCP Client using .Net & attempt connection to target
    $TCPClient = New-Object .net.sockets.tcpclient("[IP address or hostname]",[port])
    # At the above point you may see familiar-looking Windows error messages in
    # red text. (You may want to use "try/catch" if you intend to loop).
    # Otherwise, check your new object's status to see if it worked
    $TCPClient.Connected
    # The response is either "True" or False"
    # Now to avoid leaving idle connections, run:
    $TCPClient.Close()
    

    Naturally, it should be possible to create a loop which tests multiple connections, and outputs the results by selecting the properties of the $TCPClient. My initial testing shows you would want to Select these properties

    The address you tested

    $TCPClient.Client.RemoteEndPoint.Address.IPAddressToString
    

    The port you tested

    $TCPClient.Client.RemoteEndPoint.Port
    

    The result

    $TCPClient.Connected
    

    HIH

提交回复
热议问题