[PowerShell] Come recuperare le informazione della Network e verificare le Porte Firewall SMB Share Folder siano aperte

shell

Oggi vi parlo di come recuperare le informazioni della Network , esempio il suo IPAddress, Subnet mask, Gateway Predefinito, Dominio DNS, ecc.. e verifica delle porte in ascolto TCP 139 e 445 Share Folder di Windows (Porte didicate al Share Folder SMB) , utilizzando il potentissimo PowerShell .

Per realizzare questo Script mi sono basato su due classi importanti la Win32_NetworkAdapterConfiguration  e  Win32_ComputerSystem .

Dalla classe Win32_ComputerSystem ho ricuperato il Dominio di lavoro (Esempio : MSHOME, WORGROUP, ecc..) e il Nome Utente Completo

Dalla classe Win32_NetworkAdapterConfiguration   ho recuperato  ComputerName , DNSDomain , IPAddress , SubnetMask ,
Gateway , IsDHCPEnabled , DNSServers , MACAddress

Vi allego il codice utilizzato e il file da eseguire su PowerShell .

<#
" Davide De Rubeis"

Author : Davide De Rubeis
E-Mail : [email protected]
website : www.imaginsystems.it
Purpose : Getting Network Information And Check Port SMB Open Firewall

ComputerName :
DNSDomain :
IPAddress :
SubnetMask :
Gateway :
IsDHCPEnabled : True|False
DNSServers :
MACAddress :

UserName :
FullUserName :
Workgroup :

Version : 1

#>
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)

begin {}
process {
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
}
$IPAddress = ""
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
try {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer -EA Stop | ? {$_.IPEnabled}
} catch {
Write-Warning "Error occurred while querying $computer."
Continue
}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$DNSDomain = $Network.DNSDomain
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSDomain -Value $DNSDomain
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
try {
$Networks = Get-WmiObject Win32_ComputerSystem -ComputerName .
} catch {
Write-Warning "Error occurred while querying $computer."
Continue
}
foreach ($Network in $Networks) {
$UserName = [Environment]::UserName
$FullUserName = $Network.UserName
$Workgroup = $Network.Workgroup
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name UserName -Value $UserName
$OutputObj | Add-Member -MemberType NoteProperty -Name FullUserName -Value $FullUserName
$OutputObj | Add-Member -MemberType NoteProperty -Name Workgroup -Value $Workgroup
$OutputObj
}
}
}

$t0 = New-Object Net.Sockets.TcpClient $IPAddress, 445

if($t0.Connected)
{
write-output "Check Port TCP/IP 445 is Open"
}
else
{
netsh advfirewall firewall add rule name="Open Port 445" dir=in action=allow protocol=TCP localport=445
netsh advfirewall firewall show rule name="Open Port 445"
}

$t1 = New-Object Net.Sockets.TcpClient $IPAddress, 139

if($t1.Connected)
{
write-output "Check Port TCP/IP 139 is Open `n"
}
else
{
netsh advfirewall firewall add rule name="Open Port 139" dir=in action=allow protocol=TCP localport=139
netsh advfirewall firewall show rule name="Open Port 139"
}

netstat -an | findstr "137 138 139 445" | findstr /I listen

$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()

}
end {}

Download Script PowerShell : 

Un commento su “[PowerShell] Come recuperare le informazione della Network e verificare le Porte Firewall SMB Share Folder siano aperte”

Lascia un commento