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

Bret Wilcock 21 Reputation points
2020-11-20T14:31:05.533+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,566 Reputation points
    2020-11-23T20:30:10.377+00:00

    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> 
    
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Rich Matheisen 44,696 Reputation points
    2020-11-20T15:54:22.403+00:00

    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.

    0 comments No comments

  2. Bret Wilcock 1 Reputation point
    2020-11-20T16:20:57.193+00:00

    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


  3. Bret Wilcock 21 Reputation points
    2020-11-23T09:38:31.123+00:00

    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


  4. Bret Wilcock 21 Reputation points
    2020-11-24T08:11:18.527+00:00

    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