Windows 7 上的 PowerShell:针对普通用户的 Set-ExecutionPolicy

发布于 2024-10-11 03:05:50 字数 1185 浏览 1 评论 0原文

我想以普通用户身份在 Windows 7 上运行 PowerShell 脚本。每当我尝试时,都会收到以下错误:

File C:\Users\danv\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because the
execution of scripts is disabled on this system. Please see "get-help about_signing" for
more details.
At line:1 char:2
+ . <<<<  'C:\Users\danv\Documents\WindowsPowerShell\profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

尝试通过 Set-ExecutionPolicy Unrestricted 解决失败:

PS C:\Users\danv> Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'
is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

我可以以管理员身份运行 Set-ExecutionPolicy Unrestricted 命令,但这并不'似乎会传播给非管理员用户。

如何以非管理员身份成功运行脚本?

I want to run PowerShell scripts on Windows 7 as a regular user. Whenever I try, I get the following error:

File C:\Users\danv\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because the
execution of scripts is disabled on this system. Please see "get-help about_signing" for
more details.
At line:1 char:2
+ . <<<<  'C:\Users\danv\Documents\WindowsPowerShell\profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

Attempting to solve via Set-ExecutionPolicy Unrestricted fails:

PS C:\Users\danv> Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'
is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

I can run the Set-ExecutionPolicy Unrestricted command as administrator, but this doesn't seem to propagate to non-administrator users.

How can I successfully run scripts as a non-administrator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谷夏 2024-10-18 03:05:50
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

这将为当前用户(存储在 HKEY_CURRENT_USER 中)而不是本地计算机(HKEY_LOCAL_MACHINE 中)设置执行策略。如果您对计算机没有管理控制权,这非常有用。

RemoteSigned 是比 Unrestricted 更安全的执行策略。如果您下载了脚本,并且 RemoteSigned 阻止您执行该脚本,则在审查该脚本后,通过打开文件的属性并标记“取消阻止”来删除限制。如果这是不可行的,那么您可以将该策略设置为Unrestricted

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This will set the execution policy for the current user (stored in HKEY_CURRENT_USER) rather than the local machine (HKEY_LOCAL_MACHINE). This is useful if you don't have administrative control over the computer.

RemoteSigned is a safer execution policy than Unrestricted. If you download a script and RemoteSigned is preventing you from executing it, then after vetting the script, remove the restriction by opening the file's properties and flagging "Unblock". If this is infeasible, then you can set the policy to Unrestricted instead.

白昼 2024-10-18 03:05:50

如果您(或乐于助人的管理员)以管理员身份运行 Set-ExecutionPolicy,将为所有用户设置该策略。 (我建议使用“remoteSigned”而不是“unrestricted”作为安全措施。)

注意:在 64 位操作系统上,您需要为 32 位和 64 位 PowerShell 运行 Set-ExecutionPolicy分别地。

If you (or a helpful admin) runs Set-ExecutionPolicy as administrator, the policy will be set for all users. (I would suggest "remoteSigned" rather than "unrestricted" as a safety measure.)

NB.: On a 64-bit OS you need to run Set-ExecutionPolicy for 32-bit and 64-bit PowerShell separately.

虚拟世界 2024-10-18 03:05:50

这应该可以解决您的问题,您应该尝试运行以下命令:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 

This should solve your problem, you should try to run the following below:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 
漫雪独思 2024-10-18 03:05:50

如果您是电脑的管理员,则可以在 powershell 窗口中键入以下命令

 Set-ExecutionPolicy Unrestricted

。您可能必须以管理员身份运行 shell。

一旦你这样做了,它会要求你确认,
如果要为所有全局用户设置无限制设置,请输入“A”
如果您只想为当前用户(管理员)设置不受限制的设置,请输入“Y”

记住:- 您始终可以使用以下命令恢复所做的更改:

Set-ExecutionPolicy Restricted

If your the administrator of your pc, you can type out the following command in your

 Set-ExecutionPolicy Unrestricted

powershell window. You might have to run the shell as an administrator.

Once you have done that, it will ask you for the confirmation,
If you want to set the unrestricted setting for all the global users, enter 'A'
If you want to set the unrestricted setting only for current user(Admin), enter 'Y'

Remember:- You can always revert the changes you made with the following command:

Set-ExecutionPolicy Restricted
总以为 2024-10-18 03:05:50
Select ***Start > All Programs > Windows PowerShell version > Windows PowerShell***.
Type ```Set-ExecutionPolicy RemoteSigned``` to set the policy to ```RemoteSigned```.
Type ```Set-ExecutionPolicy Unrestricted``` to set the policy to ```Unrestricted```.
Type ```Get-ExecutionPolicy``` to verify the current settings for the execution policy.
Type ```Exit```.
Select ***Start > All Programs > Windows PowerShell version > Windows PowerShell***.
Type ```Set-ExecutionPolicy RemoteSigned``` to set the policy to ```RemoteSigned```.
Type ```Set-ExecutionPolicy Unrestricted``` to set the policy to ```Unrestricted```.
Type ```Get-ExecutionPolicy``` to verify the current settings for the execution policy.
Type ```Exit```.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文