PowerShell: Execution policy workarounds to run a script

一世执手 提交于 2019-12-11 16:04:26

问题


Is there a workaround for execution policies?

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> .\stock.ps1
.\stock.ps1 : File C:\Users\thufir\Desktop\stock.ps1 cannot be loaded because running scripts is disabled on this
system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\stock.ps1
+ ~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Users\thufir\Desktop>

I am just looking to find stock quotes with a script. Yes, you can sort of/kind of run a script with copy/paste, but...isn't there anything nicer?

Policy is as:

PS C:\Users\thufir\Desktop>
PS C:\Users\thufir\Desktop> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined


PS C:\Users\thufir\Desktop>

(Not my machine.)

I am looking for the inverse to this question.


回答1:


To build off of Bill_Stewart's comment, create a shortcut with the following target and it will allow you to run a PowerShell script:

 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file "\\Server\Path\to\script\script.ps1"

If you want to run this command on a remote machine, you can can always do something like this after creating the above shortcut and storing it somewhere the remote machine can access on the network.

Invoke-Command -computername Computer1 -Scriptblock { Start-process "\\Server\Path\to\shortcut\shortcut.lnk" }


来源:https://stackoverflow.com/questions/48814448/powershell-execution-policy-workarounds-to-run-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!