Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell中运行.cmd/.bat脚本_Powershell_Batch File_Windows Server 2012 R2 - Fatal编程技术网

在Powershell中运行.cmd/.bat脚本

在Powershell中运行.cmd/.bat脚本,powershell,batch-file,windows-server-2012-r2,Powershell,Batch File,Windows Server 2012 R2,我正在尝试在powershell中编写和执行.cmd脚本。我的代码是: $script = @' @echo off SETLOCAL CALL something here '@ Invoke-Expression -Command: $script 这是基于解释powershell中的此处字符串的。它在链接的底部。给你 为那些试图做同样事情的人干杯 我不断收到一个与在字符串中包含“@”运算符有关的错误: Invoke-Expression : At line:1 char:7 + @e

我正在尝试在powershell中编写和执行.cmd脚本。我的代码是:

$script = @'
@echo off
SETLOCAL

CALL something here
'@

Invoke-Expression -Command: $script
这是基于解释powershell中的
此处字符串的
。它在链接的底部。给你

为那些试图做同样事情的人干杯

我不断收到一个与在字符串中包含“@”运算符有关的错误:

Invoke-Expression : At line:1 char:7
+ @echo off
+       ~~~
Unexpected token 'off' in expression or statement.
At line:1 char:1
+ @echo off
+ ~~~~~
The splatting operator '@' cannot be used to reference variables in an expression. '@echo' can be used only as an argument to a command. To reference variables in an expression use '$echo'.
我试着避开“@”符号和其他很多东西。我想知道为什么它在第三个链接中似乎对他们有效,但在我的例子中抛出了这个错误

编辑: 写入.bat文件,然后运行bat文件会导致相同的错误:

$batchFileContent = @'
@echo off
c:\windows\system32\ntbackup.exe backup "C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\chameme.bks" /n "1file.bkf1 created 06/09/2013 at 09:36" /d "Set created 06/09/2013 at 09:36" /v:no /r:no /rs:no /hc:off /m normal /j chameme /l:s /f "\\fs1\Exchange Backups$\1file.bkf"
'@

$batchFileContent | Out-File -LiteralPath:"$env:TEMP\backup.cmd" -Force

Invoke-Expression -Command:"$env:TEMP\backup.cmd"

Remove-Item -LiteralPath:"$env:TEMP\backup.cmd" -Force
正如Bill Stewart指出的,我应该在powershell中编写.cmd脚本的内容

编辑: 这个


似乎有效。

发生这种情况是因为
调用表达式
使用PowerShell解释字符串。PowerShell允许您运行shell命令,但它首先将内容解释为PowerShell。
@
字符是PowerShell中的splatting运算符

您应该将命令保存在批处理文件中,然后执行该批处理文件

或者,您可以执行单行命令,方法是将命令输出到
cmd.exe

Invoke-Expression "cmd.exe /c @echo something"

你为什么要这么做?直接在PowerShell中执行您需要执行的任何操作。Invoke-Expression将调用PowerShell命令。要执行批处理命令,请将其保存在批处理文件中,并将该文件作为参数执行cmd.exe。或者,您可以在powershell中输入cmd.exe会话,然后执行单个命令commands@Bill_Stewart我是如此专注于寻找一种运行脚本的方法,以至于我没有想到让powershell来运行它。在这一点上,我很好奇为什么它对我不起作用,尽管我最终可能会使用powershell。
Invoke-Expression "cmd.exe /c @echo something"