您能否帮助我将这三个脚本组合成一种类似
的格式ComputerName CPUUsageAverage MemoryUsage C PercentFree
xxx 12 50% 30%
我执行此操作的方式是:
Get-content '.\servers.txt' | Get-CPUusageAverage
以下是脚本:
CPU
Function Get-CPUusageAverage
{
$input|Foreach-Object{Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average}
}
内存
Function get-MemoryUsage
{
$input|Foreach-Object{
gwmi -Class win32_operatingsystem -computername $_ |
Select-Object @{Name = "MemoryUsage"; Expression = { “{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }
}
}
}
C PercentFree
Function get-CPercentFree
{
$input|ForEach-Object{
Get-WmiObject -Class win32_Volume -ComputerName $_ -Filter "DriveLetter = 'C:'" |
Select-object @{Name = "C PercentFree"; Expression = { “{0:N2}” -f (($_.FreeSpace / $_.Capacity)*100) } }
}
}
答案 0 :(得分:10)
首先我会避免使用$ input。您可以将查询组合成单个函数,然后输出带有每台计算机数据的pscustomobject,例如:
function Get-ComputerStats {
param(
[Parameter(Mandatory=$true, Position=0,
ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[string[]]$ComputerName
)
process {
foreach ($c in $ComputerName) {
$avg = Get-WmiObject win32_processor -computername $c |
Measure-Object -property LoadPercentage -Average |
Foreach {$_.Average}
$mem = Get-WmiObject win32_operatingsystem -ComputerName $c |
Foreach {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}
$free = Get-WmiObject Win32_Volume -ComputerName $c -Filter "DriveLetter = 'C:'" |
Foreach {"{0:N2}" -f (($_.FreeSpace / $_.Capacity)*100)}
new-object psobject -prop @{ # Work on PowerShell V2 and below
# [pscustomobject] [ordered] @{ # Only if on PowerShell V3
ComputerName = $c
AverageCpu = $avg
MemoryUsage = $mem
PercentFree = $free
}
}
}
cat '.\servers.txt' | Get-ComputerStats | Format-Table
答案 1 :(得分:1)
怎么样:
cat '.\servers.txt' | % {select -property @{'n'='CPUUsageAverage';'e'={$_ | Get-CPUUsageAverage}},@{'n'='Memory Usage';'e'={$_ | Get-MemoryUsage}},@{'n'=C PercentFree';'e'={$_ | Get-CPercentFree}}}
答案 2 :(得分:-1)
我上一篇文章(编辑过)上面的道歉,这是我用来获取生成HTML文件的CPU,Mem和C Drive可用空间的平均值的脚本
$ServerListFile = "D:\serverList.txt"
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue
$Result = @()
ForEach($computername in $ServerList)
{
$AVGProc = Get-WmiObject -computername $computername win32_processor |
Measure-Object -property LoadPercentage -Average | Select Average
$OS = gwmi -Class win32_operatingsystem -computername $computername |
Select-Object @{Name = "MemoryUsage"; Expression = {“{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}
$vol = Get-WmiObject -Class win32_Volume -ComputerName $computername -Filter "DriveLetter = 'C:'" |
Select-object @{Name = "C PercentFree"; Expression = {“{0:N2}” -f (($_.FreeSpace / $_.Capacity)*100) } }
$result += [PSCustomObject] @{
ServerName = "$computername"
CPULoad = "$($AVGProc.Average)%"
MemLoad = "$($OS.MemoryUsage)%"
CDrive = "$($vol.'C PercentFree')%"
}
$Outputreport = "<HTML><TITLE> Server Health Report </TITLE>
<BODY background-color:peachpuff>
<font color =""#99000"" face=""Microsoft Tai le"">
<H2> Server Health Report </H2></font>
<Table border=1 cellpadding=0 cellspacing=0>
<TR bgcolor=gray align=center>
<TD><B>Server Name</B></TD>
<TD><B>Avrg.CPU Utilization</B></TD>
<TD><B>Memory Utilization</B></TD>
<TD><B>Drive C Free Space</B></TD>
</TR>"
Foreach($Entry in $Result)
{
if(($Entry.CpuLoad) -or ($Entry.memload) -ge "80")
{
$Outputreport += "<TR bgcolor=white>"
}
else
{
$Outputreport += "<TR>"
}
$Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.CDrive)</TD></TR>"
}
$Outputreport += "</Table></BODY></HTML>"
}
$Outputreport | out-file "D:\Result $(Get-Date -Format yyy-mm-dd-hhmm).htm"