powershell - 无法使用批处理文件运行 PowerShell 脚本

标签 powershell batch-file

这个问题在这里已经有了答案:





PowerShell says "execution of scripts is disabled on this system."

(37 个回答)


11 个月前关闭。




我编写了一个 PowerShell 脚本来授予运行访问权限以在没有管理员权限的情况下运行。所以我需要从批处理文件运行该脚本。在这里,我附上了我的 PowerShell 脚本和批处理文件。我无法从我的批处理文件运行 PowerShell 脚本。
访问.ps1

powershell -File "%~dpn0.ps1" %*
Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Unrestricted -Force" -Verb RunAs
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Wow6432Node\Unicorn",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$rule = New-Object System.Security.AccessControl.RegistryAccessRule (".\USERS","FullControl",@("ObjectInherit","ContainerInherit"),"None","Allow")
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)

Write-Host "Successfully set permission to PM Registry!"
访问.bat
@ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE
这是错误
enter image description here

最佳答案

在您的系统上,不允许执行 PowerShell 脚本。通过执行(具有管理权限)允许它:

Set-ExecutionPolicy RemoteSigned
或者绕过它(在您的 .bat 中):
PowerShell.exe -ExecutionPolicy Bypass -File .\Access.ps1
Access.ps1 ,以下行几乎没用:
Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Unrestricted -Force" -Verb RunAs
因为您已经需要执行脚本的权限才能运行此脚本。

关于powershell - 无法使用批处理文件运行 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65335812/

相关文章:

batch-file - 如何将参数数组传递给批处理文件?

python - 使用批处理文件运行当前文件夹中的脚本

file - 将多个文本文件合并为一个

powershell - 直到循环故障

azure - 使用 Get-AzTable Powershell 脚本访问 Azure 表中的数据时出错

xml - 查找字符串并替换 XML 中的部分字符串

powershell - 如何访问 Powershell 选择字符串返回值

Powershell - 不在域管理员帐户下运行时调用命令访问被拒绝

windows - 当命令有空格时将命令输出检索到变量

batch-file - 调试批处理文件