【发布时间】:2021-08-26 15:23:44
【问题描述】:
我正在使用下面的 PowerShell 代码来删除数据库服务器路径中的日志
powershell.exe -command & {
get-childitem -path "$(ESCAPE_SQUOTE(D:\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\))" -filter *_*_*_*.txt |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | remove-item -verbose
}
它的错误如下所示
At line:1 char:25
+ powershell.exe -command & {get-childitem -path "$(ESCAPE_SQUOTE(D:\Microsoft SQL ...
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
我是 PowerShell 新手,我认为将 "" 添加到 "&" 就足够了,但它又会出错
powershell.exe : ScriptBlock should only be specified as a value of the Command parameter.
At line:1 char:1
+ powershell.exe -command "&" {get-childitem -path "$(ESCAPE_SQUOTE(D:\Microsoft S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], ParameterBindingException
+ FullyQualifiedErrorId : IncorrectValueForCommandParameter
在路径中添加了"",但没有帮助
powershell.exe -command & {get-childitem -path "$(ESCAPE_SQUOTE("D:\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log\"))" -filter *_*_*_*.txt | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | remove-item -verbose}
我需要从操作系统命令 exec 下的 SQL 代理作业中调用它
谁能帮我看看语法
问候和祝福 埃本
【问题讨论】:
-
您需要将整个表达式用引号括起来:
powershell -command "& { ... }"
标签: sql-server powershell