0
votes

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.

2
[1] that script containing a function has no call to run anything. how are you calling it? dot sourcing only loads it into your script, it does not run it unless there is a call that runs the code. ///// [2] does the function run on its own - with the required inputs provided? - Lee_Dailey
[1] Drop the space in the path (Test-Path ' C:\Temp\filelist.csv'). [2] Since the script is stored in the C:\Temp folder, it is likely that your AV software prevents it from running or maybe a Group Policy blocks you there. [3] DON'T USE $input as a self declared variable, because in PowerShell this is an Automatic variable. [4] Who is running the scheduled task? Does that user have permissions to do so? (check out -ExecutionPolicy) - Theo
Thank you and I fixed space and variable name as you suggested. Since I created the scheduled task, I assume it is run under my name. There is no AV software installed on the server. Also there are other scheduled tasks that are running fine. - signalhouse
Edited the question and added the arguments that I am passing to the function. - signalhouse
Does not work means you haven't undertaken any troubleshooting. What does the windows scheduler log and windows event log say? - Nick.McDermaid

2 Answers

1
votes

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

0
votes

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