Search code examples
powershellcsvexport-to-csv

Update Status Field on CSV and Export again


I'm developing a script for creating a navigation in SharePoint with PnP. For this purpose I created a CSV with my URLS and some addional informations.

My CSV looks like this:

Name,ParentName,Level,URL,Status,Order
Tools,Consulting,1,http://linkless.header/,Existing,3
Tool Wiki,Tools,2,http://linkless.header/,Existing,3
Tool Box,Tools,2,http://linkless.header/,add,2
Tool Development,Tools,2,https://linkless.header/,Remove,1

I import the CSV with following command

$CURRENT_DIRECTORY=Get-Location
$CSV_Navigation = Import-Csv $CURRENT_DIRECTORY\UrlsNavigation.csv 

Then I want to make a few changes and export it again.

$STATUS_ADD="Add"
$STATUS_REMOVE="Remove"
$STATUS_EXISTING="Existing"

function setStatusInCSV {
    ForEach($Item in $CSV_Navigation) {
        If ($Item.Status -eq $STATUS_ADD) {
            $Item.Status = $STATUS_EXISTING
        }   
    }
    Export-Csv $CSV_Navigation -Path $CURRENT_DIRECTORY\UrlsNavigation.csv -NoTypeInformation 
}

For this I get the following Error

Export-Csv : Cannot convert 'System.Object[]' to the type 'System.Char' required by parameter 'Delimiter'. Specified method is not supported.
At Path\Navigation\UpdateNavigation.ps1:95 char:16
+     Export-Csv $CSV_Navigation -Path $CURRENT_DIRECTORY\UrlsNavigatio ...
+                ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.ExportCsvCommand

If I run the command with a delimiter it looks like this:

Export-Csv : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At Path\Navigation\UpdateNavigation.ps1:95 char:5
+     Export-Csv $CSV_Navigation -Path $CURRENT_DIRECTORY\UrlsNavigatio ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

My $CSV_Navigation looks like this

Write-Host $CSV_Navigation 
@{Name=Tools; ParentName=Consulting; Level=1; URL=http://linkless.header/; Status=Ex
isting; Order=3} @{Name=Tool Wiki; ParentName=Tools; Level=2; URL=http://linkless.header/; Status=Existing; Order=3} @{Name=Tool Box; ParentName=Tools; Level=2; URL=http://linkless.header/; Status=Existing; Order=2} @{Name=Tool Development; ParentName=Tools; Leve
l=2; URL=https://linkless.header/; Status=Remove; Order=1} 

Solution

  • I think the problem here is the positional parameter of this command.

    you need to define what your input objetc is. Like this:

    Export-Csv -InputObject $CSV_Navigation -Path $CURRENT_DIRECTORY\UrlsNavigation.csv -NoTypeInformation 
    

    Edit: You need to use Select-Object -Property YourPropeerties to get the right output or pipe your array to the command:

    $CSV_Navigation | Export-Csv -Path $CURRENT_DIRECTORY\UrlsNavigation.csv -NoTypeInformation