5

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

8
  • 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 Commented Apr 24, 2020 at 7: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. Commented Apr 24, 2020 at 7: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 Commented Apr 24, 2020 at 7: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= Commented Apr 24, 2020 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? Commented Apr 24, 2020 at 11:38

2 Answers 2

10

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/

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Larry, great instructions. Script updated but still no output, here is a link to my code in GitHub if that helps at all .github.com/wishmouse/PowerShell/blob/master/Stackoverflow
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?
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
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.
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
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.