【问题标题】:Save the powershell's output in a CSV file将 powershell 的输出保存在 CSV 文件中
【发布时间】:2018-07-07 09:52:00
【问题描述】:

我有一个 powershell 脚本,我可以在其中获取有关我的操作系统(Windows 版本、构建...)的信息。然而,所有这些信息都显示在 powershell 控制台中,我希望将它们导出为 CSV 或 XML 文件。

脚本是:

Get-CimInstance Win32_OperatingSystem |
    Select-Object  Caption, CSDVersion, ServicePackMajorVersion, BuildNumber |
    FL

【问题讨论】:

    标签: windows powershell csv


    【解决方案1】:

    使用Export-Csv cmdlet:

    Get-CimInstance Win32_OperatingSystem | Select-Object Caption, CSDVersion, ServicePackMajorVersion, BuildNumber | Export-Csv -NoTypeInformation -Path .\OS_Info.csv
    

    结果(OS_Info.csv):

    "Caption","CSDVersion","ServicePackMajorVersion","BuildNumber"
    "Microsoft Windows Server 2012 R2 Datacenter",,"0","9600"
    

    谢谢你的工作,文件在文件夹System32中生成

    正如 Rohin Sidharth 提到的,.\ 路径前缀将在当前目录(PowerShell 中的$PWD)中创建文件。您可能以管理员身份运行 PowerShell:在这种情况下,默认目录是 %WinDir%\System32。只需使用完整路径或GetFolderPath .Net 方法即可获取常用文件夹路径,如桌面:

    ... | Export-Csv -NoTypeInformation -Path 'C:\OS_Info.csv'
    ... | Export-Csv -NoTypeInformation -Path (Join-Path -Path [System.Environment]::GetFolderPath('Desktop') -ChildPath 'OS_Info.csv')
    

    你能告诉我如何在同一个文件中导出多个结果吗?为了 例如我有一个脚本,我可以在其中知道所有更新 已安装:

    Get-Hotfix | Select HotfixID,Description,InstalledOn | Sort InstalledOnfunction

    我希望结果保存在同一个 CSV 文件中

    您可以使用 Select-Object 的 calculated properties 来做到这一点:

    # Get OS info
    $OsInfo = Get-CimInstance -ClassName Win32_OperatingSystem
    
    Get-Hotfix | # Get HotFixes
    Sort-Object -Property InstalledOnfunction | # Sort them
    Select-Object -Property @( # Select required fields
    
        # Add Caption property from $OsInfo variable
        @{
            Name = 'Caption'
            Expression = {$OsInfo.Caption}
        }
    
        # Add CSDVersion property from $OsInfo variable
        @{
            Name = 'CSDVersion'
            Expression = {$OsInfo.CSDVersion}
        }
    
        # Add ServicePackMajorVersion property from $OsInfo variable
        @{
            Name = 'ServicePackMajorVersion'
            Expression = {$OsInfo.ServicePackMajorVersion}
        }
    
        # Add BuildNumber property from $OsInfo variable
        @{
            Name = 'BuildNumber'
            Expression = {$OsInfo.BuildNumber}
        }
    
        # Add other properties from original HotFix object
        'HotfixID'
        'Description'
        'InstalledOn'
    
    ) | Export-Csv -NoTypeInformation -Path 'C:\OS_Info.csv'
    

    您也可以尝试使用自定义函数join objects

    【讨论】:

    • 谢谢你的工作,文件是在文件夹 System32 中生成的。
    • 你能告诉我在桌面或任何其他选定的文件中生成文件的方法吗?
    • 你能告诉我如何在同一个文件中导出多个结果吗?例如,我有一个脚本,我可以在其中知道所有已安装的更新:Get-Hotfix |选择 HotfixID、描述、InstalledOn |对 InstalledOnfunction 进行排序,我希望将结果保存在同一个 CSV 文件中。
    • .\ 在文件名前面强制 powershell 使用相对路径。如果您的 Powershell 提示位于 C:\Temp,则该文件将在 temp 文件夹中创建。在您的情况下,它是 C:\windows\system32。您还可以指定路径` ... | Export-Csv -NoTypeInformation -Path C:\MyScript\OS_Info.csv`
    【解决方案2】:

    快速提示:确保不要通过管道传输到格式列表 (FL),然后通过管道传输到 export-csv,否则您将打开 CSV 文件,您的数据将如下所示。

    ClassId2e4f51ef21dd47e99d3c952918aff9cd pageHeaderEntry pageFooterEntry 
    autosizeInfo    shapeInfo   groupingEntry
    033ecb2bc07a4d43b5ef94ed5a35d280                
    Microsoft.PowerShell.Commands.Internal.Format.ListViewHeaderInfo    
    9e210fe47d09416682b841769c78b8a3                    
    27c87ef9bbda4f709f6b4002fa4af63c                    
    4ec4f0187cb04f4cb6973460dfe252df                    
    cf522b78d86c486691226b40aa69e95c                    
    

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 2021-08-24
      • 2015-08-07
      • 2022-11-28
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2022-01-13
      相关资源
      最近更新 更多