通过bat文件运行powershell命令时出现问题

通过bat文件运行powershell命令时出现问题,powershell,batch-file,cmd,scripting,Powershell,Batch File,Cmd,Scripting,如果直接放入powershell,命令将运行,但是如果我尝试在bat文件中使用powershell-command标记运行,则会出现以下错误 运行的命令是: powershell -Command $tempFilePath = "$env:TEMP\$($filePath ^| Split-Path -Leaf)" 收到的错误是: 第1行字符:26 + $tempFilePath = $env:TEMP\$($filePath ^| Split-Path -Leaf) +

如果直接放入powershell,命令将运行,但是如果我尝试在bat文件中使用powershell-command标记运行,则会出现以下错误

运行的命令是:

powershell -Command $tempFilePath = "$env:TEMP\$($filePath ^| Split-Path -Leaf)"
收到的错误是:

第1行字符:26

+ $tempFilePath = $env:TEMP\$($filePath ^| Split-Path -Leaf)
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\$($filePath ^| Split-Path -Leaf)' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
如果您需要更多信息,请询问,我将不胜感激。非常感谢。

注释代码

$filePath = 'filedir/file.conf.js'
$tempFilePath = "$env:TEMP\$($filePath | Split-Path -Leaf)"
$find = 'texttochange'
$replace = 'newtext'
(Get-Content -Path $filePath) -replace $find, $replace | Add-Content -Path $tempFilePath
Remove-Item -Path $filePath
Move-Item -Path $tempFilePath -Destination $filePath
注释中概述的新错误:

Add-Content : Cannot find drive. A drive with the name '$env' does not exist.
At line:1 char:227
+ ... h) -replace $find, $replace | Add-Content -Path $tempFilePath; Remove ...
+                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($env:String) [Add-Content], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.AddContentCommand

Move-Item : Cannot find drive. A drive with the name '$env' does not exist.
At line:1 char:289
+ ... -Path $filePath; Move-Item -Path $tempFilePath -Destination $filePath
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($env:String) [Move-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.MoveItemCommand
在我的bat文件中运行的完整命令会产生上述错误:

powershell -Command $filePath = 'filedir/file.conf.js'; "$tempFilePath = '$env:TEMP\$($filePath ^| Split-Path -Leaf)'"; $find = 'texttochange'; $replace = 'newtext'; (Get-Content -Path $filePath) -replace $find, $replace ^| Add-Content -Path $tempFilePath; Remove-Item -Path $filePath; Move-Item -Path $tempFilePath -Destination $filePath
对于我来说,解决方法是,我将所有powershell命令放在一个ps1文件中,并从bat文件中调用它,如下所示。pshellfile.ps1文件的内容:

$filePath = 'filedir/file.conf.js'
$tempFilePath = "$env:TEMP\$($filePath | Split-Path -Leaf)"
$find = 'texttochage'
$replace = 'newtext'
(Get-Content -Path $filePath) -replace $find, $replace | Add-Content -Path $tempFilePath
Remove-Item -Path $filePath
Move-Item -Path $tempFilePath -Destination $filePath
这是我的bat文件中的命令:

powershell.exe -ExecutionPolicy ByPass -File pshellfile.ps1

对于最初的问题,在cmd.exe行中使用正确的魔法来运行PowerShell可能会令人沮丧

SET "FILEPATH=C:\src\t\xxx.txt"
powershell -NoLogo -NoProfile -Command "Join-Path $($Env:TEMP) $('%FILEPATH%' | Split-Path -Leaf)"
产生:

C:\Users\lit\AppData\Local\Temp\xxx.txt

powershell-Command“$tempFilePath=”$env:TEMP\$($filePath^ Split Path-Leaf)”这个命令对我来说没有意义!哪里定义了
$filePath
?当然,即使定义了它,您所做的只是在powershell中设置一个变量,然后关闭它,从而再次取消定义该变量!还是一样的结果,同样的错误。谢谢你。你把我的建议抄对了吗?我没有收到任何错误。您不能在批处理文件的一行中输入所有这些
>
字符。我假设它们应该代表新行,所以我已经将它们添加到您的问题中的各行中。请以后不要将代码发布到评论区,如果复制和粘贴,请将其以我们或系统可读的格式显示。