1

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
5
  • Please enable task history 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. Commented Apr 14, 2022 at 21:38
  • @zett42 The task history is enabled and there are no errors Commented Apr 14, 2022 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. Commented Apr 15, 2022 at 3: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 Commented Apr 15, 2022 at 7: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. Commented Apr 15, 2022 at 10:52

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.