Rete
Cmdlet di rete di PowerShell (moduli NetTCPIP, NetAdapter, DnsClient). In Windows sostituiscono ipconfig, ping, netstat, nslookup e telnet.
Indirizzi e schede
Get-NetIPAddress
Get-NetIPAddress -AddressFamily IPv4
Get-NetIPConfiguration
Get-NetIPConfiguration -Detailed
Get-NetAdapter
Get-NetAdapter | Where-Object Status -eq Up
Get-NetAdapter | Select-Object Name, InterfaceDescription, Status, LinkSpeed, MacAddress
Get-NetRoute
Get-NetRoute -DestinationPrefix 0.0.0.0/0
Get-NetIPConfiguration dà una sintesi simile a ipconfig /all.
Impostare un IP statico
New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 192.168.1.50 -PrefixLength 24 -DefaultGateway 192.168.1.1
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses 1.1.1.1, 8.8.8.8
Tornare al DHCP:
Set-NetIPInterface -InterfaceAlias "Ethernet" -Dhcp Enabled
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses
Il comando di un IP statico richiede console amministratore e, se esiste già un IP statico, va prima rimosso con Remove-NetIPAddress.
Test-NetConnection
L'equivalente moderno di ping, telnet e tracert.
Test-NetConnection google.com
Test-NetConnection 8.8.8.8
Test-NetConnection google.com -Port 443
Test-NetConnection 192.168.1.10 -Port 3389
Test-NetConnection google.com -TraceRoute
Test-NetConnection google.com -InformationLevel Quiet
Test-NetConnection google.com -Port 443 | Select-Object TcpTestSucceeded
TcpTestSucceeded : True significa che la porta è raggiungibile.
Ping classico:
Test-Connection google.com
Test-Connection google.com -Count 2
Test-Connection google.com -Count 1 -Quiet
Test-Connection PC01, PC02, PC03 -Count 1
Scansione di una subnet (semplice):
1..254 | ForEach-Object { $ip = "192.168.1.$_"; if (Test-Connection $ip -Count 1 -Quiet -TimeoutSeconds 1) { $ip } }
-TimeoutSeconds esiste in PowerShell 7+; in 5.1 la scansione è più lenta.
Connessioni e porte in ascolto
Get-NetTCPConnection
Get-NetTCPConnection -State Listen
Get-NetTCPConnection -State Established
Get-NetTCPConnection -LocalPort 443
Get-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess | Sort-Object LocalPort
Get-NetUDPEndpoint
Associare il processo:
Get-NetTCPConnection -State Listen | Select-Object LocalPort, @{n='Processo';e={(Get-Process -Id $_.OwningProcess).Name}} | Sort-Object LocalPort
DNS
Resolve-DnsName google.com
Resolve-DnsName google.com -Type MX
Resolve-DnsName google.com -Type TXT
Resolve-DnsName google.com -Server 1.1.1.1
Resolve-DnsName 8.8.8.8
Get-DnsClientCache
Clear-DnsClientCache
Get-DnsClientServerAddress
Wi-Fi e firewall
netsh wlan show profiles
netsh wlan show profile name="NomeRete" key=clear
Get-NetFirewallProfile
Get-NetFirewallRule -Enabled True -Direction Inbound | Select-Object DisplayName, Action, Profile
New-NetFirewallRule -DisplayName "Apri porta 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow
Remove-NetFirewallRule -DisplayName "Apri porta 8080"
Riavviare e reimpostare la rete
Restart-NetAdapter -Name "Ethernet"
Disable-NetAdapter -Name "Wi-Fi" -Confirm:$false
Enable-NetAdapter -Name "Wi-Fi"
IP pubblico
(Invoke-RestMethod https://api.ipify.org?format=json).ip
Condivisioni SMB
Get-SmbShare
Get-SmbConnection
New-SmbMapping -LocalPath Z: -RemotePath \\server\condivisione -Persistent $true
Remove-SmbMapping -LocalPath Z: -Force