Wednesday, 10 September 2014

Exception “Script is not digitally signed” or “Script cannot be loaded because running scripts is disabled on this system” while running the PowerShell script


Hi again,
This is next step to my previous blog. I have written a CSOM based PowerShell script for creating a site collection in Office 365. But script couldn’t get executed getting a following exception

File Create-SiteCollection.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
+ .\Create-SiteCollection.ps1.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess


The above error appears when I ran the script from my Windows 7 machine.  Just curiosity purpose I tried to ran the script from Windows Server 2008 (Even though it doesn’t matter for Office 365 from where I am executing the scriptJ) but getting a slightly different exception than the above as

File Create-SiteCollection.ps1 cannot be loaded.  The file Create-SiteCollection.ps1 is not digitally signed. The script will not execute on the system.  For more information, see

about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1

+ .\Create-SiteCollection.ps1.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Actually both the errors are same just little difference in the message.  This error means that script is not allowed to execute on this computer.  There are 4 kinds of execution policies:
  1. Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
  2. AllSigned - Only scripts signed by a trusted publisher can be run.
  3. RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
  4. Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
Reference: http://technet.microsoft.com/en-us/library/ee176961.aspx

There is Get-ExecutionPolicy cmdlet available which returns the current execution policy set. Default execution policy is “Restricted” that means no script can be run.
There are two possible solutions to solve this problem:
  1. Change the execution policy using Set-ExecutionPolicy cmdlets. Set the execution policy to “Unrestricted” as                                                                                                    Set-ExecutionPolicy Unrestricted or any one of the above possible value
Thanks !
Enjoy reading :)
Feel free to comment / feedback / suggestions /thoughts if any or if you have any query :)



No comments: