admin管理员组

文章数量:1278854

I've got a script that adds a file then reads an excel sheet and then populate a sharepoint document library with that data. The command looks something like

Add-PnPFile -Path $Path -Folder $LibraryName -Values @{"Name" = $Values[0]; "Classification" = $Values[1]; "DocumentID" = $Values[2]; "EffectiveDate" = $Values[3]; "DatePublished" = $Values[4]; "EndDate" = $Values[5]; "RNumber" = $Values[6]}

Everything works fine until one of the date columns needs to be a blank and it's value in the array is blank. Whenever that happens i notice that even though the document gets populated, all the columns are blank. What values can be passed instead of an empty string for the datetimes for this to work?

Edit: do want to add that whenever i replace the blank with a dummy date, it works just fine

I've got a script that adds a file then reads an excel sheet and then populate a sharepoint document library with that data. The command looks something like

Add-PnPFile -Path $Path -Folder $LibraryName -Values @{"Name" = $Values[0]; "Classification" = $Values[1]; "DocumentID" = $Values[2]; "EffectiveDate" = $Values[3]; "DatePublished" = $Values[4]; "EndDate" = $Values[5]; "RNumber" = $Values[6]}

Everything works fine until one of the date columns needs to be a blank and it's value in the array is blank. Whenever that happens i notice that even though the document gets populated, all the columns are blank. What values can be passed instead of an empty string for the datetimes for this to work?

Edit: do want to add that whenever i replace the blank with a dummy date, it works just fine

Share Improve this question edited Feb 24 at 22:19 DanielJ asked Feb 24 at 21:32 DanielJDanielJ 1091 gold badge4 silver badges14 bronze badges 4
  • When you say "blank", do you mean the empty string ('') or $null? – mklement0 Commented Feb 24 at 22:14
  • Tried both $null and '' as well as string::empty. none worked – DanielJ Commented Feb 24 at 22:19
  • You can try to build the hashtable's entries conditionally, up front: only add entries to it for which you have "non-blank" values. – mklement0 Commented Feb 24 at 22:21
  • 1 yup, That's what i ended up doing. Not as quick and dirty as what i initially had but definitely better in the long run. thanks – DanielJ Commented Feb 24 at 22:31
Add a comment  | 

1 Answer 1

Reset to default 1

Ended up creating a hashtable and only passing in the Key/Value if they had data

# Initialize an empty hashtable
        $HashTable = @{}

        # Split into key-value pairs safely
        $Values -split ";" | ForEach-Object {
        if ($_ -match "=") {  # Ensure the pair contains '='
            $Key, $Value = $_ -split "=", 2  # Split into two parts only
            $Key = $Key.Trim()
            $Value = $Value.Trim()

            # Ensure both key and value are valid before adding to hashtable
            if ($Key -ne "" -and $null -ne $Value) {
                $HashTable[$Key] = $Value
                }
            }
        }

        # Debug output
        $HashTable | Format-Table -AutoSize
    
            Add-PnPFile -Path $Path -Folder $LibraryName -Values $HashTable


本文标签: