簡體   English   中英

從 powershell 腳本導出 CSV

[英]CSV export from a powershell script

我在 powershell 中有一段代碼,我將其用作“安裝配方”。 我使用這個腳本來檢查 PC 的准備是否良好以及各種軟件是否安裝正確。 Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force

If ((Test-Path "C:\Program Files\7-Zip") -eq $True) 
   {Write-Host " ~                 7-ZIP : Installation => OK!                  ~" -ForegroundColor Green}
else{Write-Host " ~                7-ZIP : Installation => NOK!                  ~" -ForegroundColor Red}
 
Sleep 3

If ((Test-Path "C:\Program Files (x86)\Adobe\Acrobat Reader DC") -eq $True) 
   {Write-Host " ~             Adobe Reader DC : Install => OK!                 ~" -ForegroundColor Green}
else{Write-Host " ~            Adobe Reader DC : Install => NOK!                 ~" -ForegroundColor Red}

exit

如果安裝正常 (OK),那么它會生成一個我們存儲的值,然后導出到 .CSV 或 .XLSX 文件。 如果安裝不正常 (NOK),同上。

你是怎樣做的?

謝謝你的幫助

一種方法是將每個未安裝軟件的名稱保存到一個數組中以供以后處理。

話雖這么說,測試路徑可以改進。 不要在這里和那里輸入路徑,而是將它們存儲在一個集合中以便於處理。 哈希表工作正常。 像這樣,

$ht = @{
"7-Zip" = "C:\Program Files\7-Zip"
"Adobe Reader DC" = "C:\Program Files (x86)\Adobe\Acrobat Reader DC"
"FooApp" = "C:\Program Files\FooApp"
}
$failedInstalls = @()
    
foreach($key in $ht.keys){ 
    if(test-path $ht[$key] ) {
        Write-Host " ~ $key : Installation => OK! ~" -ForegroundColor Green
    } else {
        Write-Host " ~ $key : Installation => NOK! ~" -ForegroundColor Red
        $failedInstalls += $key
    }
}
$failedInstalls

這里所做的是將軟件名稱和路徑存儲在哈希表中。 因此,所有路徑的一個中心位置。 然后迭代集合並將每個丟失的軟件添加到$failedInstalls數組。 更改軟件數量是微不足道的,它只需要更改哈希表 - 不需要每個軟件的if(test-path...語句。

如何將數組導出為 XSLX 或 CSV 留給讀者作為練習。

VonPryz 答案中顯示的哈希表收集要測試的軟件的名稱和路徑確實是處理此問題的最簡單方法。

從問題標題,您需要一個包含測試結果的 CSV 文件; 不僅是彩色的控制台輸出。 為此,您需要遍歷您在變量中收集的軟件和輸出對象列表,如下所示:

# add as many items here as you would like to test
$software = @{
    "7-Zip"           = "C:\Program Files\7-Zip"
    "Adobe Reader DC" = "C:\Program Files (x86)\Adobe\Acrobat Reader DC"
}

# a template line for output to console
$message = ' ~ {0} : Install => {1}! ~'

# loop through the hashtable Keys
$result = $software.Keys | ForEach-Object {
    $installed = Test-Path -Path $software[$_]
    if ($installed) { $color = 'Green'; $success = 'OK' } 
    else { $color = 'Red'; $success = 'NOK' }
    # output to console
    Write-Host ($message -f $_, $success) -ForegroundColor $color
    # output an object to save as CSV
    [PsCustomObject]@{
        'Software'    = $_
        'Path'        = $software[$_]
        'IsInstalled' = $installed
    }
}

# output result to console as table
$result | Format-Table -AutoSize

# output result as CSV file
$result | Export-Csv -Path 'D:\Test\InstalledSoftware.csv' -NoTypeInformation

$result在屏幕上的輸出為表:

Software        Path                                           IsInstalled
--------        ----                                           -----------
7-Zip           C:\Program Files\7-Zip                               False
Adobe Reader DC C:\Program Files (x86)\Adobe\Acrobat Reader DC        True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM