Command to run PowerShell command from Windows CMD

Consider:

(Get-Content Rel_DDL.SQL) | ForEach-Object { 
  $_ -replace "SWIFT [d.]+", "SWIFT 2.4.0" 
} | Set-Content Rel_DDL.SQL

The above PowerShell code replaces SWIFT 2.3.0 with SWIFT 2.4.0 in a SQL file, which when I run through PowerShell works fine.

I want to run the PowerShell command through Windows CMD, but I am getting errors.


Use the powershell command in that .bat script. I would not write to the same file as used for the input. Yes, I know this works because Get-Content reads the entire file, but it is not a good practice.

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"

You can use the Powershell.exe command in CMD Windows with the -command parameter. Did you try it?

-Command

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.

If the value of Command is "-", the command text is read from standard input. If the value of Command is a script block, the script block must be enclosed in braces ({}).

You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script block are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command, because any characters typed after the command are interpreted as the command arguments.

To write a string that runs a Windows PowerShell command, use the format: "& {}" where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

链接地址: http://www.djcxy.com/p/30252.html

上一篇: Unix的“哪些Java”等效命令在Windows上?

下一篇: 从Windows CMD运行PowerShell命令的命令