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,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,656 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. Bret Wilcock 21 Reputation points
    2020-11-25T10:10:15.827+00:00

    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

    0 comments No comments