服务器信息收集

#Version:1.2
#Modify Date:2013-05-21

#说明:
#该脚本可以获取计算机名,域名,IP地址,操作系统版本,CPU名称+单颗CPU内核数量*CPU个数,内存大小(GB),单块磁盘大小,计算机序列号,制造商,计算机型号
#该脚本先将计算机信息输出到txt文件中,然后再自动输出到csv格式文件,检查无误后,可以将txt文件删除
#在运行该脚本的时候需要确保对需要获取信息的计算机有管理员权限,可以通过UNC路径访问到分区即可
#增加系统x86,x64版本判断

#定义获取计算机信息的函数
Function GetServerInfo ($server)
{
If (Test-Connection $server -count 1 -quiet)
{$system = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $server
If ($?)
{
#获取计算机域名、型号
$domainname = $system.Domain
$model = $system.Model

#获取计算机IP地址,取IP和gw不为空的网卡IP地址
$ip = gwmi Win32_NetworkAdapterConfiguration -computer $server|?{$_.ipaddress -ne $null -and $_.defaultipgateway -ne $null}
$ipaddr = $ip.ipaddress

#获取操作系统版本
$os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $server
#获取SP版本号
$osversion_letter = switch ($os.Version.Split(".")[2])
{
9200 {$null}
7601 {"SP1"}
3790 {"SP2"}
default {$os.Version}
}
$osversion = $os.Caption + " " + $osversion_letter + " X"+$os.OSArchitecture.Substring(0,2)


#获取CPU名称、单颗CPU核心数量*CPU个数
$cpus = Get-WmiObject win32_processor -ComputerName $server
$cpucount = 0
Foreach ($cpu in $cpus)
{
If ($cpu.DeviceID -ne $null)
{$cpucount += 1}
}
$cpunamecore = $cpu.name+" "+[string]$cpu.NumberOfCores + '*' + [string]$cpucount + "C"

#获取内存大小
$memorys = Get-WmiObject -Class Win32_PhysicalMemory -ComputerName $server
#$memorylist = $null
$memorysize_sum = $null
Foreach ($memory in $memorys)
{
#$memorylist += ($memory.capacity/1024/1024/1024).tostring("F1")+"GB + "
$memorysize_sum += $memory.capacity/1024/1024/1024
}

#获取磁盘信息
$disks = Get-WmiObject Win32_Diskdrive -ComputerName $server
$disklist = $null
#$disksize_sum = $null
Foreach ($disk in $disks)
{
$disklist += ($disk.deviceid.replace("\\.\PHYSICALDRIVE","Disk") +":" + [int]($disk.size/1024/1024/1024)+"GB ")
#$disksize_sum+=$disk.size
}

#获取计算机序列号、制造商
$bios = Get-WmiObject -Class Win32_BIOS -ComputerName $server
$sn = $bios.SerialNumber
If ($sn.Substring(0,6) -eq "VMware")
{$sn = "VMware-"}
$manufacturer = $bios.Manufacturer

$serverinfo = $server + "," + $domainname + "," + $ipaddr + "," + $osversion + "," + $cpunamecore + "," + $memorysize_sum + "," + $disklist + "," + $sn + "," + $manufacturer + "," + $model
}
Else {$serverinfo = $server + ",RPC服务不可用"}
}
Else {$serverinfo = $server + ",无法Ping通"}
Return $serverinfo
}


#定义服务器列表文件位置,输出结果保存位置
$serverlist_path="D:\infor_collect\serverlist.txt"
$serverInfo_result_path_tmp="D:\infor_collect\ServerInfo_result_tmp.txt"
$serverInfo_result_path="D:\infor_collect\ServerInfo_result.csv"

If (Test-Path $serverInfo_result_path_tmp)
{
$vbs = New-Object -ComObject WScript.Shell
$vbs.popup($serverInfo_result_path_tmp + " 文件已存在,请重新输入保存位置!",0,"提示")
Exit
}
If (Test-Path $serverInfo_result_path)
{
$vbs = New-Object -ComObject WScript.Shell
$vbs.popup($serverInfo_result_path + " 文件已存在,请重新输入保存位置!",0,"提示")
Exit
}


$dt = Get-Date
[array]$serverlist=gc $serverlist_path
$usedrows = $serverlist.count
#写入标题行
"计算机名,域名,IP地址,操作系统,CPU,内存大小(GB),磁盘,序列号,制造商,计算机型号"|Out-File $serverInfo_result_path_tmp -Append
For($i=1;$i -le $usedrows;$i++)
{
$server = $serverlist[$i-1]
#显示进度条,显示百分比 和 当前第几个
$w=$i.tostring()+'/'+$usedrows.tostring()
Write-Progress -Activity "进度显示" -status "正在处理 $server,请耐心等待" -percentcomplete ($i/$usedrows*100) -currentoperation $w

If ($server -ne "$null")
{ GetServerInfo $server|Out-File $serverInfo_result_path_tmp -Append}
Else {Write-Host "第" $i "行为空" -ForegroundColor Red}
}
#将txt格式文件转换为csv格式
Import-Csv $serverInfo_result_path_tmp |Export-Csv $serverInfo_result_path -Encoding UTF8 -NoTypeInformation
#获取脚本运行时长
Write-Host "该脚本运行时长为:" (New-Timespan $dt).TotalMinutes "分钟" -ForegroundColor Green

个人搜集整理学习路线及笔记icon-default.png?t=N3I4https://mp.weixin.qq.com/s/KQx_eIwdjCj3QdErxKb7ZQ

猜你喜欢

转载自blog.csdn.net/2201_75719295/article/details/130681311