【发布时间】:2021-10-15 22:19:18
【问题描述】:
我正在设置计划任务以注销断开的会话。我使用下面显示的参数打开 PowerShell.exe 来设置任务
powershell.exe -ExecutionPolicy Bypass -NoProfile -command "Invoke-command -ScriptBlock {quser | Select-String "Disc" | ForEach {logoff ($_.tostring() -split " +")[2]}}"
该参数在 PowerShell 中单独起作用,但是当我尝试创建此任务时,它失败并出现错误:
powershell.exe : At line:1 char:89
At line:2 char:1
+ powershell.exe -ExecutionPolicy Bypass -NoProfile -command "Invoke-co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (At line:1 char:89:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ ... {quser | Select-String Disc | ForEach {logoff (((.tostring() -split ...
+ ~
An expression was expected after '('.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
我相信我需要对表达式进行评估以正确填充字符串,并尝试转义引号和括号以使其正常工作。任何帮助将不胜感激。
【问题讨论】:
-
在我看来, ($_.tostring() -split ' +')[2] 没有被理解为来自 quser 的管道对象的表达式。还在做一些测试。
-
powershell.exe -command "(quser | select-string 'Disc').count" 返回对象的计数,但 powershell.exe -command "quser | select-string 'Disc' | foreach- object {$_} 不返回任何对象。我很困惑
标签: powershell expression scheduled-tasks quotes