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



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

开始菜单.bat:

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

测试.ps1:

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

如果我运行Startmenu.bat并且test.ps1被放置在 C:\

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

我在 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

相关内容

最新更新