本文介绍了Powershel 从 [pscustomobject] 属性计数创建新的 [pscustomobject]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我需要使用另一个 [pscustomobject] 的数据创建新的 [pscustomobject].创建一份报告.使用这个新的 [pscustomobject],我将导出到 excel.

Now i need to create new [pscustomobject] with the data of another [pscustomobject]. To create one report.With this new [pscustomobject], i will export to an excel.

我的代码:

Function ConvertTo-MultiArray {

param(
    [Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true)]
    [PSObject[]]$InputObject
)
BEGIN {
    $objects = @()
    [ref]$array = [ref]$null
}
Process {
    $objects += $InputObject
}
END {
    $properties = $objects[0].psobject.properties |%{$_.name}
    $array.Value = New-Object 'object[,]' ($objects.Count+1),$properties.count
    # i = row and j = column
    $j = 0
    $properties |%{
        $array.Value[0,$j] = $_.tostring()
        $j++
    }
    $i = 1
    $objects |% {
        $item = $_
        $j = 0
        $properties | % {
            if ($item.($_) -eq $null) {
                $array.value[$i,$j] = ""
            }
            else {
                $array.value[$i,$j] = $item.($_).tostring()
            }
            $j++
        }
        $i++
    }
    $array
  }
}

   function Convert-TimeString ([String]$Time, [String[]]$Format)
 {
   $result = New-Object TimeSpan

   $convertible = [TimeSpan]::TryParseExact(
   $Time,
   $Format,
   [System.Globalization.CultureInfo]::InvariantCulture,
   [ref]$result)

   if ($convertible) { $result.ToString('hh\:mm\:ss\.fff') }
   }

   $allMediaFiles = Get-ChildItem -Path C:\Users\User\Documents\ScripsPS1\xip\Testes\*   | % {
   $content = get-content -Path $_ -Raw
   $xml = [xml]$content
 

    [pscustomobject]@{
       video_audio = $xml.XIP.ContentObject.CustomType
       SecurityTag = $xml.XIP.ContentObject.SecurityTag 
       Duration = ($xml.XIP.Generation.Properties.Property | where {$_.PropertyName -eq  "Duration"}).value
      Format = $xml.XIP.Bitstream.Filename -replace ".*\.", ""
   }    
}

    $tt =[pscustomobject]@{
    FileTypes = $allMediaFiles | ? {$_.video_audio} | select -ExpandProperty video_audio -Unique
    O_OpenAccess = 'a'  
    U_UserAccess = 'c'
    S_SubscriberAccess = 'd'
    Duration = 'e'
  }

     $allMediaFiles | ? {$_.video_audio -eq "video"} |  ForEach-Object {

     if ($_.Duration -match "H|M|S") {
     $_.Duration = (Convert-TimeString -Time $_.Duration -Format 'm\Ms\.fff\S')
     [TimeSpan[]]$DurationVideo += $_.Duration
     }    
  } 
     $Result1 = ($DurationVideo | Measure-Object -Property TotalMilliseconds -Sum).Sum
     $mts =  [timespan]::FromMilliseconds($Result1)
     $VideoTime =("{0:hh\:mm\:ss\,fff}" -f $mts)
    [pscustomobject]@{'Timespan' = $VideoTime} 

    Clear-Variable -Name "DurationVideo"

    $allMediaFiles | ? {$_.video_audio -eq "audio"} |  ForEach-Object {
    [TimeSpan[]]$DurationAudio +=$_.Duration       
   }   
    $Result2 = ($DurationAudio | Measure-Object -Property TotalMilliseconds -Sum).Sum
    $Nts =  [timespan]::FromMilliseconds($Result2)
    $AudioTime =("{0:hh\:mm\:ss\,fff}" -f $Nts)
    [pscustomobject]@{'Timespan' = $AudioTime} 

     Clear-Variable -Name "DurationAudio"


     $DurationColl = $allMediaFiles | ? {$_.video_audio} | Group-Object -Property Duration |  Select Name

     $outItems = New-Object System.Collections.Generic.List[System.Object]
     [TimeSpan[]]$outItems = foreach ($eachtimer in $DurationColl){
     if ($eachtimer -match 'H|M|S'){Convert-TimeString -Time  $eachtimer -Format 'm\Ms\.fff\S' }
   else {
         Convert-TimeString -Time $eachtimer -Format "h\:mm\:ss"} 
}

    $seconds = ($outItems | Measure-Object -Property TotalMilliseconds -Sum).Sum
    $ts =  [timespan]::FromMilliseconds($seconds) 
    $AllTime =  ("{0:hh\:mm\:ss\,fff}" -f $ts) 
    $b=[pscustomobject]@{'Timespan' = $AllTime} 



   $excel = New-Object -ComObject Excel.Application
   $excel.visible = $true
   $workbook = $excel.Workbooks.Add()


   $array = ($tt |ConvertTo-MultiArray).Value

   $starta = [int][char]'a' - 1
   if ($array.GetLength(1) -gt 26) {
   $col = [char]([int][math]::Floor($array.GetLength(1)/26) + $starta) + [char]  (($array.GetLength(1)%26) + $Starta)}
 else {
      $col = [char]($array.GetLength(1) + $starta)
 }

  $range = $workbook.ActiveSheet.Range("a1","$col$($array.GetLength(0)+1)")
  $range.Value2 = $array

