Group-object cmdlet的结果不一致

时间:2015-07-05 05:56:59

标签: powershell

我在2009版本中使用了powershell,并在我的脚本中使用了Group-object。

$todaye = (Get-Date -Hour 0 -Minute 00 -Second 00)
$yesterday = $todaye.AddDays(-1)

$EventLogs = 'Operations Manager'
$csv = @()
$strlog = $EventLogs.Split(',')
foreach($log in $strlog)
    {
       $log | ForEach-Object{ get-eventlog -log  $_ -After $yesterday - 
       Before $todaye | Group-Object EventID,EntryType,Source,MachineName,
    {(([String]$_.TimeGenerated) -split " ")[0]},  
    {(([String]$_.TimeGenerated) -split " ")[1]}, $_ -NoElement | 
              Sort-Object Count -Descending | 
              Select-Object count,name  | ForEach-Object{
       $properties = @{
        'ServerName' = ($_.Name -split ", ")[3];
        'day' = (($_.Name -split ", ")[4] -split " ")[0];
        'time' = (($_.Name -split ", ")[5]);
        'EventID' = ($_.Name -split ", ")[0];
        'EventType' = ($_.Name -split ", ")[1];
        'Source'    = ($_.Name -split ", ")[2];
        'Count' = $_.Count;

       }
    $psobject = new-object psobject -Property $properties
    $csv += $psobject
          }
       }
    }

我运行脚本后出现以下错误。

Group-Object : Cannot convert System.Management.Automation.PSObject to one
of the following types {System.String, 
System.Management.Automation.ScriptBlock}.

我在powersell 2012中运行我的脚本并没有任何错误。如何解决此错误。

1 个答案:

答案 0 :(得分:0)

从第二个$_行中删除{(([String]$_.TimeGenerated),然后它也可以在powershell 2上运行 像这样:

{(([String]$_.TimeGenerated) -split " ")[1]} -NoElement