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.
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.

(Test-Path ' C:\Temp\filelist.csv'). [2] Since the script is stored in theC:\Tempfolder, it is likely that your AV software prevents it from running or maybe a Group Policy blocks you there. [3] DON'T USE$inputas 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