从Windows CMD运行PowerShell命令的命令

从Windows CMD运行PowerShell命令的命令,powershell,cmd,Powershell,Cmd,考虑: (Get-Content Rel_DDL.SQL) | ForEach-Object { $_ -replace "SWIFT [\d\.]+", "SWIFT 2.4.0" } | Set-Content Rel_DDL.SQL 上面的PowerShell代码将SQL文件中的SWIFT 2.3.0替换为SWIFT 2.4.0,当我运行PowerShell时,它可以正常工作 我想通过Windows CMD运行PowerShell命令,但出现错误。您可以在CMD Windows中

考虑:

(Get-Content Rel_DDL.SQL) | ForEach-Object { 
  $_ -replace "SWIFT [\d\.]+", "SWIFT 2.4.0" 
} | Set-Content Rel_DDL.SQL
上面的PowerShell代码将SQL文件中的
SWIFT 2.3.0
替换为
SWIFT 2.4.0
,当我运行PowerShell时,它可以正常工作


我想通过Windows CMD运行PowerShell命令,但出现错误。

您可以在CMD Windows中使用带有
-command
参数的PowerShell.exe命令。你试过了吗

-命令

Executes the specified commands (and any parameters) as though they were
typed at the Windows PowerShell command prompt, and then exits, unless
NoExit is specified. The value of Command can be "-", a string. or a
script block.
如果命令值为“-”,则从标准输入读取命令文本。 如果Command的值是脚本块,则脚本块必须用大括号({})括起来

只有在Windows PowerShell中运行PowerShell.exe时,才能指定脚本块。脚本块的结果将作为反序列化的XML对象而不是活动对象返回到父shell

如果Command的值是字符串,则Command必须是该命令中的最后一个参数,因为在该命令之后键入的任何字符都将被解释为命令参数


要编写运行Windows PowerShell命令的字符串,请使用以下格式:&{},其中引号表示字符串,调用运算符(&)导致执行该命令。

使用该.bat脚本中的
PowerShell
命令。我不会写入与用于输入的文件相同的文件。是的,我知道这是有效的,因为
Get Content
读取整个文件,但这不是一个好做法

powershell -NoProfile -Command "(Get-Content Rel_DDL.SQL) |" ^
    "ForEach-Object { $_ -replace 'SWIFT [\d\.]+', 'SWIFT 2.4.0' } |" ^
    "Out-File -FilePath Rel_DDL2.SQL -Encoding Default"

可能重复您能详细描述一下您的设置吗?通过文件位置、从powershell和cmd执行时的提示增强您的问题。