signalhouse
signalhouse

Reputation: 83

PowerShell function does not work from Windows Task Scheduler

I am calling a function from a PowerShell script to update some files on a remote server. The script works fine and calls the function as expected when I run it from the ISE. But the function does not get called when I run the script using a windows task scheduler. Not sure what I am supposed to do in this case. I dot source the function to call the function from the script.

 . “c:\temp\updatefiles.ps1” Monitoring  CUSTOMER Approved

even I tried this.

 . 'c:\temp\updatefiles.ps1' Monitoring  CUSTOMER Approved

and here's the function.

Function Monitoring($NAME, $status) {

  $REMOTE_SERVER = "sasipt01"
   # change the status on the REMOTE Server - sasipt01.

   # Check if the file exists on the REMOTE Server.
    $Chk = Invoke-Command -ComputerName "$REMOTE_SERVER" -ScriptBlock {if 
      (Test-Path ' C:\Temp\filelist.csv') {'True'} else {'False'}} 

     if ($Chk = 'True') {

       # File exists on the server.

        $Input = Invoke-Command -ComputerName "$REMOTE_SERVER" -ScriptBlock 
                {Import-Csv 'C:\Temp\filelist.csv'}
        $Output = foreach ($i in $input) { 
          if ($i.Process_Instance -match "$NAME") {$i.Status = "$status"} $i } 

          $OutArray = $Output | Select-Object -Property * -ExcludeProperty 
                                    PSComputerName, RunspaceId, PSShowComputerName
          $OutArray | Invoke-Command -ComputerName "$REMOTE_SERVER" -ScriptBlock 
          {Export-Csv 'C:\Temp\filelist.csv' -NoTypeInformation}
                   }
                }

updatefiles.ps1 is called from the main script (start.ps1) which is executed from a windows task scheduler.

enter image description here

I also added the function on the profiles (AllUsersAllHosts and AllUsersCurrentHost) and tried to run the main script from the task scheduler. But still it did not work.

Upvotes: 0

Views: 1175

Answers (2)

signalhouse
signalhouse

Reputation: 83

I figured out the issue was due to task scheduler which is being run by the local system account and the system account does not have access to the remote server where the csv file resides. I have changed the user account on the task scheduler and it worked

Upvotes: 0

Theo
Theo

Reputation: 61068

Change the start.ps1 script to do the following:

function Monitoring($Name, $Status) {
    # your code here to do things with the parameters 

    # for demo, I'll just write them out
    Write-Host $Name
    Write-Host $Status
    Read-Host
}

# call the function with the parameters supplied to the script
Monitoring $args[0] $args[1]

Then in the Task Scheduler, double-click the task, go to the Actions tab and edit the Arguments field to contain this:

-File "C:\Temp\start.ps1" -ArgumentList 'CUSTOMER','Approved' -ExecutionPolicy Bypass

See ExecutionPolicy

In the General tab, you can define who the task user will be and if the task can run without being logged on etc.

P.S. I'd advice to have the code write a new csv file instead of overwriting the original during testing.

Hope this helps

Upvotes: 2

Related Questions