Powershell:缺少' ='哈希文字错误键后的运算符

时间:2015-04-01 14:17:44

标签: file powershell crash

代码:

$ServerListFile = "D:\Scripts\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) } 

Get-Eventlog -LogName Security -Newest 5000 | Where-Object {$_.EventID -eq "4624"} | Select-Object @{Name ='Username'; Expression = {$_.ReplacementStrings[1]}}
} 

$result += [PSCustomObject] @{  
        ServerName = "$computername" 
        CPULoad = "$($AVGProc.Average)%" 
        MemLoad = "$($OS.MemoryUsage)%" 
        EventLog = "$username"
    } 

    $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>Username Event-4624</B></TD></TR>" 

    Foreach($Entry in $Result)  

        {  
          if((($Entry.CpuLoad) -or ($Entry.memload)) -ge 80 )  
          {  
            $Outputreport += "<TR bgcolor=red>"  
          }  
          else 
           { 
            $Outputreport += "<TR>"  
          } 
          $Outputreport += "<TD>$($Entry.Servername)</TD><TD align=center>$($Entry.CPULoad)</TD><TD align=center>$($Entry.MemLoad)</TD><TD align=center>$($Entry.EventLog)</TD></TR>"  
        } 
     $Outputreport += "</Table></BODY></HTML>"  
        }  

$Outputreport | out-file D:\Scripts\Test.htm  
Invoke-Expression D:\Scripts\Test.htm

产生以下错误:

Missing '=' operator after key in hash literal.

At D:\Scripts\AGAIN.PS1:13 char:13

+ Get-Eventlog <<<<  -LogName Security -Newest 5000 | Where-Object {$_.EventID -eq "4624"} | Select-Object @{Name ='Username'; Expression = {$_.ReplacementStrings[1]}}

+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral

1 个答案:

答案 0 :(得分:1)

在这里错过}

Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) } }