question

BretWilcock-2705 avatar image
0 Votes"
BretWilcock-2705 asked ·

Script works in Powershell ISE but not in using Command Line (Administrator)

Hi

I am trying to download from documents from Sharepoint to a windows folder. The scrip works in ISE but not when running from the command line (using administrator). The script is:-


$SharePointSiteURL = "https://sueryder.sharepoint.com/sites/Document Hub/H%26SC%20Performance%20Management%20Data/"
$O365ServiceAccount = "sharepoint.download@xyz.org"
$O365ServiceAccount1 = "xyz"
[SecureString]$Secure = ConvertTo-SecureString $O365ServiceAccount1 -AsPlainText -Force
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $Secure)

Add-PnPStoredCredential -Name $SharePointSiteURL -Username $PSCredentials.UserName -Password $Secure

Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered ·

The script works in ISE but not when running from the command line (using administrator).


I might be off base with this (and maybe thinking a little too far "out of the box"), but I'm curious about the above statement.

Specifically, what do you mean by "using administrator"? Are you using the same domain account to run Powershell_ISE.exe and Powershell.exe? Are both processes being run with or without UAC elevation? IE "run as administrator"? Are you running both processes interactively? (That is; not using task scheduler.)

If you launch a new instance of Powershell_ISE.exe and execute the .ps1 file from the PS prompt, does it work or fail? Don't load the script into the editor, just run the .ps1 from the prompt.

Or from the ISE prompt run a sub instance of Powershell.exe and have it run the script. Like I did with get-host.

 PS C:\WINDOWS\system32> (Get-Host).Name
 Windows PowerShell ISE Host
    
 PS C:\WINDOWS\system32> powershell.exe "(Get-Host).Name"
 ConsoleHost
    
 PS C:\WINDOWS\system32> 


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

In what way does it not work? Is an error reported? If so, what is it?

When something works in the ISE but not the from the PowerShell shell it's usually something that's been defined (and remembered) by ISE. When you work from the shell nothing is remembered between executions of each script.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BretWilcock-3476 avatar image
0 Votes"
BretWilcock-3476 answered ·

Hi Richard

Many thanks for getting back to me.

This is the error:-

get-pnplist : Object reference not set to an instance of an object.
At C:\Projects\Healthcare\Palliative\Powershell\Capture\Test.ps1:9 char:1
+ get-pnplist
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-PnPList], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,PnP.PowerShell.Commands.Lists.GetList

It has to be something to do with the connect-pnplonline. If I use the parameter -UseWebLogin it works however eventually I need to run a similar script connecting to sharepoint remotely overnight.

Bret

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

There aren't a lot of parameters for the get-pnplist, so you should start by verifying that none that you use is null -- including the "current web" if you aren't using the -Web parameter.

0 Votes 0 ·
BretWilcock-2705 avatar image
0 Votes"
BretWilcock-2705 answered ·

Hi Richard

I may have found the possible reason why ISE is working but command line is not. When I display the connection in both methods, in the Command line the Context "OfficeDevPnP.Core.PnPClientContext" is missing. How can I prevent this happenning?

ISE
HttpClient : System.Net.Http.HttpClient
ConnectionType : O365
InitializationType : Unknown
Scopes :
MinimalHealthScore : -1
RetryCount : 10
RetryWait : 1
PSCredential : System.Management.Automation.PSCredential
ClientId :
ClientSecret :
TelemetryClient : Microsoft.ApplicationInsights.TelemetryClient
Url : https://sueryder.sharepoint.com/sites/Document%20Hub/H%26SC%20Performance%20Management%20Data
TenantAdminUrl :
Certificate :
DeleteCertificateFromCacheOnDisconnect : False
Context : OfficeDevPnP.Core.PnPClientContext
Tenant :
AzureEnvironment : Production

Command Line
HttpClient : System.Net.Http.HttpClient
ConnectionType : O365
InitializationType : Unknown
Scopes :
MinimalHealthScore : -1
RetryCount : 10
RetryWait : 1
PSCredential : System.Management.Automation.PSCredential
ClientId :
ClientSecret :
TelemetryClient : Microsoft.ApplicationInsights.TelemetryClient
Url : https://sueryder.sharepoint.com/sites/Document%20Hub/H&SC%20Performance%20Mana
gement%20Data
TenantAdminUrl :
Certificate :
DeleteCertificateFromCacheOnDisconnect : False
Context :
Tenant :
AzureEnvironment : Production


Many thanks

Bret

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have zero experience with SharePoint, so I'm not going to be a person able to offer much assistance here. You might want to post this question in the Microsoft 365 group(s).

I can only state that there's a difference in the two environments. It may be that the ISE is using an Add-On that's adding the missing elements to the environment. Have you tried using another editor (say, VS Code or Visual Studio)?

Are you using the latest version of the module?

0 Votes 0 ·
BretWilcock-2705 avatar image
0 Votes"
BretWilcock-2705 answered ·

Hi

When I say "run as administrator" with Powershell and you right click on the Start menu you get two options as per the attachment. However depending which option I chose I got differing results whether I was in ISE or Command line.

When I set the Sharepoint url to https://sueryder.sharepoint.com/sites/Document%20Hub/H%26SC%20Performance%20Mana....... in ISE it worked fine but in Command Line the connect-pnponline failed. However I found a post somewhere using Google (I forget where now) that told me that having spaces in the url when using Command Line caused issues. So I replaced spaces with %20 and it still failed. So I replaced %26 with an & and my script started working in both options (ISE and Command Line).

Hope that explains the problem and the resolution.

Many thanks

Bret

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is this the problem description you found? 1250

The last entry says "Fixed in next release", but that was 18 months ago! 3.0.1808.1 was one of the reported released with this problem. 3.9.1905.3 looks to be the current version. Is that the one you're using?

0 Votes 0 ·
BretWilcock-2705 avatar image
0 Votes"
BretWilcock-2705 answered ·

It certainly looks like it could be the solution. However my release says "SharePointPnPPowerShellOnline 3.26.2010.0".

Not to worry I have a solution now. many thanks for your help

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.