I wrote a simple batch file as a PowerShell script, and I am getting errors when they run.
It's in a scripts directory in my path. This is the error I get:
Cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about-signing".
I looked in the help, but it's less than helpful.
This question is related to
powershell
The command set-executionpolicy unrestricted
will allow any script you create to run as the logged in user. Just be sure to set the executionpolicy setting back to signed using the set-executionpolicy signed
command prior to logging out.
You need to run Set-ExecutionPolicy
:
Set-ExecutionPolicy Restricted <-- Will not allow any powershell scripts to run. Only individual commands may be run.
Set-ExecutionPolicy AllSigned <-- Will allow signed powershell scripts to run.
Set-ExecutionPolicy RemoteSigned <-- Allows unsigned local script and signed remote powershell scripts to run.
Set-ExecutionPolicy Unrestricted <-- Will allow unsigned powershell scripts to run. Warns before running downloaded scripts.
Set-ExecutionPolicy Bypass <-- Nothing is blocked and there are no warnings or prompts.
On Windows 10: Click change security property of myfile.ps1 and change "allow access" by right click / properties on myfile.ps1
Use:
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Always use the above command to enable to executing PowerShell in the current session.
We can bypass execution policy in a nice way (inside command prompt):
type file.ps1 | powershell -command -
Or inside powershell:
gc file.ps1|powershell -c -
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
The above command worked for me even when the following error happens:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
Also it's worth knowing that you may need to include .\
in front of the script name. For example:
.\scriptname.ps1
import-module IISAdministration;
function StartSite{
param($sitename)
try{
Start-IISSite -Name $sitename;
Write-Host "Site was started";
}
catch{
Write-Error "Error while staring the IISSite";
}
}
function StopSite{
param($sitename)
try{
Stop-IISSite -Name $sitename -confirm:$False; # Supress interaction inputs
Write-Host "Site was stopped";
}
catch{
Write-Error "Error while stopping the IISSite";
}
}
function ReplaceSiteFiles{
try{
Get-ChildItem -Path A:\APPS\CreditApp -Recurse | Foreach-Object {Remove-Item -Recurse -Path $_.FullName} # Remove file from AppPool Directory
Expand-Archive A:\Staging\LTA\Installers\CreditApp\CreditApp.zip -DestinationPath A:\APPS\ # Extract files from zip
Write-Host "Site files replaced successfully!";
}
catch [System.SystemException]{
Write-Host "Error while replacing the site files";
Write-Host $_
}
}
## Start Here
$site=Get-IISSite -Name "Default Web Site";
Write-Host $site
if($site.length -eq 1){
$siteState = $site.state;
Write-Host "The Site Exists with state: ${siteState}";
switch ($siteState)
{
'started' {
StopSite -sitename $site.name;
ReplaceSiteFiles;
StartSite -sitename $site.name;
}
'stopped' {
ReplaceSiteFiles;
StartSite -sitename $site.name;
}
default { "Deployment failed! Site state could not be determined.";}
}
}
else{
Write-Error "Invalid! Site does not exists";
}
## End Here
I was able to bypass this error by invoking PowerShell like this:
powershell -executionpolicy bypass -File .\MYSCRIPT.ps1
That is, I added the -executionpolicy bypass
to the way I invoked the script.
This worked on Windows 7 Service Pack 1. I am new to PowerShell, so there could be caveats to doing that that I am not aware of.
[Edit 2017-06-26] I have continued to use this technique on other systems including Windows 10 and Windows 2012 R2 without issue.
Here is what I am using now. This keeps me from accidentally running the script by clicking on it. When I run it in the scheduler I add one argument: "scheduler" and that bypasses the prompt.
This also pauses the window at the end so I can see the output of PowerShell.
if NOT "%1" == "scheduler" (
@echo looks like you started the script by clicking on it.
@echo press space to continue or control C to exit.
pause
)
C:
cd \Scripts
powershell -executionpolicy bypass -File .\rundps.ps1
set psexitcode=%errorlevel%
if NOT "%1" == "scheduler" (
@echo Powershell finished. Press space to exit.
pause
)
exit /b %psexitcode%
Source: Stackoverflow.com