powershell - 无法使用批处理文件运行 PowerShell 脚本
问题描述
我编写了一个 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
这是错误
解决方案
在您的系统上,不允许执行 PowerShell 脚本。通过执行(具有管理权限)允许它:
Set-ExecutionPolicy RemoteSigned
或绕过它(在你的.bat
):
PowerShell.exe -ExecutionPolicy Bypass -File .\Access.ps1
在里面Access.ps1
,下面这行几乎没用:
Start-Process PowerShell -ArgumentList "Set-ExecutionPolicy Unrestricted -Force" -Verb RunAs
因为您已经需要执行脚本的权限才能运行此脚本。
推荐阅读
- java - Spring Cloud Gateway - 审计日志
- mysql - 在这个例子中如何使用 INNER JOIN
- r - 在R闪亮中隐藏标签面板
- swift - CAShapeLayer 获取当前比例
- python - IndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引增强现实
- r - R 没有要绘制的交易/头寸
- r - 无法安装包“userfriendlyscience”
- ramda.js - ramda 根据原始值更新对象值
- mysql - 更新条件以更改索引行
- sql - 为什么 T-SQL 在尝试截断列大小时会自动回滚事务?