es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

No es posible añadir datos al archivo CSV usando PowerShell.

Estoy tratando de agregar datos a un archivo csv.

Estoy creando el csv con el encabezado primero y luego tratando de agregar las filas, pero está devolviendo un archivo csv en blanco.



$props=[ordered]@{ ServerName='' SystemFolderPath='' IdenityReference='' FileSystemRights='' } New-Object PsObject -Property $props | Export-Csv "C:\status_report.csv" -NoTypeInformation

$serverlist = Get-Content -Path “C:\ServerList.txt”

foreach($server in $serverlist)
{

$paths_list = $env:Path -Split ‘;’

Foreach ($sys_Path in $paths_list)
{

$Permissions = Get-Acl -Path $sys_Path
$Users_Permissions = $Permissions.Access | Where-Object {$_.IdentityReference}
#$Users_Permission
        Foreach  ($user in $Users_Permissions)
        {
                $IdenityReference = $user.IdentityReference.Value
                $FileSystemRights = $user.FileSystemRights

                $NewLine = "{0},{1},{2},{3}" -f $server,$sys_Path,$IdenityReference,$FileSystemRights
                $NewLine | Export-Csv -Path "C:\status_report.csv" -Append -NoTypeInformation -Force

        }

}
}

Por favor, avíseme qué estoy haciendo mal aquí.

Tags:  ,

Answer

  1. Avatar for davy.ai

    El problema aquí es con la forma en que estás creando las nuevas filas para agregar al archivo CSV. Estás utilizando el operador -f para formatear la nueva fila como una cadena, pero esto está causando que los datos se agreguen como una sola columna en lugar de varias columnas. Para agregar datos como columnas separadas en el archivo CSV, necesitas utilizar un objeto con propiedades para cada columna.

    Aquí tienes una versión actualizada de tu código que crea un objeto para cada fila y lo agrega al archivo CSV utilizando Export-Csv:

    $props=[ordered]@{
        ServerName=''
        SystemFolderPath=''
        IdenityReference=''
        FileSystemRights=''
    }
    New-Object PsObject -Property $props | Export-Csv "C:\status_report.csv" -NoTypeInformation
    
    $serverlist = Get-Content -Path "C:\ServerList.txt"
    
    foreach ($server in $serverlist) {
        $paths_list = $env:Path -Split ';'
        foreach ($sys_Path in $paths_list) {
            $Permissions = Get-Acl -Path $sys_Path
            $Users_Permissions = $Permissions.Access | Where-Object {$_.IdentityReference}
            foreach ($user in $Users_Permissions) {
                $row = New-Object PsObject -Property @{
                    ServerName = $server
                    SystemFolderPath = $sys_Path
                    IdenityReference = $user.IdentityReference.Value
                    FileSystemRights = $user.FileSystemRights
                }
                $row | Export-Csv -Path "C:\status_report.csv" -Append -NoTypeInformation -Force
            }
        }
    }
    

    Este código crea un nuevo objeto para cada fila con propiedades para cada columna en el archivo CSV. Luego utiliza Export-Csv para agregar el objeto al archivo CSV. Ten en cuenta que estamos utilizando el parámetro -Append para agregar cada fila al archivo sin sobrescribir los datos existentes.

Comments are closed.