国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PowerShell > 正文

PowerShell腳本實現(xiàn)檢測網(wǎng)絡(luò)內(nèi)主機類型

2020-03-26 19:12:28
字體:
供稿:網(wǎng)友

最近一直在寫一個自動檢測網(wǎng)絡(luò)內(nèi)主機類型的腳本?;竟δ芸梢詫崿F(xiàn)判斷主機操作系統(tǒng)類型,如果是域內(nèi)的主機可以獲取主機的硬件參數(shù)和性能參數(shù),并判斷是否存在網(wǎng)絡(luò)設(shè)備。對一個運維人員來說往往需要盡快熟悉一個陌生的網(wǎng)絡(luò)。所以這個腳本就很方便了,如果有更好的建議歡迎指正感謝!

復(fù)制代碼 代碼如下:

############################################
#Author:Lixiaosong
#Email:lixiaosong8706@gmail.com
#For:檢測/24掩碼網(wǎng)絡(luò)內(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){
#檢測相關(guān)端口狀態(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"){
#服務(wù)器信息
$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
#服務(wù)器硬件資源信息
$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服務(wù)器:$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)絡(luò)發(fā)送/秒:$($NetworkSent.Padleft(8)) 網(wǎng)絡(luò)接收/秒:$($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
}
}
#判斷linux主機
if($port22-like"$true"){
write-host"
================================================================================================================
服務(wù)器:$IPaddress 開放端口:"22" 可能是一臺是"linux"主機"-ForegroundColorYellow
}
#判斷網(wǎng)絡(luò)設(shè)備
if($port23-like"$true"){
Write-host"
================================================================================================================
服務(wù)器:$Ipaddress 開放端口:"23" 可能是一臺"網(wǎng)絡(luò)"設(shè)備"-ForegroundColorCyan
}
#主機不存在
if($Pingtest-like"$False"){
Write-host"
================================================================================================================
服務(wù)器:$Ipaddress 此主機不存在"-ForegroundColorRed
}
}

使用方法舉例:

 

1 將腳本保存至c:/
2 運行powershell 執(zhí)行PS C:/> ./test.ps1 10.7.2 #只需輸入網(wǎng)絡(luò)的前三位

復(fù)制代碼 代碼如下:

PS C:/> ./test.ps1 10.7.2

 

PowerShell腳本,檢測網(wǎng)絡(luò)內(nèi)主機類型

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 保山市| 英德市| 湘潭市| 新邵县| 海林市| 东安县| 济源市| 汨罗市| 峨眉山市| 安岳县| 淮阳县| 武川县| 百色市| 开远市| 若尔盖县| 尚义县| 神木县| 成武县| 石屏县| 察哈| 南城县| 葫芦岛市| 蒲江县| 车致| 宽城| 乌拉特中旗| 灌南县| 永州市| 大悟县| 九台市| 泽库县| 南雄市| 绥阳县| 昔阳县| 永清县| 织金县| 云和县| 巴马| 西乡县| 贵德县| 本溪|