Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $invocation = (Get-Variable MyInvocation).Value
- $directorypath = Split-Path $invocation.MyCommand.Path
- $path = $directorypath + "\"
- # Definisci il nome del file di input e output
- $inputFile = $path + "creazione_nuovi_utenti.csv"
- $outputFile = $path + "Creazione_Nuovi_Utenti_BONIFICATO.csv"
- # Leggi il file CSV usando ';' come delimitatore
- $data = Import-Csv -Path $inputFile -Delimiter ';'
- # Crea un oggetto DataTable per manipolare i dati
- $table = New-Object System.Data.DataTable
- # Aggiungi le colonne al DataTable
- $data[0].PSObject.Properties | ForEach-Object {
- $table.Columns.Add($_.Name) | Out-Null
- }
- # Aggiungi le righe al DataTable
- $data | ForEach-Object {
- $row = $table.NewRow()
- $_.PSObject.Properties | ForEach-Object {
- $row[$_.Name] = $_.Value
- }
- $table.Rows.Add($row) | Out-Null
- }
- # Elenco delle colonne da rimuovere
- $columnsToRemove = @()
- # Identifica le colonne da rimuovere
- $table.Columns | ForEach-Object {
- $column = $_
- $isEmpty = $true
- foreach ($row in $table.Rows) {
- if (![string]::IsNullOrEmpty($row[$column.ColumnName])) {
- $isEmpty = $false
- break
- }
- }
- if ($isEmpty) {
- $columnsToRemove += $column.ColumnName
- }
- }
- # Rimuovi le colonne identificate
- foreach ($column in $columnsToRemove) {
- $table.Columns.Remove($column) | Out-Null
- }
- # Rimuovi le righe vuote
- $data = $data | Where-Object { $_.PSObject.Properties.Value -ne $null -and $_.PSObject.Properties.Value -ne '' }
- # Scrivi i dati puliti nel file di output
- $data | Export-Csv -Path $outputFile -Delimiter ';' -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement