简体   繁体   English

如何通过没有辅助文件的批处理文件运行Powershell脚本

[英]How to run Powershell script via batch file without secondary file

I have two files, one is called Startmenu.bat and the second one is test.ps1 我有两个文件,一个叫做Startmenu.bat ,第二个是test.ps1

Startmenu.bat: Startmenu.bat:

@echo ON
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "C:\test.ps1"' -Verb RunAs}";

test.ps1: test.ps1:

Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}

These two files are working properly if I run Startmenu.bat and test.ps1 is placed to C:\\ 如果我运行Startmenu.bat并将test.ps1放置到C:\\,这两个文件将正常工作

But my question if can I make just one file? 但是我的问题是我是否只能制作一个文件? Can I somehow include the Powershell script inside first file, I mean Startmenu.bat ? 我可以以某种方式在第一个文件(即Startmenu.bat包含Powershell脚本吗? I want to make just a single file and I really don't know how. 我只想制作一个文件,但我真的不知道该怎么做。

My Start menu in Windows 10 is broken and I have to run Powershell command to access Start Menu (at every boot). Windows 10中的我的“开始”菜单已损坏,我必须运行Powershell命令才能访问“开始”菜单(每次启动时)。

The answer is in the question! 答案就在问题中! You're running PowerShell first with the -Command parameter, which runs PowerShell commands directly; 首先使用-Command参数运行PowerShell,该参数直接运行PowerShell命令。 as it happens you're using this to open PowerShell again (with the RunAs verb so that it's elevated). 发生这种情况时,您将使用它再次打开PowerShell(使用RunAs动词以使其升高)。 You could simply use -Command again instead of using -File . 你可以简单地使用-Command再次而是采用-File

Another way is to use the -EncodedCommand parameter and Base64 encode your PowerShell commands. 另一种方法是使用-EncodedCommand参数并对Base64编码PowerShell命令。

See the very end of the help when you run powershell.exe -help : 运行powershell.exe -help时,请参阅帮助的结尾:

# To use the -EncodedCommand parameter:
$command = 'dir "c:\program files" '
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand

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

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