使用此代码:

       $allMediaFiles = Get-ChildItem -Path C:\Users\User\Documents\ScripsPS1\xip\Testes\*   | % {
   $content = get-content -Path $_ -Raw
   $xml = [xml]$content
 

    [pscustomobject]@{
       video_audio = $xml.XIP.ContentObject.CustomType
       SecurityTag = $xml.XIP.ContentObject.SecurityTag 
       Duration = ($xml.XIP.Generation.Properties.Property | where {$_.PropertyName -eq  "Duration"}).value
      Format = $xml.XIP.Bitstream.Filename -replace ".*\.", ""
   }    
}

我知道了(感谢 Mathias)

I've got this (thanks to Mathias)

     video_audio SecurityTag    Duration  Format
     ----------- -----------    --------  ------
      video       U_UserAccess   2M9.686S  mp4   
      video       U_UserAccess   2M37.526S mp4   
      video       U_UserAccess   5M41.419S mp4   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           pdf   
      audio       LOADING_BAY_10 0:11:19   WAV   
      audio       O_OpenAccess_10 0:03:24  WAV   
      audio       LOADING_BAY_10 0:03:37   WAV   
      audio       LOADING_BAY_10 0:28:49   WAV   
      document    O_OpenAccess             eaf   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      audio       LOADING_BAY_10 0:53:03   WAV   
      document    LOADING_BAY_10           eaf   
      audio       LOADING_BAY_10 0:23:39   WAV   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      document    LOADING_BAY_10           eaf   
      audio       LOADING_BAY_10 0:13:34   WAV   
      document    LOADING_BAY_10           eaf   
      audio       LOADING_BAY_10 0:44:07   WAV   
      audio       LOADING_BAY_10 0:10:56   WAV   
      audio       LOADING_BAY_10 0:25:56   WAV   
      audio       LOADING_BAY_10 0:16:46   wav 

现在我需要这个:

             FileTypes      O         U       L   Duration
             ---------- --------- -------- ------ --------
   video          3                   3                    
   Audio         11                  10       1                                         
   eaf           11          1               10             
   pdf            1                           1         
   mp4            3                   3                 
   WAV           11          2                9          

但是我在这方面遇到了一些麻烦:

But i'm having some trouble on this part:

    $tt =[pscustomobject]@{
    FileTypes = $allMediaFiles | ? {$_.video_audio} | select -ExpandProperty video_audio -Unique
    O_OpenAccess = 'a'  
    U_UserAccess = 'c'
    S_SubscriberAccess = 'd'
    Duration = 'e'
  }

这是我将创建一个excel文件的结果.因此,代码读取一些 xml 文件,获取所需的信息,最后使用该数据创建报告:

This is the result that i will create a excel file. So, the code read some xml files, get the information needed and at the end's create a report with that data:

Ex - 我们有多少视频带有 U_UserAccess securityTag ,或者我们有多少音频O_OpenAccess 等.我统计了许多音频、视频、mp4、waw 等等.

Ex - How many video do we have with U_UserAccess securityTag , or how many audios do we have withO_OpenAccess , and so on. I make the count of many audios, videos, mp4, waw, and so on.

推荐答案

不是很优雅,但很有效:

Not really elegant but does the job:

$obj = @()
$List1 = ($allMediaFiles | Group-Object "video_audio").Name
ForEach ($item in $List1) {
    $group = ($allMediaFiles | Group-Object "video_audio" | ? {$_.name -eq $item}).Group
    $obj += [pscustomobject]@{
        'name' = $item; 
        'FileTypes' = ($allMediaFiles | Group-Object "video_audio" | ? {$_.name -eq $item}).count; 
        'O' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "O_OpenAccess"}).count; 
        'U' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "U_UserAccess"}).count; 
        'L' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "LOADING_BAY_10"}).count;
        }
    }
$List2 = ($allMediaFiles | Group-Object "Format").Name
ForEach ($item in $List2) {
    $group = ($allMediaFiles | Group-Object "Format" | ? {$_.name -eq $item}).Group
    $obj += [pscustomobject]@{
        'name' = $item; 
        'FileTypes' = ($allMediaFiles | Group-Object "video_audio" | ? {$_.name -eq $item}).count; 
        'O' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "O_OpenAccess"}).count; 
        'U' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "U_UserAccess"}).count; 
        'L' = ($group | Group-Object "SecurityTag" | ? {$_.Name -eq "LOADING_BAY_10"}).count;
        }
    }

这篇关于Powershel 从 [pscustomobject] 属性计数创建新的 [pscustomobject]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:21