0

I am trying to schedule a PowerShell Core 7.2 script to run on Windows Server 2012R2. The script runs manually, without any errors, from the server and the Task Scheduler runs the task. In the History, I can see Task Completed

The issue is that the script is not executed. It is supposed to move the files and files are not moving which means the script was not executed.

Settings of the Task Scheduler that are selected are as follows:

General - Run whether user is logged on or not, Run with the highest privileges.
Actions -> Action Start a Program Actions -> Program/Script "C:\Program Files\PowerShell\7\pwsh.exe" (location of pwsh.exe)
Actions -> Add Arguments -Executionpolicy Bypass -File "R:\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\CNC_File_Transfer_Fastems.ps1"
Location -> Name of the local machine

I am not really sure what is going wrong here.

I am thinking there is an issue with the script. Because there is another script set up to be executed with PS Core and Task Scheduler. I am going to post the script here. It is a simple batch file that moves all the contents of one folder from one server to another. I achieve this in two functions. Function MoveFiles moves all the contents of the parent folder(excluding the subfolder called "Mazak"). The second function,Function MoveMazakFiles function moves the contents of "Mazak" only. ( I am completely aware I could have done this using fewer lines of code but that is not the point here)

Code:

$logPath = "\\MMS25163S1\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\Log.txt"
$trancriptPath = "\\MMS25163S1\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\LogTranscript.txt"
$getDate = Get-Date -Format "dddd MM/dd/yyyy HH:mm "
$counter = 0
$mazakCounter = 0
Start-Transcript -Path $trancriptPath -Append
Add-Content -Path $logPath -Value ("LOG CREATED $getDate") -PassThru
#Sources 
$srcMca = "\\MMS25163S1\Public\NcLib\FromNC\*"
$srcMcaNameChg ="\\MMS25163S1\Public\NcLib\FromNC"
$srcMazak= "\\MMS25163S1\Public\NcLib\FromNC\Mazak\*"
$srcMcaNameChgMazak = "\\MMS25163S1\Public\NcLib\FromNC\Mazak"
#Destination 
$destMca = "\\Sidney2\MfgLib\RevisedPrograms\MC-A"
#Time with milliseconds
$time = (Get-Date -Format hh-mm-fff-tt).ToString() 

Function MoveFiles{
    Param(
        [string]$src,
        [string]$dest,
        [string]$srcNameChange
    )
   Get-Item -Path $src -Exclude *Mazak* -ErrorAction SilentlyContinue | ForEach-Object{
        $counter++
        $fileName = $_.BaseName
        $fileNameExt = $_.Name
        Write-host $fileName -ForegroundColor Green
        Rename-Item -Path "$srcMcaNameChg\$fileNameExt"  -NewName ($fileName+"_"+"(Time-$time)"+$_.Extension);
        Add-Content -Path $logPath -Value ("Name changed: Time stamp added to $fileName ") -PassThru
    }
    Move-Item -Path $src -Exclude *Mazak*  -Destination $dest -Force
   Add-Content -Path $logPath -Value ("$counter file(s) moved to $dest") -PassThru
} 
MoveFiles -src $srcMca -dest $destMca -srcNameChange $srcMcaNameChg

Function MoveMazakFiles{
    Param(
        [string]$srcMazak,
        [string]$dest,
        [string]$srcNameChange
    )
    Get-ChildItem $srcMazak -Recurse -ErrorAction SilentlyContinue | ForEach-Object{
        $mazakCounter++
        $fileName = $_.BaseName
        $fileNameExt = $_.Name
        Write-host $fileName -ForegroundColor Green
        Rename-Item -Path "$srcMcaNameChgMazak\$fileNameExt"  -NewName ($fileName+"_"+"(Time-$time)"+$_.Extension);  
    }
    Move-Item -Path $srcMazak  -Destination $dest -Force
    Add-Content -Path $logPath -Value ("$mazakCounter file(s) from Mazak folder moved to $dest") -PassThru
}
MoveMazakFiles -srcMazak $srcMazak -dest $destMca -srcNameChange $srcMcaNameChg

Stop-Transcript
TylerH
  • 20,799
  • 66
  • 75
  • 101
Brute
  • 121
  • 1
  • 10
  • Please [enable task history](https://stackoverflow.com/a/14651161/7571258) and run the PS script again. Afterwards have a look into the history tab of the task to see if you get any errors. You could also do some logging from the script itself. – zett42 Apr 14 '22 at 21:38
  • @zett42 The task history is enabled and there are no errors – Brute Apr 14 '22 at 21:50
  • I'm testing this on Windows 10 and PS Core 7.2, and I don't see "Location" in Task Scheduler. Is that a tab same as "General", "Trggers", "Actions", etc...? Mine is working with trigger set to every 2 minutes. Script is just placing a timestamp in a log file, and new entry showing every 2 minutes. If "Location" is left blank, does your's script run? In most cases, not specifying a computer will default to the current computer. – Darin Apr 15 '22 at 03:16
  • And what do you have for "*When running the task, use the following user account:*", System? --> see: [Scheduled Task Powershell Script - Runs OK as user account, but not as SYSTEM](https://stackoverflow.com/a/51612478/1701026) – iRon Apr 15 '22 at 07:08
  • Is there any entry at all in the task history? If not, does it work when you manually start the task? Then the problem lies with the trigger. – zett42 Apr 15 '22 at 10:52

1 Answers1

0

When setting the scheduled task, under Action -> Start a Program -> Program/Script. Call powershell and pass the script as the parameter

Like

powershell -File "R:\Public\IT\Vantage_Utilities\CNC_Scripts\File Transfer\Fastems\CNC_File_Transfer_Fastems.ps1"

swani14
  • 34
  • 3