Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quoting a parameter when running a powershell script from task scheduler

Status
Not open for further replies.

ls62

Programmer
Oct 15, 2001
177
0
0
US
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

 
I think it may have to do with how you setup your task - which you didn't specify.
see https://superuser.com/questions/270...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

FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top