PowerShell腳本實現(xiàn)檢測網(wǎng)絡內(nèi)主機類型
更新時間:2014年12月23日 10:37:53 投稿:junjie
這篇文章主要介紹了PowerShell腳本實現(xiàn)檢測網(wǎng)絡內(nèi)主機類型,本文腳本基本功能可以實現(xiàn)判斷主機操作系統(tǒng)類型,如果是域內(nèi)的主機可以獲取主機的硬件參數(shù)和性能參數(shù),并判斷是否存在網(wǎng)絡設備,需要的朋友可以參考下
最近一直在寫一個自動檢測網(wǎng)絡內(nèi)主機類型的腳本?;竟δ芸梢詫崿F(xiàn)判斷主機操作系統(tǒng)類型,如果是域內(nèi)的主機可以獲取主機的硬件參數(shù)和性能參數(shù),并判斷是否存在網(wǎng)絡設備。對一個運維人員來說往往需要盡快熟悉一個陌生的網(wǎng)絡。所以這個腳本就很方便了,如果有更好的建議歡迎指正感謝!
復制代碼 代碼如下:
############################################
#Author:Lixiaosong
#Email:lixiaosong8706@gmail.com
#For:檢測/24掩碼網(wǎng)絡內(nèi)主機系統(tǒng)類型并獲取windows主機參數(shù)
#Version:1.0
##############################################
Param(
[Parameter(Mandatory=$true)]$Network
)
$Ip=for($i= 1; $i-ile255; $i+= 1){"$Network.$i"}
foreach($Ipaddressin$IP){
#檢測相關端口狀態(tài)
$Port3389=3389 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Port22=22 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Port23=23 | %{ echo ((new-objectNet.Sockets.TcpClient).Connect("$Ipaddress",$_)) "$true"} 2>$null
$Pingtest=Test-connection-ComputerName$IPaddress-quiet
if($Port3389-like"$true"){
#服務器信息
$HostSN=(GWMI-ComputerName"$Ipaddress"win32_bios).SerialNUmber
$HostFirm=(GWMI-ComputerName"$Ipaddress"win32_bios).Manufacturer
$HostModel=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Model
#主機信息
$HostName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).DNSHostName
$DomainName=(GWMI-ComputerName"$Ipaddress"Win32_ComputerSystem).Domain
#服務器硬件資源信息
$Freemem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).FreePhysicalMemory#空余物理內(nèi)存
$Totalmem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).TotalVisibleMemorySize#總物理內(nèi)存
$cpu=((get-counter-ComputerName"$IPaddress"-counter"\processor(_total)\% processor time").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue
$DiskRead=" {0:0.0} KB"-f($(((get-counter-ComputerName"$Ipaddress"-counter"\LogicalDisk(_total)\Disk Read Bytes/sec").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue) / 1KB)
$DiskWrite="{0:0.0} KB"-f($(((get-counter-ComputerName"$Ipaddress"-counter"\LogicalDisk(_total)\Disk Write Bytes/sec").CounterSamples|where{$_.InstanceName -eq"_total"}).CookedValue) /1KB)
$NetworkSent=" {0:0.0} KB"-f($((Get-Counter-ComputerName"$Ipaddress"-Counter"\Network Interface(*)\Bytes Sent/sec").CounterSamples|%{$_.CookedValue}|sort|select-last1) / 1KB)
$NetworkReceive=" {0:0.0} KB"-f($((Get-Counter-ComputerName"$IPaddress"-Counter"\Network Interface(*)\Bytes Received/sec").CounterSamples|%{$_.CookedValue}|sort|select-last1) / 1KB)
$Havecpu= "{0:0.0} %"-f$cpu
$Permem="{0:0.0} %"-f((($Totalmem-$Freemem)/$Totalmem)*100)
$Disks= GWMI-ComputerName"$IPaddress"win32_logicaldisk|?{$_.drivetype -eq3}
#獲取域內(nèi)Windows主機參數(shù)
Write-host"
=================================================================================================================
時間:$(get-date) WINDOWS服務器:$HostName.$DomainName IP:$($IPaddress.Padleft(2)) 品牌:$($HostFirm.Padleft(2)) 型號:$($HostModel.Padleft(2)) 序列號:$($HostSN.Padleft(2))
CPU使用率:$($Havecpu.Padleft(8)) 內(nèi)存使用率:$($Permem.Padleft(13))
磁盤讀/秒:$($DiskRead.Padleft(8)) 磁盤寫/秒:$($DiskWrite.Padleft(13))
網(wǎng)絡發(fā)送/秒:$($NetworkSent.Padleft(8)) 網(wǎng)絡接收/秒:$($NetworkReceive.Padleft(13))
盤符 盤總空間 空閑空間 使用空間 使用百分比"-ForegroundColorGreen
foreach($Diskin$Disks){
$Size= "{0:0.0} GB"-f($Disk.Size / 1GB )
$FreeSpace= "{0:0.0} GB"-f($Disk.FreeSpace / 1GB)
$Used= ([int64]$Disk.size - [int64]$Disk.freespace)
$SpaceUsed= "{0:0.0} GB"-f($Used/ 1GB)
$Percent="{0:0.0} %"-f($Used* 100 / $Disk.Size)
$n=3
Write-Host" "$Disk.deviceid.PadRight($n)-no-ForegroundColorGreen
$n=10
Write-Host$Size.Padleft($n)-no-ForegroundColorGreen
Write-Host$FreeSpace.Padleft($n)-no-ForegroundColorGreen
Write-Host$SpaceUsed.Padleft($n)-no-ForegroundColorGreen
Write-Host$Percent.Padleft($n)-ForegroundColorGreen
}
}
#判斷l(xiāng)inux主機
if($port22-like"$true"){
write-host"
================================================================================================================
服務器:$IPaddress 開放端口:"22" 可能是一臺是"linux"主機"-ForegroundColorYellow
}
#判斷網(wǎng)絡設備
if($port23-like"$true"){
Write-host"
================================================================================================================
服務器:$Ipaddress 開放端口:"23" 可能是一臺"網(wǎng)絡"設備"-ForegroundColorCyan
}
#主機不存在
if($Pingtest-like"$False"){
Write-host"
================================================================================================================
服務器:$Ipaddress 此主機不存在"-ForegroundColorRed
}
}
使用方法舉例:
1 將腳本保存至c:\
2 運行powershell 執(zhí)行PS C:\> .\test.ps1 10.7.2 #只需輸入網(wǎng)絡的前三位
復制代碼 代碼如下:
PS C:\> .\test.ps1 10.7.2
相關文章
PowerShell中字符串使用單引號和雙引號的區(qū)別
這篇文章主要介紹了PowerShell中字符串使用單引號和雙引號的區(qū)別,大家可能會經(jīng)常遇到這個問題,需要的朋友可以參考下2014-08-08PowerShell中運行CMD命令的技巧總結(解決名稱沖突和特殊字符等問題)
這篇文章主要介紹了PowerShell中運行CMD命令的技巧總結(解決名稱沖突和特殊字符等問題),需要的朋友可以參考下2014-05-05