【问题标题】:CSV export from a powershell script从 powershell 脚本导出 CSV
【发布时间】:2021-01-04 04:09:42
【问题描述】:

我在 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),则同上。

你是怎么做到的?

感谢您的帮助

【问题讨论】:

    标签: powershell csv export


    【解决方案1】:

    一种方法是将每个未安装软件的名称保存到一个数组中以供以后处理。

    话虽如此,可以改进有问题的测试路径。与其在这里和那里输入路径,不如将它们存储在一个集合中以便于处理。哈希表工作正常。像这样,

    $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 留给读者作为练习。

    【讨论】:

    • 我找到了这个 | Export-Csv -Path "C:\users\$env:username\Desktop\export.csv" -Force -NoTypeInformation 但我不知道你如何使用它。感谢您的帮助。
    【解决方案2】:

    VonPryz answer 所示的Hashtable 来收集要测试的软件的名称和路径确实是最简单的处理方法。

    从问题标题中,您想要一个包含测试结果的 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 在屏幕上的输出为表格:

    软件路径已安装 -------- ---- ------------ 7-Zip C:\Program Files\7-Zip 错误 Adobe Reader DC C:\Program Files (x86)\Adobe\Acrobat Reader DC True

    【讨论】:

    • 感谢您的帮助!这正是我想要的!你是老板!
    • @Reise62 很高兴它对你有用。如果您觉得我的回答解决了您的问题,请考虑通过单击左侧的 ✓ 图标来接受答案。见How to accept SO answers。这将帮助其他有类似问题的人更轻松地找到它。
    猜你喜欢
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多