繁体   English   中英

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

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

我有两个文件,一个叫做Startmenu.bat ,第二个是test.ps1

Startmenu.bat:

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

test.ps1:

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

如果我运行Startmenu.bat并将test.ps1放置到C:\\,这两个文件将正常工作

但是我的问题是我是否只能制作一个文件? 我可以以某种方式在第一个文件(即Startmenu.bat包含Powershell脚本吗? 我只想制作一个文件,但我真的不知道该怎么做。

Windows 10中的我的“开始”菜单已损坏,我必须运行Powershell命令才能访问“开始”菜单(每次启动时)。

答案就在问题中! 首先使用-Command参数运行PowerShell,该参数直接运行PowerShell命令。 发生这种情况时,您将使用它再次打开PowerShell(使用RunAs动词以使其升高)。 你可以简单地使用-Command再次而是采用-File

另一种方法是使用-EncodedCommand参数并对Base64编码PowerShell命令。

运行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