Network in PowerShell
Introduction
This article is about working with the network in PowerShell
You can study the topics related to networking in Windows
here
General information about networks and protocols can be found in the article
«Computer networks»
Open ports
Get-NetTcpConnection
Ubuntu installation
Only the ports that are listening at the moment
Get-NetTcpConnection -State Listen
Ubuntu installation
Check whether a port is open
You can check whether a particular port on a remote host is open with Test-NetConnection
An example with port 8080 open for Jenkins
Test-NetConnection 10.30.200.116 -port 8080
ComputerName : 10.30.200.116 RemoteAddress : 10.30.200.116 RemotePort : 8080 InterfaceAlias : Ethernet 6 SourceAddress : 10.30.200.115 TcpTestSucceeded : True
If the port is closed
Test-NetConnection 10.30.200.116 -port 8082
WARNING: TCP connect to (10.30.200.116 : 8082) failed
ComputerName : 10.30.200.116 RemoteAddress : 10.30.200.116 RemotePort : 8082 InterfaceAlias : Ethernet 6 SourceAddress : 10.30.200.115 PingSucceeded : True PingReplyDetails (RTT) : 5 ms TcpTestSucceeded : False
Open a port
To open port 22 for SSH access, run
New-NetFirewallRule -DisplayName "Allow SSH" -Profile "Private" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 22
Ubuntu installation
Get network information
The closest analogue of ipconfig from the regular cmd is Get-NetIPConfiguration
PS C:\Users\Andrei> Get-NetIPConfiguration
InterfaceAlias : vEthernet (Default Switch) InterfaceIndex : 33 InterfaceDescription : Hyper-V Virtual Ethernet Adapter IPv4Address : 172.24.128.1 IPv6DefaultGateway : IPv4DefaultGateway : DNSServer : fec0:0:0:ffff::1 fec0:0:0:ffff::2 fec0:0:0:ffff::3 InterfaceAlias : WiFi InterfaceIndex : 8 InterfaceDescription : Intel(R) Dual Band Wireless-AC 8265 NetProfile.Name : Lester2.4G IPv4Address : 192.168.0.105 IPv4DefaultGateway : 192.168.0.1 DNSServer : 192.168.0.1 InterfaceAlias : Bluetooth Network Connection InterfaceIndex : 11 InterfaceDescription : Bluetooth Device (Personal Area Network) NetAdapter.Status : Disconnected InterfaceAlias : Local Area Connection InterfaceIndex : 13 InterfaceDescription : TAP-ProtonVPN Windows Adapter V9 NetAdapter.Status : Disconnected InterfaceAlias : Ethernet InterfaceIndex : 4 InterfaceDescription : Intel(R) Ethernet Connection (4) I219-V NetAdapter.Status : Disconnected
Allow RDP connections
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0
Allow the connection in the Firewall
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
OpenSSH server status
Check the server status
Get-Service sshd
Ubuntu installation
Start the OpenSSH server
Start-Service sshd
Get-Service sshd
Ubuntu installation
hostname
You can find out the host name with the command
hostname
Andrei0123
Download a file
You can download a file with the Invoke-WebRequest command.
An example of a script that downloads
thrift-0.19.0.exe
and saves it as
thrift.exe
$THRIFT_URL = "https://dlcdn.apache.org/thrift/0.19.0/thrift-0.19.0.exe" $FilePath = ".\thrift.exe" If (Test-Path -path $FilePath -PathType Leaf) { Write-Host "thrift.exe file exists" -f Green } Else { Write-Host "thrift.exe file does not exist - starting download" -f Yellow Invoke-WebRequest $THRIFT_URL -OutFile thrift.exe }
Article author: Andrei Olegovich