foreach - Testing to see if a scheduled task exist in powershell -
i can't figure out why below code won't work:
function createfirefoxtask() { $schedule = new-object -com schedule.service $schedule.connect() $tasks = $schedule.getfolder("\").gettasks(0) foreach ($task in ($tasks | select name)) { echo "task: $task.name" if($task.equals("firefoxmaint")) { write-output "$task exists" break } } } createfirefoxtask
the output this:
firefoxmaint task: @{name=firefoxmaint}.name task: @{name=task1}.name task: @{name=task2}.name task: @{name=task3}.name task: @{name=task4}.name task: @{name=task5}.name
if echo $task.name
shell without going through script, displays name.
when used in double-quoted string, variable evaluation stop @ punctuation. can use $()
denote subexpression within string, this:
"task: $($task.name)"
powershell evaluate expression inside parentheses , expand string outcome
Comments
Post a Comment