Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Powershell中组织/排序阵列的数据_Arrays_Sorting_Powershell - Fatal编程技术网

Arrays 在Powershell中组织/排序阵列的数据

Arrays 在Powershell中组织/排序阵列的数据,arrays,sorting,powershell,Arrays,Sorting,Powershell,我有一个数组,它将用户的登录信息作为对象保存。以下是该阵列的打印: PS> $Write-Out $data1 System.Object System.Object System.Object System.Object System.Object System.Object System.Object PS> $data1 ComputerName User Time Action

我有一个数组,它将用户的登录信息作为对象保存。以下是该阵列的打印:

PS> $Write-Out $data1
System.Object System.Object System.Object System.Object System.Object System.Object System.Object

PS> $data1 
ComputerName       User               Time                      Action                            
------------       ----               -------------------       ------
DC1                usr1               05/06/2013 11:51:35       logoff                         
DC1                usr1               05/06/2013 11:46:24       logon                         
DC1                usr1               05/06/2013 11:42:05       logoff
DC2                usr2               05/06/2013 11:44:08       logon
DC2                Administrator      05/06/2013 11:43:50       logoff
DC2                Administrator      05/06/2013 11:42:53       logon
DC2                Administrator      05/06/2013 11:40:27       logoff
我想转换此数组,以便可以在同一行上看到登录和注销时间,如下所示:

PS> $data2
ComputerName      User               Time LOGON           Time LOGOFF      
------------      ----               -------------------  -------------------                  
DC1               usr1               05/06/2013 11:46:24  05/06/2013 11:51:35                         
DC1               usr1                                    05/06/2013 11:42:05       
DC2               usr2               05/06/2013 11:44:08                        
DC2               Administrator      05/06/2013 11:42:53  05/06/2013 11:43:50
DC2               Administrator                           05/06/2013 11:40:27       

您能帮我将$data1数组转换为$data2数组吗?

我认为您应该创建自己的对象,并从$data1中分配值。 你可以这样做:

    $readableData1| Select ComputerName,User #Just an example to create a simple object
    $data1|%{ #Foreach line in $data1

    $readabledata.ComputerName = $_.ComputerName.ToString()
    $readabledata.User= $_.User.ToString()

    $array+= $readableData1
    }

echo $array

我认为您应该创建自己的对象,并从$data1分配值。 你可以这样做:

    $readableData1| Select ComputerName,User #Just an example to create a simple object
    $data1|%{ #Foreach line in $data1

    $readabledata.ComputerName = $_.ComputerName.ToString()
    $readabledata.User= $_.User.ToString()

    $array+= $readableData1
    }

echo $array

试试这个。它将根据您的
$data1
为您提供相同的
$data2
,但可能包含错误

$data2 = $data1 | Group-Object -Property ComputerName, User | foreach { # group
    foreach ($r in $_.Group) { # record
        if ($r.Action -eq "logon") {
            New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=$r.Time; User=$r.User; ComputerName=$r.ComputerName};
            $off = $null;
            continue;
        } else {$off = $r.Time;}
    }
    if ($off) {New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=""; User=$r.User; ComputerName=$r.ComputerName}; $off=$null;}
}

试试这个。它将根据您的
$data1
为您提供相同的
$data2
,但可能包含错误

$data2 = $data1 | Group-Object -Property ComputerName, User | foreach { # group
    foreach ($r in $_.Group) { # record
        if ($r.Action -eq "logon") {
            New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=$r.Time; User=$r.User; ComputerName=$r.ComputerName};
            $off = $null;
            continue;
        } else {$off = $r.Time;}
    }
    if ($off) {New-Object PSObject -Property @{"Time LOGOFF"=$off; "Time LOGON"=""; User=$r.User; ComputerName=$r.ComputerName}; $off=$null;}
}

您能给出生成$data1的命令行吗?您能给出生成$data1的命令行吗?从$data1生成自定义对象是一种方法;)代码不起作用,但我知道了。谢谢$data1中的自定义对象是一种方式;)代码不起作用,但我知道了。谢谢