2017-09-06 149 views
0
@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 

當我保存爲.bat文件並運行它不工作,否則它在PowerShell中工作正常,當我手動輸入..如何在批處理文件中運行PowerShell命令

+5

[如何從批處理文件運行PowerShell腳本](https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file ) – arco444

+1

我不確定重複的問題是否可以解釋它。您需要將您的腳本保存爲.ps1文件,然後您可以從.bat腳本執行它,如上述鏈接問題中所述。 –

+0

[如何從批處理文件執行powershell命令?](https://stackoverflow.com/questions/6037146/how-to-execute-powershell-commands-from-a-batch-file) –

回答

0

附上您的PowerShell代碼中,

powershell -Command "& {}" 

記住所有的語句與;分離,並用帶引號的字符串附上",通過使用""

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

感謝它現在的工作:) – Alone

+0

如果有人遇到這個問題,就像我這樣做 - 當執行這樣的命令時,可能有助於使用'foreach-object'而不是'%'別名。 否則,可能會導致'表達式只被允許作爲管道錯誤的第一個元素。 –

0

PowerShell有它自己的腳本文件類型,擴展名爲.ps1

你的代碼保存到一個名爲.ps1文件,並從一個批處理文件運行它:

PowerShell的xxx.ps1

1

,即我認爲this is最簡單和最清晰的方式,因爲它穿上」 t需要任何多餘的開關;只是簡單的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 

就在每一行的末尾添加一個; ^和轉義反斜線每個報價。

相關問題