Powershell 批量获取 Windows 服务器信息并导出到 CSV 文件
以下是一个示例脚本,可以获取 100 台 Windows 服务器的 IP 地址、CPU、内存容量和硬盘容量,并将结果保存到 CSV 文件中:
# 定义服务器列表
$serverList = @(
'server1.example.com',
'server2.example.com',
'server3.example.com',
# ...
'server100.example.com'
)
# 创建空数组以保存结果
$results = @()
# 循环遍历服务器列表
foreach ($server in $serverList) {
# 获取系统信息
$systemInfo = Get-WmiObject Win32_ComputerSystem -ComputerName $server
$cpuInfo = Get-WmiObject Win32_Processor -ComputerName $server
$memoryInfo = Get-WmiObject Win32_PhysicalMemory -ComputerName $server
$diskInfo = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'" -ComputerName $server
# 构建结果对象
$result = New-Object PSObject
$result | Add-Member NoteProperty Server $server
$result | Add-Member NoteProperty CPU $cpuInfo.Name
$result | Add-Member NoteProperty MemoryCapacity ($memoryInfo.Capacity | Measure-Object -Sum).Sum
$result | Add-Member NoteProperty DiskCapacity ($diskInfo.Size / 1GB)
# 将结果添加到数组中
$results += $result
}
# 将结果保存到 CSV 文件
$results | Export-Csv -Path 'server_info.csv' -NoTypeInformation
以上脚本中,首先定义了一个包含 100 台服务器名称的列表。然后使用 foreach 循环遍历该列表,并使用 Get-WmiObject 命令获取每台服务器的系统信息。然后,将所有信息组合成一个结果对象,并将其添加到结果数组中。最后,使用 Export-Csv 命令将结果数组保存到 CSV 文件中。
原文地址: https://www.cveoy.top/t/topic/mPcH 著作权归作者所有。请勿转载和采集!