测试以查看 powershell 中是否存在计划任务 [英] Testing to see if a scheduled task exist in powershell

查看:31
本文介绍了测试以查看 powershell 中是否存在计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么下面的代码不起作用:

I can't figure out why the 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 already exists"
         break
      }
   }
} 
createFirefoxTask

我得到的输出是这样的:

The output I get is 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

如果我在没有通过脚本的情况下从 shell 回显 $task.name,它会正确显示名称.

If I echo $task.name from the shell without going through the script, it properly displays the name.

推荐答案

在双引号字符串中使用时,变量计算将在标点符号处停止.您可以使用 $() 表示字符串中的子表达式,如下所示:

When used in a double-quoted string, variable evaluation will stop at punctuation. You can use $() to denote a subexpression within a string, like this:

"TASK: $($task.name)"

PowerShell 将计算括号内的表达式并用结果扩展字符串

PowerShell will then evaluate the expression inside the parentheses and expand the string with the outcome

这篇关于测试以查看 powershell 中是否存在计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