2013-08-21 32 views
1

我有我希望有人能夠幫助...一個問題的Windows PowerShell - 無法創建一個HTML表格正確

我有以下代碼:

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) 

我確定有一些顯而易見的東西可以忽略,但是看不到我做錯了什麼。

你的幫助,這非常讚賞,感謝

+0

看一看HTML源代碼輸出?它依賴於ConvertTo-Html,但似乎它爲每行添加了一個整個表格。你可以將Get-HTMLTable分成3個函數:get-htmlTableHeader(打印一切,直到),get-htmlTableRow(打印,直到)和get-htmlTableFooter(從開始)。你必須在循環之外調用get-htmlTableHeader和get-htmlTableFooter。 –

回答

1

我也懷疑這是關係到你是如何構建的foreach循環對象中的自定義對象;玩了一段時間後,我放棄了,並試圖從頭開始構建它。希望這會對你有用。

的GET-ChildItem調用之前創建一個空數組:

$tableObject = @() 

然後更改您的foreach對象塊,填補了數組迭代與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 
    } 
+0

非常好,是的,這成功地工作。我正在嘗試更改出現在創建的HTML表中的標題。我想用數組中的值替換數組中的值,例如'$ tableObject = $ tableObject -replace'Name','ReplacementText',它將數組中的「Name」更改爲「ReplacementText」。但是,當我用新數組調用Get-HTMLTable函數並打印表格時,它只是打印幾個數字,而不是我想要的「新名稱」標題更改爲「ReplacementText」的新表格。這樣做的正確方法是什麼?非常感謝 –

+0

可能最好將自定義對象屬性更改爲您希望它們調用的內容,而不是稍後重命名它們。將$ properties PSCustomObject中的音符屬性的名稱更改爲任何你想要的,只要確保在填充它們時正確引用它們即可!上面的描述性代碼。 – nimizen

+0

非常好,謝謝......這是我認爲應該完成的方式,但是我錯誤的地方並沒有改變''「|選擇Whatever,You,Want'部分。如果我希望替換文本包含空格,而不是「名稱」,它會說「這是名稱」,更改'$ property'部分時使用的正確語法是什麼,因爲顯然是'$ properties.This名稱= $ _。名稱'不起作用? thansk求助 –