Automate Powershell command with task scheduler

I need to run an automated power shell script.

It downloads a file from the the website and places it in the correct folder on the network drive.

I got the script to run manually by right-clicking and run with powershell but haven't had any luck automating. I've attempted to setup in task scheduler but it hasn't worked.

Attached is the script I run.

Any help is appreciated.
LVL 1
bkisielAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
No script attached.

But, this is fairly simple to accomplish.

1.) Create a new task.
2.) The task when triggered should run
 powershell.exe -File "pathToScript"

Open in new window

3.) Be sure that the task is running as an account that has permissions to do whatever need to be accomplished.
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
0
bkisielAuthor Commented:
I attemped both provided solutions and it didn't work for me.
Maybe I'm missing something simple.
See screenshots
Below is the code

Shows successful
  $url = "http://updates.aatrix.com/web/SageMAS/MAS90.exe" 
    $path = "T:\PayrollUpdate\MAS90.exe" 
   # param([string]$url, [string]$path) 
      
    if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
      $path = Join-Path $pwd (Split-Path -leaf $path) 
    } 
      [embed=file 1221139][embed=file 1221140]
    "Downloading [$url]`nSaving at [$path]" 
    $client = new-object System.Net.WebClient 
    $client.DownloadFile($url, $path) 
    #$client.DownloadData($url, $path) 
      
    $path

Open in new window

General.png
Actions.png
0
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
In the powershell\script field, you just put "powershell.exe"
try the whole path: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

About the script, you have a couple of Embed file that shouldn't be there.

try{
    $url = "http://updates.aatrix.com/web/SageMAS/MAS90.exe" 
    $path = "T:\PayrollUpdate\MAS90.exe" 
    # param([string]$url, [string]$path) 
      
    if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
        $path = Join-Path $pwd (Split-Path -leaf $path) 
    } 
    
    ####This line is wrong
    #[embed=file 1221139][embed=file 1221140]
    Write-Host  "Downloading [$url]`nSaving at [$path]" 
    
    $client = new-object System.Net.WebClient 
    $client.DownloadFile($url, $path) 
    $path
}
catch{
    $ErrorMessage = $_.Exception.Message
    Write-Error $ErrorMessage

}

Open in new window

0
David Johnson, CD, MVPOwnerCommented:
you won't get any output from $path
remove line 9 to download the file  [embed file...
if you want to execute the downloaded file
Start-Process -FilePath $path
0
Jose Gabriel Ortega CEE Solution Guide - CEO Faru Bonon ITCommented:
Or a bit more complicated of the script but it contains a progress bar (of course you won't be able to see it in task scheduler)

try{
    $url = "http://updates.aatrix.com/web/SageMAS/MAS90.exe" 
    $path = "T:\PayrollUpdate\MAS90.exe" 
    
    #Progress variables
    $Global:isDownloaded = $false
    [int]$percent=0
    [int]$totalBytes =0
    [int]$receivedByte =0
    Get-EventSubscriber | Unregister-Event
    Get-Job | Remove-Job -Force  | Out-Null

      
    if(!(Split-Path -parent $path) -or !(Test-Path -pathType Container (Split-Path -parent $path))) { 
        $path = Join-Path $pwd (Split-Path -leaf $path) 
    } 
    
    ####This line is wrong
    #[embed=file 1221139][embed=file 1221140]
    Write-Host  "Downloading [$url]`nSaving at [$path]" 
    
    [System.Net.WebClient]$client = new-object System.Net.WebClient 
    $client.DownloadFileAsync($url, $path) 

    Register-ObjectEvent -InputObject $client -EventName DownloadFileCompleted -SourceIdentifier Web.DownloadFileCompleted -Action { $Global:isDownloaded = $True} 
    Register-ObjectEvent -InputObject $client -EventName DownloadProgressChanged -SourceIdentifier Web.DownloadProgressChanged -Action {$Global:Data = $event} 

    Write-Output $path


    While (-Not $isDownloaded) {
        $percent = $Global:Data.SourceArgs.ProgressPercentage 
        $totalBytes = $Global:Data.SourceArgs.TotalBytesToReceive
        $receivedBytes = $Global:Data.SourceArgs.BytesReceived 

        $receivedBytes/= 1kb
        $totalBytes/= 1kb
        If ($percent -ne $null) {
            Write-Progress -Activity ("Downloading {0} from {1}" -f $path,$uri) -Status ("{0} Kbytes \ {1} Kbytes" -f $receivedBytes,$totalBytes)  -PercentComplete $percent
        }
    }



}
catch{
    $ErrorMessage = $_.Exception.Message
    Write-Error $ErrorMessage


}

Open in new window


Using:
https://www.experts-exchange.com/questions/29070576/Powershell-Script-Download-File-and-Show-Progress-Bar.html
0
bkisielAuthor Commented:
I changed the  powershell\script field as suggested.

I also copied the script you left Jose.

It works only if I right-click the file and run with powershell.

The task-scheduler runs that script and doesn't give me any errors. It just says says in the history task completed.
If I go to the top of the task scheduler it says operation completed successfully. The file was never downloaded though. ( I even removed the file before running in case that was a problem)

Not sure where the issue is.

Any help is appreciated.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Use a local path instead of T: for test first - I suspect the task does mot have access to that drive (without special setup).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bkisielAuthor Commented:
I was able to get the script to run locally only. Not sure what the issue is as I am using the domain admin account. I used a batch file and xcopy to copy the file to the T: after download. It is working successfully this way in task scheduler.
Thanks for all the help.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Next step is to try with an UNC path (no drive letter), to see if the mapping is the issue (versus privileges). It's probably just that you cannot share access to T: for an administrative/elevated task.
"Run with highest privlieges", "... whether logged in ..." are two of the settings which can change access to network resources.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.