我有以下脚本,用于检查远程服务器上的页面文件大小,是否可以针对主机名的 CSV 文件运行该脚本?
clear
$strComputer="computername"
$PageFile=Get-WmiObject Win32_PageFile -ComputerName $strComputer
Write-Host "Page File Size in MB: " ($PageFile.Filesize/(1024*1024))
$colItems=Get-WmiObject Win32_PhysicalMemory -Namespace root\CIMv2 -ComputerName $strComputer
$total=0
foreach ($objItem in $colItems) {
$total=$total+ $objItem.Capacity
}
$isPAEEnabled =Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer
答案1
如果您有一个带有“主机名”列的 Csv 文件(如下所示),则可以使用Import-Csv
。
示例 servers.csv 文件:
Id,Hostname,"ServerRole"
123,Server1,"Web Server"
124,Server2,"Web Server"
131,Server3,"App Server"
132,Server4,"App Server"
脚本使用foreach
并Import-Csv
完成您要做的事情:
# Clear the screen
Clear-Host
# Import the Hostname values from the CSV file
$ComputerNames = Import-Csv -Path "C:\servers.csv" | Select-Object -ExpandProperty "Hostname"
# Empty array to hold the results
$ResultSet = @()
# query each computer for their Page file and memory details
foreach($strComputer in $ComputerNames)
{
$PageFile=Get-WmiObject Win32_PageFile -ComputerName $strComputer
$PFSize = ($PageFile.Filesize/(1024*1024))
$colItems=Get-WmiObject Win32_PhysicalMemory -Namespace root\CIMv2 -ComputerName $strComputer
$total=0
foreach ($objItem in $colItems) {
$total = $total + $objItem.Capacity
}
$isPAEEnabled =Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer
# Store all the details in a temporary hashtable
$PageFileDetails = @{
"ComputerName" = $strComputer
"PagefileInMB" = $PFSize
"Memory" = $total
"PAEEnabled" = $isPAEEnabled
}
# Create a new object with the properties we defined in the hashtable and add it to the result
$ResultSet += New-Object psobject -Property $PageFileDetails
}
# the host application will print out the details for each computer on the screen
$ResultSet
该$ResultSet
变量现在包含PSObject
代表每台查询的计算机,每台计算机都有“ComputerName”、“PagefileInMB”、“Memory”和“PAEEnabled”属性。
现在您可以轻松找到禁用 PAE 的计算机:
$ResultSet | Where-Object {$_.PAEEnabled -eq $False}