Home > Enterprise >  POWERSHELL Task Scheduler passing variables inside -Argument
POWERSHELL Task Scheduler passing variables inside -Argument

Time:10-01

Good afternoon, I am very new to Powershell and am trying to achieve the following:

  1. Loop through a folder directory

  2. Set the folder name as a variable

  3. Create a Task

  4. Pass the variable (declared in Step 2) as the required parameter for the -File being called in the Task Action

    Get-ChildItem -Path C:\Users\Paul\Documents\RSYNC -Directory -Recurse |ForEach-Object {
    $FolderName = $_.name
    
    $taskName = 'My Powershell Task_'   $FolderName
    
    # Create Action
    $Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Users\Paul\Documents\RSYNC\Get-LatestAppLog.ps1" -name "$FolderName"' 
    
    # Create Trigger
    $Trigger = New-ScheduledTaskTrigger -Daily -At 12:35am
    
    # Create Settings
    $Settings = New-ScheduledTaskSettingsSet
    
    # Create Task
    $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
    
    # Register Task
    Register-ScheduledTask -TaskName $taskName -InputObject $Task -User 'username' -Password 'password'
    }
    

The tasks are created as desired, however, the problem is that inside the $Action step, instead of passing the folder name inside the $FolderName variable, it is simply passing $FolderName as a string (I hope that makes sense).

How can I correctly pass the folder name to the PowerShell script being called?

CodePudding user response:

Thank you Theo and Abraham Zinala. Both suggested to swap the double and single quotes and that worked perfectly.

CodePudding user response:

  • Your immediate problem was to expect the reference to variable $FolderName to be expanded (interpolated) inside a verbatim PowerShell string literal, '...':

    • Only "..." strings (double-quoted) perform string interpolation in PowerShell: see this answer for an overview of PowerShell's expandable strings (interpolating strings) and this answer for an overview of PowerShell string literals in general.
  • While swapping the use of quotation marks - using "..." for the outer quoting and '...' for the embedded quoting in order to get interpolation may situationally work - depending on the target program or API - it does not work in the context of Task Scheduler.

    • For command lines in Task Scheduler - and generally on Windows from outside PowerShell - you must use "..." quoting for the embedded strings too, which therefore requires escaping " as `" ("" would work too).

    • The reason is that PowerShell doesn't treat ' characters as having syntactic function when its CLI is called from the outside, such as from Task Scheduler, cmd.exe, or the Windows Run dialog (WinKey-R). For instance, if the path passed to -File were 'C:\Users\Paul\Documents\RSYNC\Get-LatestAppLog.ps1', the ' chars. would be interpreted as part of the path.

Specifically:

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument `
 "-File `"C:\Users\Paul\Documents\RSYNC\Get-LatestAppLog.ps1`" -name `"$FolderName`"" 

Note that you could simplify the quoting with the use of an expandable here-string; the embedded " then do not require escaping:

$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument @"
 -File "C:\Users\Paul\Documents\RSYNC\Get-LatestAppLog.ps1" -name "$FolderName"
"@ 
  • Related