1

I need to run a PowerShell script from Task scheduler for SharePoint online. The script :

  1. Gets the Document version from a https://xxx.sharePoint.com/site/ggg.
  2. Outputs to a .csv file on local drive.
  3. Acccess https://xxx-admin.sharepoint.com to post the CSV to a xxx.Sharepoint.com/site/aaaa.

The script works perfectly in PowerShell. I can't get it to run off my machine. There is no error message, or output file on C:/.

Can this script be run of my local machine?

Task Scheduler Settings

enter image description here

Heidi
  • 45
  • 2
  • 9
  • 1
    Are you saying that it runs interactively on your machine, but it doesn't run in task scheduler on your machine? Please try and be specific because we can't see what you see – Nick.Mc Apr 24 '20 at 07:10
  • It runs interactively on my machine, the report is generated, outputs to a .csv, .csv is added to a c:\ and then uploaded to SharePoint online. When I try and run from the Task Scheduler there are no errors and no outputs. – Heidi Apr 24 '20 at 07:45
  • What does the task scheduler log say? How long does it run for? The issue is usually that it is running under an account that has insufficient rights – Nick.Mc Apr 24 '20 at 07:56
  • Thanks Nick. Found log files here at C:\Windows\System32\winevt\Logs, most recent log is : Engine state is changed from None to Available. Details: NewEngineState=Available PreviousEngineState=None SequenceNumber=13 HostName=ConsoleHost HostVersion=5.1.18362.752 HostId=c12140e9-de5d-4ea5-8f9d-500fc66d8d23 HostApplication=Powershell.exe -ExecutionPolicy Bypass C:\x\xx\xx\x\VersionHistory.ps1 EngineVersion=5.1.18362.752 RunspaceId=6232fc05-7e9b-4a1a-9870-2b579f7c1373 PipelineId= CommandName= CommandType= ScriptName= CommandPath= CommandLine= – Heidi Apr 24 '20 at 11:35
  • It takes a few seconds to run. I am able to run it in SharePoint Online Management Shell - (Run ISE as Administrator). Would Privileges restrict the script from running the report or uploading a file to SharePoint? – Heidi Apr 24 '20 at 11:38
  • If it runs interactively as Administrator, then I suggest you try setting the "run under" account in task scheduler as an administrator and see if that fixes it. (It's not a good permanent solution though) – Nick.Mc Apr 24 '20 at 11:47
  • Updated original post with an image of Settings. Do not have the option to Change User or Group to anything other than myself. – Heidi Apr 24 '20 at 12:13
  • Perhaps you could try this approach to try and capture some output from powershell http://webofwood.com/2013/08/21/httpwww-webofwood-comp339/ – Nick.Mc Apr 25 '20 at 04:30

2 Answers2

4

To run PowerShell scripts you need to do one of two things. Change the Execution policy to either "RemoteSigned" (script must be created on this machine) or "Unrestricted" (not recommend), because by default it is set to "Restricted" which will not run any scripts. The second option ignores the execution policy. To start you open up\edit your original powershell script and encapsulate it with this syntax "powershell -ExecutionPolicy ByPass { script code goes here } " Also, if you want to see if an error is generated when you run the scheduled task then you need to add this argument "-NoExit". The reason the console closes is because by default the scheduled task runs the script in a background console and this closes right after and you need the "-NoExit" command to keep it open. However, if you leave the "-NoExit" argument then it will keep open all of these consoles each time the task runs. So make sure you remove it when you are sure the script is running successfully and without errors.

Encapsulated script syntax example:

powershell -ExecutionPolicy ByPass {

   # Original script code here
}

Encapsulated script that keeps the console from closing syntax example:

powershell -ExecutionPolicy ByPass -NoExit {

   # Original script code here
}

Creating A Scheduled Task:

  1. Open Task Scheduler by pressing "Windows Key + R" this will bring up the run dialog and in the "Open" text-box type "taskschd.msc"

  2. Click "Create Task" and type in the NAME field the name you want to give this task. Then determine security options you want to use, to run as administrator use the "Run with highest privileges" option.

  3. Click "Triggers" tab and then click "New". This is where you choose what begins the task by choosing from the drop down, by default the on a schedule is selected. Then you choose the frequency, times and other advance settings you want.

  4. Click "Actions" tab then click "New" and for actions leave as "Start a program" in the drop-down. In the "Program/script" text-box type "powershell.exe" and in the "Add arguments (option)" field type -File "FULL FILE PATH" and add the opening and closing quotation marks too. example: -File "C:\Users\Public\Documents"

  5. Click "Conditions" tab and leave the defaults for the most part or select other conditions.

  6. Click "Settings" tab and normally the default is fine but you can choose other settings if you like.

https://blog.netwrix.com/2018/07/03/how-to-automate-powershell-scripts-with-task-scheduler/

  • Thanks Larry, great instructions. Script updated but still no output, here is a link to my code in GitHub if that helps at all .https://github.com/wishmouse/PowerShell/blob/master/Stackoverflow – Heidi Apr 24 '20 at 09:45
  • How are you running the script (ISE, PS console or Task Scheduler)? Do you have permissions to write to C:\ and the SharePoint site? Can I see the source code? – Larry Sigley Apr 24 '20 at 09:46
  • source code is here: github.com/wishmouse/PowerShell/blob/master/Stackoverflow I created / ran the script in SharePoint Online Management Shell - (Run ISE as Administrator) and want to make this a scheduled task using Task Scheduler. Output is c:/users/[ me ]/documents/NRG/VersionHistory.csv – Heidi Apr 24 '20 at 10:01
  • Thank you for the link to the source code. I thinking it may be an access issue because Windows keeps profiles separated for security reasons and other profile (users) need to be granted permissions. When a PS console is launched as Admin it is opened under the admin profile. I would launch the PowerShell console as Admin and run this command ```Test-Path -Path C:\Users\wishmouse\Documents\NRG\``` to make sure you get a true value returned. Also, I noticed in the code there is no "Test-Path" being done on any of the hard coded file paths, I would test those manually too. – Larry Sigley Apr 24 '20 at 17:40
  • The first path was turning a false. Changed it as follows and got the following : **Accessing the path :** PS C:\WINDOWS\system32> Test-Path -Path C:\Users\versionhistory\ True **Accessing the file in the path:** PS C:\WINDOWS\system32> Test-Path -Path C:\Users\versionhistory\versionhistory.ps1 False – Heidi Apr 25 '20 at 04:09
  • When you get a FALSE from using the Test-Path command. This means it was unable to find that location/file you have give. So in the case of C:\User\versionhistory\versionhistory.ps1. It means that PowerShell was unable to find the script "versionhistory.ps1" in the folder "versionhistory" under "C:\Users". So in short all paths being used must lead to a correct and valid path or file. So I would always ise the Test-Path command to check if the path or file you want to read or write exists. You can create them if they do not exist. – Larry Sigley Apr 25 '20 at 07:59
0

Not really sure, how you run it, but to answer your question: Yes, it can if you have PowerShell and all necessary libraries installed. You may need to set Execution policy, if you haven't yet and run the script with user that has enough permissions.

Check these questions and responses: How to run a PowerShell script

En.Kidu
  • 1
  • 2