Score:0

Executing a PowerShell Script With Spaces in Arguments via Task Scheduler on Windows Server 2012

br flag

I'm attempting to create a scheduled task on Windows Server 2012 that will execute a powershell script that will create an archive of a specific directory once per day.

The arguments to execute the script should be as follows (via command line): .\CreateBackup.ps1 -BackupTarget "D:\SOFT_DIST\Production\_New_Build\Documentation\Windows Build Documentation\" -BackupDestination "D:\SOFT_DIST\Production\_New_Build\Documentation\Build Documentation Backups\Backups\" -DeleteOutdatedFiles $true -FileTTLDays 30

The script works perfectly fine, however the issue is when I input the arguments into Task Scheduler, I receive an error stating "D:\SOFT_DIST\Production_New_Build\Documentation\Build is not recognized as the name of a cmdlet, function, script file, or operable program." . Keep in mind that in actuality , the directories are valid.

In Task Scheduler, this is what I have entered under Actions (pulled from the XML export file)

<Command>powershell.exe</Command>
  <Arguments>
    -ExecutionPolicy Bypass "D:\SOFT_DIST\Production\_New_Build\Documentation\Build Documentation Backups\CreateBackup.ps1" 
    -BackupTarget 'D:\SOFT_DIST\Production\_New_Build\Documentation\Windows Build Documentation\'
    -BackupDestination 'D:\SOFT_DIST\Production\_New_Build\Documentation\Build Documentation Backups\Backups\' 
    -DeleteOutdatedFiles $true
    -FileTTLDays 30
  </Arguments>

Based on this, what should I fix to get this script to execute via a Scheduled Task? I believe the issue lies with spaces in the directory paths.

br flag
[1] have you tried adding an outer set of single quotes? something like `'"d:\ThingOne\Phrase With Embedded Spaces"'`. note the outer quotes are singletons - so it goes `single quote`, `double quote`, `string`, `double quote`, `single quote`. ///// [2] are you allowed to get rid of the [annoyingly problematic] spaces?
Daniel avatar
br flag
@Lee_Dailey unfortunately I can't get rid of them. I tried what you suggested, but no luck: `powershell.exe -ExecutionPolicy Bypass '"D:\SOFT_DIST\Production\_New_Build\Documentation\Build Documentation Backups\CreateBackup.ps1"' -BackupTarget '"D:\SOFT_DIST\Production\_New_Build\Documentation\Windows Build Documentation\"' -BackupDestination '"D:\SOFT_DIST\Production\_New_Build\Documentation\Build Documentation Backups\Backups\"' -DeleteOutdatedFiles $false -FileTTLDays 30`
br flag
arg! [*frown*] have you tried putting the `powershell.exe` in the action slot named `program/script` and then putting the entire argument line into the action slot named `add arguments`? folks often put the entire thing in the `program/script` slot ... and that has problems.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.