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í.
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
: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.