1

私は誰かが助けてくれることを望んでいる問題を抱えています...

次のコードがあります。

Else {
                $script:MyReport += Get-CustomHeader "2" "Files older than"

                Get-ChildItem -Path $Target -Recurse | Where-Object { $_.LastWriteTime -lt $(get-date).('Add' + $PeriodName).Invoke(-$periodvalue) `
                -and $_.psiscontainer -eq $false } | `
                #Loop through the results and create a hashtable containing the properties to be added to a custom object

                ForEach-Object {
                    $properties = @{ 
                        Path = $_.Directory 
                        Name = $_.Name 
                        DateModified = $_.LastWriteTime }
                    #Create and output the custom object
                    $var1 = New-Object PSObject -Property $properties | select Path,Name,DateModified  
                    $script:MyReport += Get-HTMLTable ( $var1 | select Path,Name,DateModified )   

                }             

       } #Close Else clause on Test-Path conditional

関数 (以下Get-HTMLTableに示す):

Function Get-HTMLTable{
    param([array]$Content)
    $HTMLTable = $Content | ConvertTo-Html
    $HTMLTable = $HTMLTable -replace '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', ""
    $HTMLTable = $HTMLTable -replace '<html xmlns="http://www.w3.org/1999/xhtml">', ""
    $HTMLTable = $HTMLTable -replace '<head>', ""
    $HTMLTable = $HTMLTable -replace '<title>HTML TABLE</title>', ""
    $HTMLTable = $HTMLTable -replace '</head><body>', ""
    $HTMLTable = $HTMLTable -replace '</body></html>', ""
    Return $HTMLTable
}

私が抱えている問題:

これから得られる出力は次のとおりです。

Path                Name             DateModified
\\10.0.0.1\folder1  document1.xls    19/08/2013 16:02:01
Path                Name             DateModified
\\10.0.0.1\folder1  test.doc     20/08/2013 15:47:06
Path                Name             DateModified
\\10.0.0.1\folder1  anotherfile.txt  20/08/2013 15:42:26

ただし、実際に必要な出力は次のとおりです。

Path                Name             DateModified
\\10.0.0.1\folder1  document1.xls    19/08/2013 16:02:01
\\10.0.0.1\folder1  test.doc     20/08/2013 15:47:06
\\10.0.0.1\folder1  anotherfile.txt  20/08/2013 15:42:26

私が間違っていると思われるコードの部分は、これらの数行と foreach オブジェクト ループが原因で、正しい出力が得られない理由です。

$var1 = New-Object PSObject -Property $properties | select Path,Name,DateModified  
$script:MyReport += Get-HTMLTable ( $var1 | select Path,Name,DateModified )   

私が見落としている明らかな何かがあると確信していますが、私が間違っていることはわかりません。

ご協力ありがとうございます。

4

1 に答える 1

1

foreach-object ループ内でカスタム オブジェクトを構築する方法に関連していると思われます。しばらく遊んだ後、あきらめてゼロから構築しようとしました。うまくいけば、これはあなたのために働くでしょう。

Get-ChildItem 呼び出しの前に、空の配列を作成します。

$tableObject = @()

次に、 foreach-object ブロックを変更して、配列に psobject の内容を繰り返し入力します。

    ForEach-Object {

        $properties = "" | Select Directory, Name, DateModified
            $properties.Directory = $_.Directory 
            $properties.Name = $_.Name 
            $properties.DateModified = $_.LastWriteTime
            $tableObject += $properties
    }             

最後に、 foreach-object ブロックの外側にテーブルを追加し、何も選択せずに配列を渡します。

$script:MyReport += Get-HTMLTable ( $tableObject )

私のコメントによると、配列の値を変更しないでください。配列に正しい値を入力してください。

    ForEach-Object {

        $properties = "" | Select 'Whatever You', Might, Want
            $properties.('Whatever You') = $_.Directory 
            $properties.Might = $_.Name 
            $properties.Want = $_.LastWriteTime
            $tableObject += $properties
    } 
于 2013-08-21T18:49:06.393 に答える