When I run any script from a .ps1 file on my server I receive an error about the Set-ExcutionPolicy being successful but being overridden by a higher scope.
However none of the code I am running has anything to do with execution policies or changing them. Any Idea why I'm getting this error?
This is on a Windows 2012 R2 server where execution policy for all levels is set to remote signed. I'm running on PowerShell V4.0
If I open PowerShell or the ISE and type in the command it completes without showing the error it only occurs when I try and run a script from a .ps1 file.
This is the error:
Set-ExecutionPolicy : Windows PowerShell updated your execution policy
successfully, but the setting is overridden by a policy defined at a more
specific scope. Due to the override, your shell will retain its current
effective execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List"
to view your execution policy settings. For more information please see
"Get-Help Set-ExecutionPolicy".
At line:1 char:46
+ if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException
+ FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
The "Run with PowerShell" context menu entry for .ps1 files invokes the following commandline:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
It's stored in the registry key HKCU\Microsoft.PowerShellScript.1\Shell\0\Command. Since you have the execution policy defined via Group Policy, setting a conflicting execution policy in the Process scope whenever you're running a PowerShell script via its context menu causes the error you observed.
Change the commandline in the registry to something like this:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "%L"
and the error will disappear.
I have a clean Windows 2012 R2 with SharePoint 2016. Installed Visual Studio Community 2017, when I try to install the JustInTime Debugger using the VS update I get the following error:
The product failed to install the listed workloads and components due to one or more package failures.
Incomplete components
Just-In-Time debugger (Microsoft.VisualStudio.Component.Debugger.JustInTime,version=15.0.26208.0)
You can search for solutions using the information below, modify your selections for the above workloads and components and retry the installation, or remove the product from your machine.
Following is a collection of individual package failures that led to the incomplete workloads and components above. To search for existing reports of these specific problems, please copy and paste the URL from each package failure into a web browser. If the issue has already been reported, you can find solutions or workarounds there. If the issue has not been reported, you can create a new issue where other people will be able to find solutions or workarounds.
Package 'Microsoft.VisualStudio.Debugger.JustInTime,version=15.0.26424.2' failed to install.
Search URL: https://aka.ms/VSSetupErrorReports?q=PackageId=Microsoft.VisualStudio.Debugger.JustInTime;PackageAction=Install;ReturnCode=1
Impacted components
Just-In-Time debugger (Microsoft.VisualStudio.Component.Debugger.JustInTime,version=15.0.26208.0)
Log
C:\Users\Administrator\AppData\Local\Temp\dd_setup_20170622105625_003_Microsoft.VisualStudio.Debugger.JustInTime.log
Details
Command executed: "c:\windows\syswow64\\windowspowershell\v1.0\powershell.exe" -ExecutionPolicy Bypass -InputFormat None -NoLogo -NonInteractive -NoProfile -Command "$ErrorActionPreference="""Stop""";$VerbosePreference="""Continue""";$Action="""Modify""";$AppId="""9180617e""";$Packages="""Microsoft.VisualStudio.Component.CoreEditor,Microsoft.VisualStudio.Component.NuGet,Microsoft.Net.Component.4.6.1.SDK,Microsoft.VisualStudio.Component.TypeScript.2.2,Microsoft.VisualStudio.Component.JavaScript.TypeScript,Component.WebSocket,Microsoft.VisualStudio.Component.JavaScript.Diagnostics,Microsoft.VisualStudio.Component.Roslyn.Compiler,Microsoft.VisualStudio.Component.Static.Analysis.Tools,Microsoft.VisualStudio.Component.Roslyn.LanguageServices,Microsoft.VisualStudio.Component.PortableLibrary,Microsoft.VisualStudio.Component.SQL.CLR,Microsoft.VisualStudio.Component.VisualStudioData,Microsoft.VisualStudio.Component.AppInsights.Tools,Microsoft.Component.ClickOnce,Microsoft.Net.Component.4.5.TargetingPack,Microsoft.VisualStudio.Component.Debugger.JustInTime,Microsoft.Component.MSBuild,Microsoft.Net.Component.4.6.1.TargetingPack,Microsoft.Net.ComponentGroup.DevelopmentPrerequisites,Microsoft.VisualStudio.Component.TextTemplating,Microsoft.VisualStudio.Component.ManagedDesktop.Core,Microsoft.Net.Component.4.TargetingPack,Microsoft.Net.Component.4.5.2.TargetingPack,Microsoft.VisualStudio.Component.IISExpress,Microsoft.VisualStudio.Component.WebDeploy,Microsoft.VisualStudio.Component.SQL.NCLI,Microsoft.VisualStudio.Component.SQL.LocalDB.Runtime,Microsoft.VisualStudio.Component.SQL.ADAL,Microsoft.VisualStudio.Component.Common.Azure.Tools,Microsoft.VisualStudio.Component.SQL.CMDUtils,Microsoft.VisualStudio.Component.SQL.SSDT,Microsoft.VisualStudio.Component.SQL.DataSources,Microsoft.VisualStudio.Component.Web""";[io.file]::ReadAllText("""C:\ProgramData\Microsoft\VisualStudio\Packages\Microsoft.VisualStudio.Debugger.JustInTime,version=15.0.26424.2\RegisterJustInTimeDebugger.ps1""") | Invoke-Expression; if (!$?) { exit 1603 } elseif ($LastExitCode) { exit $LastExitCode }"
Return code: 1
Return code details: Incorrect function.
In the log:
[10c8:002a][2017-06-22T10:32:43] c:\windows\syswow64\\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -InputFormat None -NoLogo -NonInteractive -NoProfile -Command "$ErrorActionPreference="""Stop""";$VerbosePreference="""Continue""";$Action="""Modify""";$AppId="""9180617e""";$Packages="""Microsoft.VisualStudio.Component.CoreEditor,Microsoft.VisualStudio.Component.NuGet,Microsoft.Net.Component.4.6.1.SDK,Microsoft.VisualStudio.Component.TypeScript.2.2,Microsoft.VisualStudio.Component.JavaScript.TypeScript,Component.WebSocket,Microsoft.VisualStudio.Component.JavaScript.Diagnostics,Microsoft.VisualStudio.Component.Roslyn.Compiler,Microsoft.VisualStudio.Component.Static.Analysis.Tools,Microsoft.VisualStudio.Component.Roslyn.LanguageServices,Microsoft.VisualStudio.Component.PortableLibrary,Microsoft.VisualStudio.Component.SQL.CLR,Microsoft.VisualStudio.Component.VisualStudioData,Microsoft.VisualStudio.Component.AppInsights.Tools,Microsoft.Component.ClickOnce,Microsoft.Net.Component.4.5.TargetingPack,Microsoft.VisualStudio.Component.Debugger.JustInTime,Microsoft.Component.MSBuild,Microsoft.Net.Component.4.6.1.TargetingPack,Microsoft.Net.ComponentGroup.DevelopmentPrerequisites,Microsoft.VisualStudio.Component.TextTemplating,Microsoft.VisualStudio.Component.ManagedDesktop.Core,Microsoft.Net.Component.4.TargetingPack,Microsoft.Net.Component.4.5.2.TargetingPack,Microsoft.VisualStudio.Component.IISExpress,Microsoft.VisualStudio.Component.WebDeploy,Microsoft.VisualStudio.Component.SQL.NCLI,Microsoft.VisualStudio.Component.SQL.LocalDB.Runtime,Microsoft.VisualStudio.Component.SQL.ADAL,Microsoft.VisualStudio.Component.Common.Azure.Tools,Microsoft.VisualStudio.Component.SQL.CMDUtils,Microsoft.VisualStudio.Component.SQL.SSDT,Microsoft.VisualStudio.Component.SQL.DataSources,Microsoft.VisualStudio.Component.Web""";[io.file]::ReadAllText("""C:\ProgramData\Microsoft\VisualStudio\Packages\Microsoft.VisualStudio.Debugger.JustInTime,version=15.0.26424.2\RegisterJustInTimeDebugger.ps1""") | Invoke-Expression; if (!$?) { exit 1603 } elseif ($LastExitCode) { exit $LastExitCode }"
[10c8:002a][2017-06-22T10:32:43] Error: Invoke-Expression : The term 'Invoke-Expression' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1812
+ ... bugger.ps1") | Invoke-Expression; if (!$?) { exit 1603 } elseif ($LastExitCode) ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Invoke-Expression:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
It seems that Invoke-Expression is not recognized....how is it possible?
I a tool just downloaded that opens a Visual Studio Command prompt from within the IDE, in the root of the current project. My main gripe is that it opens an old style command window, where I would rather have a PowerShell window. According to this post, this simple change should allow this:
cmd.exe /k ""%VS120COMNTOOLS%VsDevCmd.bat" & powershell"
When I this command from outside of VS 2015, it seems to work fine and gives me a PowerShell window. Yet when I try and run it from inside VS, using the utility's menu item, it gives me this error:
Cannot load PSReadline module. Console is running without PSReadline.
. : File
C:\Users\brady\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.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:3
+ . 'C:\Users\brady\Documents\WindowsPowerShell\Microsoft.PowerShell_pr
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess PS C:\Development\vNext\Commerce\src\Commerce.Test> Get-Execution-Policy
PS C:\Development\vNext\Commerce\src\Commerce.Test>
Get-ExecutionPolicy Restricted
My global execution policy is RemoteSigned, but in the same window that show me the error, when I run a Get-ExecutionPolicy, the returned value is Restricted.
I have tried modifying my command to include the PS switch:
cmd /k ""%VS140COMNTOOLS%VsDevCmd.bat" & powershell -ExecutionPolicy Bypass"
But this still gives me exactly the same error.
The output of the command suggested by #PetSerAl in the comments
[Environment]::Is64BitOperatingSystem;[Environment]::Is64BitProcess;Get-ExecutionPolicy -List
gives two different results. The first in a normal PS window external to VS:
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
And the second in the only PS window I can find inside VS, the Package Manager:
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process RemoteSigned
CurrentUser Undefined
LocalMachine Undefined
You can configure an external command and optionally configure a keystroke for it.
I just did this in my VS2015...neat!
Create a cmd file
Create cmd file to call VsDevCmd.bat (Developer Command Prompt for VS2015) and then PowerShell.
dev14powershell.cmd
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
powershell -ExecutionPolicy Bypass
Configure External Tool
Tools -> External Tools
Calls the cmd file above, starts in Solution Dir (configurable)
Run the new External Tool
Result
A new command window started in Solution Directory.
Optional, configure keyboard shortcut
Tools -> Options -> Keyboard
Search for external and remember the number/order of the command you created (4 in my case)
Hit assign and you have:
We're running a Team City build server which already executes this script locally with no problem. However, when I try to run this script manually I have the following problem:
Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.
PowerShell Community Extensions Imported.
PS C:\Users\user.domain> cd D:\pkg\platform-2b0c7e3f71f9-BUILD01; powershell.exe -noprofile -executionpolicy Bypass -file D:\pkg\platform-2b0c7e3f71f9-BUILD01\stage.ps1 production \\192.168.x.x\staging \\testserver.domain.com\staging
D:\pkg\platform-2b0c7e3f71f9-BUILD01
Start Staging: 12/15/2014 11:18:14
Verifying target environment configuration and staging targets
Copying source and environment switch
...
Verifying configuration files are valid XML files
Test-Xml : The 'Test-Xml' command was found in the module 'Pscx', but the module could not be loaded. For more information, run 'Import-Module Pscx'.
At D:\pkg\platform-2b0c7e3f71f9-BUILD01\stage.ps1:72 char:9
+ if (!(Test-Xml $file))
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Test-Xml:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
So I see the PowerShell Community Extenstions are getting imported (line 4) which makes me wonder what is going on. I am running the script as the exact same user that runs it under Team City. I've checked the build step I'm trying to replicate:
and I can't think of anything that I've missed (working directory option is set to the script file's directory). Any ideas would be appreciated...
Line 4 loads Pscx, Line 5 opens up a new powershell session with -NoProfile option, so it's unlikely that Pscx is loaded in that Powershell session.
I was trying to run a powershell script (which I have run before) and received the following error:Authorizationmanager Check Failed
I figured it was the Execution Policy, so ran:
Set-ExecutionPolicy Unrestricted
I then received:
Set-ExecutionPolicy : Initialization failure
At line:1 char:20
+ set-executionpolicy <<<< unrestricted
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], ManagementException
+ FullyQualifiedErrorId : System.Management.ManagementException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
The same initialization error occurs for Get-ExecutionPolicy
Some basic commands seem to work fine, such as:
Write-Output "hi"
Can anyone suggest a fix?
Note: This is on Windows XP
Update: If I run the contents of ascript from within the powershell command prompt, it works. I only get the errors if it is run as a script. Also, Get-ExecutionPolicy and Set-ExecutionPolicy still fail from within the powershell prompt.
Make sure you are running the console as admin [Right click] then [click] "run as administrator" when you run Set-ExecutionPolicy Unrestricted.
The source of the error was due to a problem with the WMI repository. I was able to repair the repository running the following command:
Note: This is for Windows XP
rundll32 wbemupgd, UpgradeRepository
Once this was run, I was successfully able to execute Get-ExecutionPolicy, and other powershell scripts again.
A couple of links that detail repairing the WMI Repository.
Another very common case (I think this only applies post-XP), is where the .ps1 file has been 'blocked' after being downloaded from an untrusted location.
Solution: open the 'Properties' of the file in Windows Explorer, and on the 'General' tab click 'Unblock', then 'Apply' or 'OK'.
Another potential reason for this error (seen on Windows Server 2012) is that the Windows Management Instrumentation service is not running.
Starting and running the service allows for Get-ExecutionPolicy to run, and then Set-ExecutionPolicy.