PowerShell script implementation detects host types in the network _powershell

Source: Internet
Author: User
Tags memory usage

I've been writing a script that automatically detects host types in the network. Basic functions can be used to determine the host operating system type, if the host can get the host hardware parameters and performance parameters, and determine whether there is network equipment. It is often necessary for an operational staff to familiarize themselves with a strange network as soon as possible. So this script is handy, and if you have better advice, thank you!

Copy Code code as follows:

############################################
#Author: Lixiaosong
#Email: lixiaosong8706@gmail.com
#For: Detecting the host system type in the/24 mask network and obtaining Windows host parameters
#Version: 1.0
##############################################
Param (
[Parameter (mandatory= $true)] $Network
)
$Ip =for ($i = 1; $i-ile255; $i + + 1) {"$Network. $i"}
foreach ($Ipaddressin $ip) {
#检测相关端口状态
$Port 3389=3389 | %{Echo ((new-objectnet.sockets.tcpclient). Connect ("$Ipaddress", $_)) "$true"} 2> $null
$Port 22=22 | %{Echo ((new-objectnet.sockets.tcpclient). Connect ("$Ipaddress", $_)) "$true"} 2> $null
$Port 23=23 | %{Echo ((new-objectnet.sockets.tcpclient). Connect ("$Ipaddress", $_)) "$true"} 2> $null
$Pingtest =test-connection-computername$ipaddress-quiet
if ($Port 3389-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# Free Physical Memory
$Totalmem = (gwmi-computername "$Ipaddress" Win32_OperatingSystem). totalvisiblememorysize# Total Physical Memory
$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}
#获取域内Windows主机参数
Write-host "
=============================================================================================================== ==
Time: $ (get-date) Windows Server: $HostName. $DomainName ip:$ ($IPaddress. Padleft (2)) Brand: $ ($HostFirm. Padleft (2)) Model: $ ($ Hostmodel.padleft (2)) Serial number: $ ($HostSN. Padleft (2))
CPU Utilization: $ ($Havecpu. Padleft (8)) Memory usage: $ ($Permem. Padleft (13))
Disk Read/sec: $ ($DiskRead. Padleft (8)) Disk Write/sec: $ ($DiskWrite. Padleft (13))
Network Send/sec: $ ($NetworkSent. Padleft (8)) Network Receive/sec: $ ($NetworkReceive. Padleft (13))
Disk Palette total space free space use space use percent "-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 ($port 22-like "$true") {
Write-host "
=============================================================================================================== =
Server: $IPaddress Open port: "22" may be a "Linux host"-foregroundcoloryellow
}
#判断网络设备
if ($port 23-like "$true") {
Write-host "
=============================================================================================================== =
Server: $Ipaddress Open port: "23" may be a "network" device-foregroundcolorcyan
}
#主机不存在
if ($Pingtest-like "$False") {
Write-host "
=============================================================================================================== =
Server: $Ipaddress This host does not exist "-foregroundcolorred
}
}

Examples of how to use:

1 Save the script to C:\
2 run PowerShell to perform PS c:\>. \TEST.PS1 10.7.2 #只需输入网络的前三位

Copy Code code as follows:

PS c:\>. \test.ps1 10.7.2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.