简体   繁体   English

在批处理文件中嵌入 powershell 脚本

[英]Embed powershell script within batch file

Can you teach me how to embed/Encode this within batch file?你能教我如何在批处理文件中嵌入/编码吗?

Get-Process | Where-Object { $_.Name -eq "Sample1" } | Select-Object -First 1 | Stop-Process


$app = Get-WmiObject -Class Win32_Product | Where-Object {
 $_.Name -match "Sample1" {

$app.Uninstall()

There's really no reason to ever write a batch/powershell hybrid when using either one or the other works just fine, but there's a technique that was developed on DOSTips that you can use to embed Powershell in a batch script:当使用其中一个或另一个工作正常时,真的没有理由编写批处理/powershell 混合,但是有一种在 DOSTips上开发的技术,您可以使用它来将 Powershell 嵌入批处理脚本中:

<# :
:: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
@echo off
setlocal
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>

Get-Process | Where-Object { $_.Name -eq "Sample1" } | Select-Object -First 1 | Stop-Process


$app = Get-WmiObject -Class Win32_Product | Where-Object {
 $_.Name -match "Sample1" {

$app.Uninstall()

The batch code goes into a powershell multiline comment, calls itself with the powershell interpreter, then exits.批处理代码进入 powershell 多行注释,使用 powershell 解释器调用自身,然后退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM