• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > PowerShell脚本实现检测网络内主机类型

PowerShell脚本实现检测网络内主机类型

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了powershell脚本,powershell脚本下载,powershell脚本代码,powershell执行脚本,powershell 运行脚本等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

最近一直在写一个自动检测网络内主机类型的脚本。基本功能可以实现判断主机操作系统类型,如果是域内的主机可以获取主机的硬件参数和性能参数,并判断是否存在网络设备。对一个运维人员来说往往需要尽快熟悉一个陌生的网络。所以这个脚本就很方便了,如果有更好的建议欢迎指正感谢!
############################################
#Author:Lixiaosong
#Email:lixiaosong8706@gmail.com
#For:检测/24掩码网络内主机系统类型并获取windows主机参数
#Version:1.0
##############################################
Param(
[Parameter(Mandatory=$true)]$Network
)
$Ip=for($i= 1; $i-ile255; $i+= 1){"$Network.$i"}
foreach($Ipaddressin$IP){
#检测相关端口状态
$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#空余物理内存
$Totalmem=(GWMI-ComputerName"$Ipaddress"win32_OperatingSystem).TotalVisibleMemorySize#总物理内存
$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"
=================================================================================================================
时间:$(get-date) WINDOWS服务器:$HostName.$DomainName IP:$($IPaddress.Padleft(2)) 品牌:$($HostFirm.Padleft(2)) 型号:$($HostModel.Padleft(2)) 序列号:$($HostSN.Padleft(2))
CPU使用率:$($Havecpu.Padleft(8)) 内存使用率:$($Permem.Padleft(13))
磁盘读/秒:$($DiskRead.Padleft(8)) 磁盘写/秒:$($DiskWrite.Padleft(13))
网络发送/秒:$($NetworkSent.Padleft(8)) 网络接收/秒:$($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"
================================================================================================================
服务器:$IPaddress 开放端口:"22" 可能是一台是"linux"主机"-ForegroundColorYellow
}
#判断网络设备
if($port23-like"$true"){
Write-host"
================================================================================================================
服务器:$Ipaddress 开放端口:"23" 可能是一台"网络"设备"-ForegroundColorCyan
}
#主机不存在
if($Pingtest-like"$False"){
Write-host"
================================================================================================================
服务器:$Ipaddress 此主机不存在"-ForegroundColorRed
}
}
</div>
使用方法举例:

1 将脚本保存至c:\
2 运行powershell 执行PS C:\> .\test.ps1 10.7.2 #只需输入网络的前三位
PS C:\> .\test.ps1 10.7.2
</div>

</div>

您可能想查找下面的文章:

  • PowerShell因为在此系统中禁止执行脚本的解决方法
  • PowerShell时间记录脚本
  • Powershell实现编写和运行脚本
  • PowerShell脚本反引号用法实例:随时随地给代码换行
  • Powershell脚本的4种执行权限介绍
  • PowerShell中获取当前运行脚本路径的方法
  • PowerShell实现动态获取当前脚本运行时消耗的内存
  • PowerShell实现的文件同步脚本分享
  • Powershell实现获取电脑序列号功能脚本分享
  • Powershell脚本中使用条件断点实例

相关文章

  • Powershell小技巧之轻松从网上下载文件
  • Powershell实现按类型排序
  • PowerShell捕获错误的2种方法(异常捕获命令、错误变量)
  • PowerShell ISE中代码转换大小写的技巧
  • Powershell Profiles配置文件的存放位置介绍
  • PowerShell中使用PrintManagement管理打印机示例
  • Powershell小技巧之使用Jint引擎在PowerShell中执行Javascript函数
  • Powershell中请求WebServices并以JSON格式输出结果
  • PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子
  • 探索PowerShell(十二) 筛选器 Filters

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell中使用PrintManagement管理打印机示例
    • PowerShell 3.0管理Hyper-V 3.0
    • PowerShell中使用正则表达式筛选数组实例
    • PowerShell统计文件夹下文件个数的方法
    • 探索PowerShell(十) 循环语句介绍
    • PowerShell函数简明教程
    • PowerShell中使用Get-ChildItem命令读取目录、文件列表使用例子和小技巧
    • Powershell读取本机注册表中的所有软件关联扩展名
    • PowerShell中编程清空IE缓存方法
    • PowerShell中使用Filter来创建管道输入函数

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有