Powershell获取列出的值
我试图从哈希表中获取值,但我遇到了一些困难Powershell获取列出的值,powershell,Powershell,我试图从哈希表中获取值,但我遇到了一些困难 $allMediafiles是一个哈希表 这是我的代码: $AllDoc = ($allMediaFiles | Group-Object "video_audio" | ? {$_.name -eq 'document'}).Group $obj2 = [pscustomobject]@{ 'FileTypes' = ($AllDoc.video_audio | Group ).Name; 'Format'
$allMediafiles
是一个哈希表
这是我的代码:
$AllDoc = ($allMediaFiles | Group-Object "video_audio" | ? {$_.name -eq 'document'}).Group
$obj2 = [pscustomobject]@{
'FileTypes' = ($AllDoc.video_audio | Group ).Name;
'Format' = ($AllDoc.Format | Group).Values ;
'O_OpenAccess' = ($AllDoc | Group-Object "SecurityTag" | ? {$_.Name -eq "O_OpenAccess"}).count;
'U_UserAccess' = ($AllDoc | Group-Object "SecurityTag" | ? {$_.Name -eq "U_UserAccess"}).count;
'S_SubscriberAccess' = ($AllDoc | Group-Object "SecurityTag" | ? {$_.Name -eq "S_SubscriberAccess"}).count;
'REVIEW_ELDP' = ($AllDoc | Group-Object "SecurityTag" | ? {$_.Name -eq "REVIEW_ELDP"}).count;
}
我的结果是:
FileTypes Format O_OpenAccess U_UserAccess S_SubscriberAccess REVIEW_ELDP
--------- ------ ------------ ------------ ------------------ -----------
document {eaf, pfsx, pdf} 0 17 2 0
我需要这样的东西:
FileTypes Format O_OpenAccess U_UserAccess S_SubscriberAccess REVIEW_ELDP
--------- ------ ------------ ------------ ------------------ -----------
document eaf 0 14 1 0
pfsx 0 3 0 0
pdf 0 0 1 0
谢谢你的帮助
$Alldoc =
video_audio SecurityTag Duration Format
----------- ----------- -------- ------
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document S_SubscriberAccess eaf
document U_UserAccess pfsx
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess eaf
document U_UserAccess pfsx
document U_UserAccess pfsx
document U_UserAccess pfsx
document U_UserAccess eaf
document S_SubscriberAccess pdf
document U_UserAccess eaf
您只需再次调用
Group Object
-按格式对记录进行分组
-然后循环结果:
foreach($format in $Alldoc |Group-Object -Property Format)
{
[pscustomobject]@{
# Just grab the first one - they're all identical
'FileTypes' = $format.Group[0].video_audio
# Grab the format Name from when we grouped them
'Format' = $format.Name
# For the remaining ones, simply count the number of occurences
'O_OpenAccess' = @($format.Group |? {$_.SecurityTag -eq "O_OpenAccess"}).Count
'U_UserAccess' = @($format.Group |? {$_.SecurityTag -eq "U_UserAccess"}).Count
'S_SubscriberAccess' = @($format.Group |? {$_.SecurityTag -eq "S_SubscriberAccess"}).Count
'REVIEW_ELDP' = @($format.Group |? {$_.SecurityTag -eq "REVIEW_ELDP"}).Count
}
}
能否将
$AllDoc
的值添加到问题正文中?是的,没问题!对不起,我的错,@AndrewAre你确定$AllDoc
是一个哈希表,而不是(看起来)PSObject数组吗?@Theo,对不起,你说得对。$AllDoc变量是PSObject数组,是哈希表$allMediaFiles的结果$AllDoc=($allMediaFiles |组对象“video_audio”|?{$_.name-eq'document')。组