[英]How to run powershell command in 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
when i save as .bat file and run it does not working otherwise it is working properly in powershell when i entering manually..当我保存为 .bat 文件并运行它时它不起作用,否则当我手动输入时它在 powershell 中正常工作..
Enclose your PowerShell code in,将您的 PowerShell 代码包含在,
powershell -Command "& {}"
Remember to separate all statements with ;
请记住用
;
分隔所有语句;
and to enclose your "
with a quoted string, ie by using ""
并用带引号的字符串将您的
"
括起来,即使用""
powershell -Command "& {$BIOS= Get-WmiObject -computername ""BAHRIATSG2-PC\"" -Namespace root/hp/instrumentedBIOS -Class HP_BIOSSettingInterface; $BIOS.SetBIOSSetting('Setup Password','<utf-16/>TheBIOSPassword','<utf-16/>')}"
I think this is the simplest and clearest way because it don't requires any superfluous switch;我认为这是最简单、最清晰的方式,因为它不需要任何多余的开关; just plain PowerShell code:
只是普通的 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
Just add a ; ^
只需添加一个
; ^
; ^
at end of each line and escape each quote with backslash. ; ^
在每一行的末尾并用反斜杠转义每个引号。
powershell has its own script file type, extension is .ps1 powershell 有自己的脚本文件类型,扩展名为 .ps1
Save your code to a .ps1 file, and run it from a batch file with:将您的代码保存到 .ps1 文件,然后从批处理文件中运行它:
powershell xxx.ps1 PowerShell xxx.ps1
Following this thread:按照这个线程:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/converting-powershell-to-batch https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/converting-powershell-to-batch
you can convert any PowerShell script into a batch file easily using this PowerShell function:您可以使用此 PowerShell 功能轻松地将任何 PowerShell 脚本转换为批处理文件:
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
To convert all PowerShell scripts inside a directory, simply run the following command:要转换目录中的所有PowerShell 脚本,只需运行以下命令:
Get-ChildItem -Path <DIR-PATH> -Filter *.ps1 |
Convert-PowerShellToBatch
Where is the path to the desired folder.所需文件夹的路径在哪里。 For instance:
例如:
Get-ChildItem -Path "C:\path\to\powershell\scripts" -Filter *.ps1 |
Convert-PowerShellToBatch
To convert a single PowerShell script, simply run this:要转换单个PowerShell 脚本,只需运行以下命令:
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
Where is the path to the desired file.所需文件的路径在哪里。
The converted files are located in the source directory.转换后的文件位于源目录中。 ie, <FILE-PATH> or <DIR-PATH> .
即, <FILE-PATH>或<DIR-PATH> 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.