×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Quoting a parameter when running a powershell script from task scheduler

Quoting a parameter when running a powershell script from task scheduler

Quoting a parameter when running a powershell script from task scheduler

(OP)
I have a PowerShell script that deletes files from a specified folder base on a supplied age and filters. We typically run it as a scheduled task and it works great as long as the suppled path does not contain any spaces. I’m struggling trying to figure out how to run this command from Windows task scheduler and have the file path passed correctly work when it gets to PowerShell.
Hoping someone can give me a suggestion on how to quote this command or parameter (even change the script) so that the $lpath value stays quoted

I can run this command from Powershell itself and it works fine:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -file C:\PSscripts\\RemoveLogfiles_v2.ps1 -days -436 -lprefix BKRRSLT_POC -lpath "\\tanas\epic_misc$\Beaker Results\POC" -filter *.PDF

1) When run from Powershell ISA or cmd line it runs fine
2) When run from task scheduler it seems to strip the quotes as I can see this in the PowerShell event viewer:
HostName=ConsoleHost
HostVersion=5.1.14393.5066
HostId=ad788760-397c-4f63-bca5-6a06de0fcb87
HostApplication=C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe C:\PSscripts\RemoveLogfiles_v2.ps1 -days -436 -lprefix BKRRSLT_POC -lpath \\tanas\epic_misc$\Beaker Results\POC -filter *.PDF
EngineVersion=
RunspaceId=
PipelineId=
CommandName=
CommandType=
ScriptName=
CommandPath=
CommandLine=

I’ve tried various methods of quoting the -lpath parameter in the task scheduler setup, yet if it gets to the script it seems to only see the path up to the space after \\tanas\epic_misc$\Beaker
Here’s actual script (which I have exited early and shortened just for testing purposes)

param (
$days,
$lpath,
$lprefix,
] $filter,
$logpath = "C:\PSLogs"
)

$start = (get-date).AddDays($days).Date

# =============================================
# Setup Logging
# 1. Auto create log folder
# 2. Set the log filename
# =============================================

if(::Exists($logpath))
{
#Do Nothing!!
}
else
{
New-Item -ItemType directory -Path $logpath
}

# Log file time stamp:
$LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
# Log file name:
$LogFile = $logpath+"\"+$lprefix+"_LOG_"+$LogTime+".log"

# ======================================
# Main
# ======================================
#
Write-Host "Output is being logged to: $LogFile"
Write-Output "

Log file deletion
=================

Files in the path
having this filter
older than days (< $start) will be deleted.
" | Out-File $LogFile -Append -Force

#
# Display number of files found
#

cd $lpath

write-output "path is now $lpath" | Out-File $LogFile -Append -Force ########### TEST
pwd | Out-File $LogFile -Append -Force ########### TEST
dir $lpath | Out-File $LogFile -Append -Force ########### TES

$cnt = (Get-ChildItem -Recurse -File -Include $filter | where {$PSItem.LastWriteTime.Date -lt $start}).count
Write-Output "Number of files to be deleted: $cnt
" | Out-File $LogFile -Append -Force

exit ########### TEST

RE: Quoting a parameter when running a powershell script from task scheduler

I think it may have to do with how you setup your task - which you didn't specify.
see https://superuser.com/questions/270643/how-to-pass-an-argument-to-a-windows-scheduled-task-with-spaces-in-it as it may help you.

but idea is that when you setup the parameters you put them all in double quotes - but the internal values that contain spaces you put a further single quote around them.

Regards

Frederico Fonseca
SysSoft Integrated Ltd
www.syssoft-int.com

FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close