pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
989 stars 662 forks source link

using Get-PnPFile and Set-PnPListItem in the same script errors out #2126

Open kritschg opened 5 years ago

kritschg commented 5 years ago

Notice: many issues / bugs reported are actually related to the PnP Core Library which is used behind the scenes. Consider carefully where to report an issue:

  1. Are you using Apply-SPOProvisioningTemplate or Get-SPOProvisioningTemplate? The issue is most likely related to the Provisioning Engine. The Provisioning engine is not located in the PowerShell repo. Please report the issue here: https://github.com/officedev/PnP-Sites-Core/issues.
  2. Is the issue related to the cmdlet itself, its parameters, the syntax, or do you suspect it is the code of the cmdlet that is causing the issue? Then please continue reporting the issue in this repo.
  3. If you think that the functionality might be related to the underlying libraries that the cmdlet is calling (We realize that might be difficult to determine), please first double check the code of the cmdlet, which can be found here: https://github.com/OfficeDev/PnP-PowerShell/tree/master/Commands. If related to the cmdlet, continue reporting the issue here, otherwise report the issue at https://github.com/officedev/PnP-Sites-Core/issues

Reporting an Issue or Missing Feature

Please confirm what it is that your reporting

Expected behavior

Please describe what output you expect to see from PnP-PowerShell Cmdlets

Actual behavior

Please describe what you see instead. Please provide samples of HTML output or screenshots

Steps to reproduce behavior

Please include complete code samples in-line or linked from gists

Which version of the PnP-PowerShell Cmdlets are you using?

What is the version of the Cmdlet module you are running?

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable)

How did you install the PnP-PowerShell Cmdlets?

ghost commented 5 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

kritschg commented 5 years ago

using Get-PnPFile and Set-PnPListItem in the same script throws the following error: format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

the script initially checked a sharepoint list item, if the status was set to a certain value it would download the attachment, upload it to azure blob storage and set the list item to a new status.

The workaround is to do the download/upload in one script and in a separate script to the list item update

here is the code, originally there was one foreach loop but I separated them to troubleshoot the issue: Connect-AzAccount -Credential $creds -Subscription "Cloud Trust NonProd" $ResourceGroup = Get-AzResourceGroup -Name $StorageAccount = Get-AzStorageAccount -Name -ResourceGroupName $ResourceGroup.ResourceGroupName $ctx = $StorageAccount.Context $container=Get-AzStorageContainer -Name "networkdiagrams" -Context $CTX Connect-PnPOnline -Url -Credentials $creds

$list = Get-PnPList SiteNetworkDiagrams $items = $list | Get-PnPListItem

foreach ($item in $items) { $item.FieldValues.Title switch ($item.FieldValues.Status) { "Uploaded"{"do nothing"} "New" { "new"

Set-PnPListItem -List "SiteNetworkDiagrams" -Identity $item.Id -Values @{"Status" = "Uploaded"}

        $attachments = ForEach-Object{Get-PnPProperty -ClientObject $item -Property "AttachmentFiles"}  
        Get-PnPFile -Url $attachments.ServerRelativeUrl -FileName $attachments.FileName -Path "E:\NetDiagrams\" -AsFile  -Force
        $filename = $item.FieldValues.Title + ".png"
        $filepath = "E:\NetDiagrams\" + $attachments.FileName
        Set-AzStorageBlobContent -File $filepath -Container $container.Name -AsJob -Blob $filename -Context $ctx -Confirm:$false -Force
        }
    "Updated"
        {
        "updated"
        #Set-PnPListItem -List "SiteNetworkDiagrams" -Identity $item.Id -Values @{"Status" = "Uploaded"}
        $attachments = ForEach-Object{Get-PnPProperty -ClientObject $item -Property "AttachmentFiles"}  
        Get-PnPFile -Url $attachments.ServerRelativeUrl -FileName $attachments.FileName -Path "E:\NetDiagrams\" -AsFile  -Force
        $filename = $item.FieldValues.Title + ".png"
        $filepath = "E:\NetDiagrams\" + $attachments.FileName
        Set-AzStorageBlobContent -File $filepath -Container $container.Name -AsJob -Blob $filename -Context $ctx -Confirm:$false -Force
        }
    }
}

Connect-PnPOnline -Url https:// -Credentials $creds $list = Get-PnPList SiteNetworkDiagrams $items = $list | Get-PnPListItem foreach ($item in $items) { $item.FieldValues.Title switch ($item.FieldValues.Status) { "Uploaded"{"do nothing"} "New" { "new" Set-PnPListItem -List "SiteNetworkDiagrams" -Identity $item.Id -Values @{"Status" = "Uploaded"} } "Updated" { "updated" Set-PnPListItem -List "SiteNetworkDiagrams" -Identity $item.Id -Values @{"Status" = "Uploaded"} } } }