如何在批处理文件中运行powershell命令

Alo*_*one 1 powershell batch-file

@ECHO off

$BIOS= Get-WmiObject -computername "BAHRIATSG2-PC" -Namespace 
root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface

$BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')

pause
Run Code Online (Sandbox Code Playgroud)

当我另存为.bat文件并运行时,它将无法正常运行,否则当我手动输入时,它将在Powershell中正常运行。

Abd*_*ari 7

将您的 PowerShell 代码包含在,

powershell -Command "& {}"
Run Code Online (Sandbox Code Playgroud)

请记住用带引号的字符串分隔所有语句;"用引号括起来,即使用""

powershell -Command "& {$BIOS= Get-WmiObject -computername ""BAHRIATSG2-PC\"" -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface; $BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')}"
Run Code Online (Sandbox Code Playgroud)


Aac*_*ini 5

我认为这是最简单,最清晰的方法,因为它不需要任何多余的切换。只是普通的PowerShell代码:

@ECHO off

PowerShell ^
   $BIOS= Get-WmiObject -computername \"BAHRIATSG2-PC\" -Namespace;  ^ 
   root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface;  ^
   $BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')

pause
Run Code Online (Sandbox Code Playgroud)

只需; ^在每行末尾添加a 并用反斜杠将每个引号转义。