我有下面的脚本,检查远程服务器上的页面文件大小,是否可以运行这个主机名的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
如果您的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
$ResultSetvariables现在包含一个表示每个计算机查询的PSObject每个计算机都有一个“ComputerName”,“PagefileInMB”,“Memory”和“PAEEnabled”属性。
现在,您可以轻松findPAE被禁用的计算机:
$ResultSet | Where-Object {$_.PAEEnabled -eq $False}