Foreach到CSV PowerShell

Foreach到CSV PowerShell,powershell,iis,Powershell,Iis,今天在愤怒中第一次使用Exportto CSV,在其他地方使用它,但当我做以下操作时,出现了一些随机数字 有人能告诉我哪里做错了吗 # List all sites, applications and appPools dir IIS:\AppPools | ForEach-Object { # Site's app pool $_.name $_.managedRuntimeVersion $_.enable32BitAppOnWin64 $_

今天在愤怒中第一次使用Exportto CSV,在其他地方使用它,但当我做以下操作时,出现了一些随机数字

有人能告诉我哪里做错了吗

    # List all sites, applications and appPools
dir IIS:\AppPools | ForEach-Object {
    # Site's app pool
    $_.name
    $_.managedRuntimeVersion
    $_.enable32BitAppOnWin64 
    $_.managedPipelineMode 
    $_.processModel.username
    $_.processModel.password
    $_.processModel.identityType
    # Any web applications on the site + their app pools
} | exportto-csv C:\bob.csv

查看
获取帮助导出Csv
说明。上面写着——

DESCRIPTION
    The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is represented as a line or
    row of the CSV. The row consists of a comma-separated list of the values of object properties. You can use this
    cmdlet to create spreadsheets and share data with programs that take CSV files as input.

    Do not format objects before sending them to the Export-CSV cmdlet. If you do, the format properties are
    represented in the CSV file, instead of the properties of the original objects. To export only selected properties
    of an object, use the Select-Object cmdlet.
阅读写有-的行,该行由逗号分隔的对象属性值列表组成。您看到的随机数是导出到CSV的属性的长度

为了克服这个问题,您可以使用如下的
PSCustomObject

$array= @()
dir IIS:\AppPools | ForEach-Object {
    $obj = New-Object PSObject

    $Name = Add-Member -MemberType NoteProperty -Name "Name" $_.Name
    $managedRuntimeVersion = Add-Member -MemberType NoteProperty -Name "managedRuntimeVersion" $_.managedRuntimeVersion
    .
    .
    #Rest of your properties
    $array += $obj
    }
$array | Export-Csv C:\bob.csv -NoTypeInformation
再次回答你的问题,你做错了什么-

  • 不了解
    Export Csv
    cmdlet的正确输入类型
  • 使用
    导出到Csv
    而不是
    导出Csv
    。我非常怀疑是否存在由
    Exportto Csv
    name生成的cmdlet。想知道你是怎么得到结果的
  • 当你生气的时候编码
    查看
    获取帮助导出Csv
    说明。上面写着——

    DESCRIPTION
        The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is represented as a line or
        row of the CSV. The row consists of a comma-separated list of the values of object properties. You can use this
        cmdlet to create spreadsheets and share data with programs that take CSV files as input.
    
        Do not format objects before sending them to the Export-CSV cmdlet. If you do, the format properties are
        represented in the CSV file, instead of the properties of the original objects. To export only selected properties
        of an object, use the Select-Object cmdlet.
    
    阅读写有-的行,该行由逗号分隔的对象属性值列表组成。您看到的随机数是导出到CSV的属性的长度

    为了克服这个问题,您可以使用如下的
    PSCustomObject

    $array= @()
    dir IIS:\AppPools | ForEach-Object {
        $obj = New-Object PSObject
    
        $Name = Add-Member -MemberType NoteProperty -Name "Name" $_.Name
        $managedRuntimeVersion = Add-Member -MemberType NoteProperty -Name "managedRuntimeVersion" $_.managedRuntimeVersion
        .
        .
        #Rest of your properties
        $array += $obj
        }
    $array | Export-Csv C:\bob.csv -NoTypeInformation
    
    再次回答你的问题,你做错了什么-

  • 不了解
    Export Csv
    cmdlet的正确输入类型
  • 使用
    导出到Csv
    而不是
    导出Csv
    。我非常怀疑是否存在由
    Exportto Csv
    name生成的cmdlet。想知道你是怎么得到结果的
  • 当你生气的时候编码
    导出CSV
    需要结构化数据作为对象属性。但是,您当前的代码通过管道将一组值传递到
    导出CSV

    尝试使用
    Select Object
    动态创建包含csv中所需属性的对象,然后通过管道将这些属性导出到
    Export csv

    像这样的事情应该可以完成工作:

    Get-ChildItem -path 'IIS:\AppPools' | 
        Select-Object -Property Name, `
            managedRuntimeVersion, `
            enabled32BitAppOnWin64, `
            managedPipelineMode, `
            @{label='processModel_username';     expression={$_.processModel.username}}, `
            @{label='processModel_password';     expression={$_.processModel.password}}, `
            @{label='processModel_identityType'; expression={$_.processModel.identityType}} | 
        Export-CSV -path 'C:\bob.csv'
    

    导出CSV
    需要结构化数据作为对象属性。但是,您当前的代码通过管道将一组值传递到
    导出CSV

    尝试使用
    Select Object
    动态创建包含csv中所需属性的对象,然后通过管道将这些属性导出到
    Export csv

    像这样的事情应该可以完成工作:

    Get-ChildItem -path 'IIS:\AppPools' | 
        Select-Object -Property Name, `
            managedRuntimeVersion, `
            enabled32BitAppOnWin64, `
            managedPipelineMode, `
            @{label='processModel_username';     expression={$_.processModel.username}}, `
            @{label='processModel_password';     expression={$_.processModel.password}}, `
            @{label='processModel_identityType'; expression={$_.processModel.identityType}} | 
        Export-CSV -path 'C:\bob.csv'
    

    为什么不使用类型Accelerator
    $array=Get ChildItem IIS:\AppPools | ForEach对象{[PSCustomObject}@{Name=$\uUx.Name;…}}
    ?应该比每次使用
    +=
    点重建数组更有效。注意并将实现taht。为什么不使用类型Accelerator
    $array=Get ChildItem IIS:\AppPools | ForEach对象{[PSCustomObject}{Name=$\uName;…}}
    ?应该比每次使用
    +=
    点重建数组更有效。注意并将实现taht。