private static string GetLocalPreferenceValue(string shellId, ExecutionPolicyScope scope) { string configurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (scope) { case ExecutionPolicyScope.CurrentUser: using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(configurationPath)) { if (registryKey != null) { string str = registryKey.GetValue("ExecutionPolicy") as string; registryKey.Close(); return(str); } break; } case ExecutionPolicyScope.LocalMachine: using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(configurationPath)) { if (registryKey != null) { string str = registryKey.GetValue("ExecutionPolicy") as string; registryKey.Close(); return(str); } break; } } return((string)null); }
private static void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { ExecutionPolicy currentPolicy = ExecutionPolicy.Undefined; using (PowerShell ps = PowerShell.Create()) { ps.AddCommand("Get-ExecutionPolicy"); foreach (PSObject result in ps.Invoke()) { currentPolicy = ((ExecutionPolicy)result.BaseObject); break; } if ((currentPolicy <= policy || currentPolicy == ExecutionPolicy.Bypass) && currentPolicy != ExecutionPolicy.Undefined) //Bypass is the absolute least restrictive, but as added in PS 2.0, and thus has a value of '4' instead of a value that corresponds to it's relative restrictiveness { return; } ps.Commands.Clear(); ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", policy).AddParameter("Scope", scope).AddParameter("Force"); ps.Invoke(); } }
private static void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { using (var ps = PowerShell.Create()) { ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", policy).AddParameter("Scope", scope); ps.Invoke(); } }
private static void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { using (var ps = PowerShell.Create()) { ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", policy).AddParameter("Scope", scope); ps.Invoke(); } }
private ExecutionPolicy GetExecutionPolicy(ExecutionPolicyScope scope) { using (PowerShell ps = PowerShell.Create()) { ps.Runspace = _runspace; ps.AddCommand("Get-ExecutionPolicy").AddParameter("Scope", scope); return(ps.Invoke <ExecutionPolicy>().FirstOrDefault()); } }
private void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { ExecutionPolicy machinePolicy = ExecutionPolicy.Undefined; ExecutionPolicy userPolicy = ExecutionPolicy.Undefined; ServiceCommon.Log("Setting execution policy"); using (PowerShell ps = PowerShell.Create()) { ps.Runspace = _runspace; ps.AddCommand("Get-ExecutionPolicy") .AddParameter("Scope", "MachinePolicy"); foreach (var result in ps.Invoke()) { machinePolicy = ((ExecutionPolicy)result.BaseObject); break; } ps.Commands.Clear(); ps.AddCommand("Get-ExecutionPolicy") .AddParameter("Scope", "UserPolicy"); foreach (var result in ps.Invoke()) { userPolicy = ((ExecutionPolicy)result.BaseObject); break; } ps.Commands.Clear(); if (machinePolicy != ExecutionPolicy.Undefined || userPolicy != ExecutionPolicy.Undefined) { return; } ps.Commands.Clear(); ps.AddCommand("Set-ExecutionPolicy") .AddParameter("ExecutionPolicy", policy) .AddParameter("Scope", scope) .AddParameter("Force"); try { //If a more restritive scope causes this to fail, this can throw //an exception and cause the host to crash. This causes it to be //recreated over and over again. This leads to performance issues in VS. ps.Invoke(); } catch (Exception ex) { ServiceCommon.Log("Failed to set execution policy. {0}", ex.Message); } } }
private void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { using (PowerShell ps = PowerShell.Create()) { ps.Runspace = _runspace; ps.AddCommand("Set-ExecutionPolicy") .AddParameter("ExecutionPolicy", policy) .AddParameter("Scope", scope); ps.Invoke(); } }
private static string GetGroupPolicyValue(string shellId, ExecutionPolicyScope scope) { switch (scope) { case ExecutionPolicyScope.UserPolicy: try { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Policies\\Microsoft\\Windows\\PowerShell")) { switch (SecuritySupport.GetRegistryKeyFromGroupPolicyTest("Software\\Policies\\Microsoft\\Windows\\PowerShell", "EnableScripts", key)) { case SecuritySupport.GroupPolicyStatus.Enabled: return(key.GetValue("ExecutionPolicy") as string); case SecuritySupport.GroupPolicyStatus.Disabled: key.Close(); return("Restricted"); } } } catch (SecurityException ex) { } return((string)null); case ExecutionPolicyScope.MachinePolicy: try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Policies\\Microsoft\\Windows\\PowerShell")) { if (key != null) { switch (SecuritySupport.GetRegistryKeyFromGroupPolicyTest("Software\\Policies\\Microsoft\\Windows\\PowerShell", "EnableScripts", key)) { case SecuritySupport.GroupPolicyStatus.Enabled: return(key.GetValue("ExecutionPolicy") as string); case SecuritySupport.GroupPolicyStatus.Disabled: key.Close(); return("Restricted"); } } } } catch (SecurityException ex) { } return((string)null); default: return((string)null); } }
private static string GetLocalPreferenceValue(string shellId, ExecutionPolicyScope scope) { string registryConfigurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (scope) { case ExecutionPolicyScope.CurrentUser: { if (OSHelper.IsUnix) { return("Unrestricted"); } else { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryConfigurationPath)) { if (key != null) { string str2 = key.GetValue("ExecutionPolicy") as string; key.Close(); return(str2); } break; } } } case ExecutionPolicyScope.LocalMachine: if (OSHelper.IsUnix) { return("Unrestricted"); } else { using (RegistryKey key2 = Registry.LocalMachine.OpenSubKey(registryConfigurationPath)) { if (key2 != null) { string str3 = key2.GetValue("ExecutionPolicy") as string; key2.Close(); return(str3); } } } break; } return(null); }
internal static ExecutionPolicy GetExecutionPolicy( string shellId, ExecutionPolicyScope scope) { switch (scope) { case ExecutionPolicyScope.Process: string environmentVariable = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference"); return(!string.IsNullOrEmpty(environmentVariable) ? SecuritySupport.ParseExecutionPolicy(environmentVariable) : ExecutionPolicy.Undefined); case ExecutionPolicyScope.CurrentUser: case ExecutionPolicyScope.LocalMachine: string localPreferenceValue = SecuritySupport.GetLocalPreferenceValue(shellId, scope); return(!string.IsNullOrEmpty(localPreferenceValue) ? SecuritySupport.ParseExecutionPolicy(localPreferenceValue) : ExecutionPolicy.Undefined); case ExecutionPolicyScope.UserPolicy: case ExecutionPolicyScope.MachinePolicy: string groupPolicyValue = SecuritySupport.GetGroupPolicyValue(shellId, scope); if (!string.IsNullOrEmpty(groupPolicyValue)) { Process process = Process.GetCurrentProcess(); string a = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe"); bool flag = false; try { for (; process != null; process = PsUtils.GetParentProcess(process)) { if (string.Equals(a, PsUtils.GetMainModule(process).FileName, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } } catch (Win32Exception ex) { } if (!flag) { return(SecuritySupport.ParseExecutionPolicy(groupPolicyValue)); } } return(ExecutionPolicy.Undefined); default: return(ExecutionPolicy.Restricted); } }
protected override void BeginProcessing() { if (!this.list || !this.scopeSpecified) { string shellID = base.Context.ShellID; if (!this.list) { if (!this.scopeSpecified) { base.WriteObject(SecuritySupport.GetExecutionPolicy(shellID)); return; } else { base.WriteObject(SecuritySupport.GetExecutionPolicy(shellID, this.executionPolicyScope)); return; } } else { ExecutionPolicyScope[] executionPolicyScopePreferences = SecuritySupport.ExecutionPolicyScopePreferences; for (int i = 0; i < (int)executionPolicyScopePreferences.Length; i++) { ExecutionPolicyScope executionPolicyScope = executionPolicyScopePreferences[i]; PSObject pSObject = new PSObject(); ExecutionPolicy executionPolicy = SecuritySupport.GetExecutionPolicy(shellID, executionPolicyScope); PSNoteProperty pSNoteProperty = new PSNoteProperty("Scope", (object)executionPolicyScope); pSObject.Properties.Add(pSNoteProperty); pSNoteProperty = new PSNoteProperty("ExecutionPolicy", (object)executionPolicy); pSObject.Properties.Add(pSNoteProperty); base.WriteObject(pSObject); } return; } } else { string listAndScopeSpecified = ExecutionPolicyCommands.ListAndScopeSpecified; ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(), "ListAndScopeSpecified", ErrorCategory.InvalidOperation, null); errorRecord.ErrorDetails = new ErrorDetails(listAndScopeSpecified); base.ThrowTerminatingError(errorRecord); return; } }
public void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { string command = string.Format(CultureInfo.InvariantCulture, "Set-ExecutionPolicy {0} -Scope {1} -Force", policy.ToString(), scope.ToString()); Invoke(command, inputs: null, outputResults: false); }
public SetExecutionPolicyCommand() { this.executionPolicyScope = ExecutionPolicyScope.LocalMachine; }
public ExecutionPolicy GetExecutionPolicy(ExecutionPolicyScope scope) { return GetExecutionPolicy("Get-ExecutionPolicy -Scope " + scope); }
public static ExecutionPolicy GetExecutionPolicy(this Runspace runspace, ExecutionPolicyScope scope) { return GetExecutionPolicy(runspace, "Get-ExecutionPolicy -Scope " + scope); }
internal static void SetExecutionPolicy(ExecutionPolicyScope scope, ExecutionPolicy policy, string shellId) { #if UNIX throw new PlatformNotSupportedException(); #else string executionPolicy = "Restricted"; string preferenceKey = Utils.GetRegistryConfigurationPath(shellId); switch (policy) { case ExecutionPolicy.Restricted: executionPolicy = "Restricted"; break; case ExecutionPolicy.AllSigned: executionPolicy = "AllSigned"; break; case ExecutionPolicy.RemoteSigned: executionPolicy = "RemoteSigned"; break; case ExecutionPolicy.Unrestricted: executionPolicy = "Unrestricted"; break; case ExecutionPolicy.Bypass: executionPolicy = "Bypass"; break; } // Set the execution policy switch (scope) { case ExecutionPolicyScope.Process: { if (policy == ExecutionPolicy.Undefined) executionPolicy = null; Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", executionPolicy); break; } case ExecutionPolicyScope.CurrentUser: { // They want to remove it if (policy == ExecutionPolicy.Undefined) { ConfigPropertyAccessor.Instance.RemoveExecutionPolicy(ConfigPropertyAccessor.PropertyScope.CurrentUser, shellId); CleanKeyParents(Registry.CurrentUser, preferenceKey); } else { ConfigPropertyAccessor.Instance.SetExecutionPolicy(ConfigPropertyAccessor.PropertyScope.CurrentUser, shellId, executionPolicy); } break; } case ExecutionPolicyScope.LocalMachine: { // They want to remove it if (policy == ExecutionPolicy.Undefined) { ConfigPropertyAccessor.Instance.RemoveExecutionPolicy(ConfigPropertyAccessor.PropertyScope.SystemWide, shellId); CleanKeyParents(Registry.LocalMachine, preferenceKey); } else { ConfigPropertyAccessor.Instance.SetExecutionPolicy(ConfigPropertyAccessor.PropertyScope.SystemWide, shellId, executionPolicy); } break; } } #endif }
public ExecutionPolicy GetExecutionPolicy(ExecutionPolicyScope scope) { return(GetExecutionPolicy("Get-ExecutionPolicy -Scope " + scope)); }
internal static ExecutionPolicy GetExecutionPolicy(string shellId, ExecutionPolicyScope scope) { #if UNIX return ExecutionPolicy.Unrestricted; #else switch (scope) { case ExecutionPolicyScope.Process: { string policy = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference"); if (!String.IsNullOrEmpty(policy)) return ParseExecutionPolicy(policy); else return ExecutionPolicy.Undefined; } case ExecutionPolicyScope.CurrentUser: case ExecutionPolicyScope.LocalMachine: { string policy = GetLocalPreferenceValue(shellId, scope); if (!String.IsNullOrEmpty(policy)) return ParseExecutionPolicy(policy); else return ExecutionPolicy.Undefined; } // TODO: Group Policy is only supported on Full systems, but !LINUX && CORECLR // will run there as well, so I don't think we should remove it. case ExecutionPolicyScope.UserPolicy: case ExecutionPolicyScope.MachinePolicy: { string groupPolicyPreference = GetGroupPolicyValue(shellId, scope); if (!String.IsNullOrEmpty(groupPolicyPreference)) { // Be sure we aren't being called by Group Policy // itself. A group policy should never block a logon / // logoff script. Process currentProcess = Process.GetCurrentProcess(); string gpScriptPath = IO.Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe"); bool foundGpScriptParent = false; try { while (currentProcess != null) { if (String.Equals(gpScriptPath, PsUtils.GetMainModule(currentProcess).FileName, StringComparison.OrdinalIgnoreCase)) { foundGpScriptParent = true; break; } else { currentProcess = PsUtils.GetParentProcess(currentProcess); } } } catch (System.ComponentModel.Win32Exception) { // If you attempt to retrieve the MainModule of a 64-bit process // from a WOW64 (32-bit) process, the Win32 API has a fatal // flaw that causes this to return the error: // "Only part of a ReadProcessMemory or WriteProcessMemory // request was completed." // In this case, we just catch the exception and eat it. // The implication is that logon / logoff scripts that somehow // launch the Wow64 version of PowerShell will be subject // to the execution policy deployed by Group Policy (where // our goal here is to not have the Group Policy execution policy // affect logon / logoff scripts. } if (!foundGpScriptParent) { return ParseExecutionPolicy(groupPolicyPreference); } } return ExecutionPolicy.Undefined; } } return ExecutionPolicy.Restricted; #endif }
internal static ExecutionPolicy GetExecutionPolicy(string shellId, ExecutionPolicyScope scope) { string groupPolicyValue; bool flag; switch (scope) { case ExecutionPolicyScope.Process: { string environmentVariable = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference"); if (string.IsNullOrEmpty(environmentVariable)) { return ExecutionPolicy.Undefined; } return ParseExecutionPolicy(environmentVariable); } case ExecutionPolicyScope.CurrentUser: case ExecutionPolicyScope.LocalMachine: { string localPreferenceValue = GetLocalPreferenceValue(shellId, scope); if (string.IsNullOrEmpty(localPreferenceValue)) { return ExecutionPolicy.Undefined; } return ParseExecutionPolicy(localPreferenceValue); } case ExecutionPolicyScope.UserPolicy: case ExecutionPolicyScope.MachinePolicy: { groupPolicyValue = GetGroupPolicyValue(shellId, scope); if (string.IsNullOrEmpty(groupPolicyValue)) { goto Label_00BA; } if (OSHelper.IsUnix) { flag = true;} else { Process currentProcess = Process.GetCurrentProcess(); string a = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe"); flag = false; try { while (currentProcess != null) { if (string.Equals(a, PsUtils.GetMainModule(currentProcess).FileName, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } currentProcess = PsUtils.GetParentProcess(currentProcess); } } catch (Win32Exception) { } } break; } default: return ExecutionPolicy.Restricted; } if (!flag) { return ParseExecutionPolicy(groupPolicyValue); } Label_00BA: return ExecutionPolicy.Undefined; }
private static string GetLocalPreferenceValue(string shellId, ExecutionPolicyScope scope) { string registryConfigurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (scope) { case ExecutionPolicyScope.CurrentUser: { if (OSHelper.IsUnix) { return "Unrestricted"; } else { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryConfigurationPath)) { if (key != null) { string str2 = key.GetValue("ExecutionPolicy") as string; key.Close(); return str2; } break; } } } case ExecutionPolicyScope.LocalMachine: if (OSHelper.IsUnix) { return "Unrestricted"; } else { using (RegistryKey key2 = Registry.LocalMachine.OpenSubKey(registryConfigurationPath)) { if (key2 != null) { string str3 = key2.GetValue("ExecutionPolicy") as string; key2.Close(); return str3; } } } break; } return null; }
private void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { using (PowerShell ps = PowerShell.Create()) { ps.Runspace = _runspace; ps.AddCommand("Set-ExecutionPolicy") .AddParameter("ExecutionPolicy", policy) .AddParameter("Scope", scope); ps.Invoke(); } }
private ExecutionPolicy GetExecutionPolicy(ExecutionPolicyScope scope) { using (PowerShell ps = PowerShell.Create()) { ps.Runspace = _runspace; ps.AddCommand("Get-ExecutionPolicy").AddParameter("Scope", scope); return ps.Invoke<ExecutionPolicy>().FirstOrDefault(); } }
public GetExecutionPolicyCommand() { this.executionPolicyScope = ExecutionPolicyScope.LocalMachine; }
/// <summary> /// Returns the value of the Execution Policy as retrieved /// from the local preference. /// </summary> /// <returns>NULL if it is not defined at this level</returns> private static string GetLocalPreferenceValue(string shellId, ExecutionPolicyScope scope) { switch (scope) { // 1: Look up the current-user preference case ExecutionPolicyScope.CurrentUser: return ConfigPropertyAccessor.Instance.GetExecutionPolicy(ConfigPropertyAccessor.PropertyScope.CurrentUser, shellId); // 2: Look up the system-wide preference case ExecutionPolicyScope.LocalMachine: return ConfigPropertyAccessor.Instance.GetExecutionPolicy(ConfigPropertyAccessor.PropertyScope.SystemWide, shellId); } return null; }
/// <summary> /// Returns the value of the Execution Policy as retrieved /// from group policy. /// </summary> /// <returns>NULL if it is not defined at this level</returns> private static string GetGroupPolicyValue(string shellId, ExecutionPolicyScope scope) { RegistryKey[] scopeKey = null; switch (scope) { case ExecutionPolicyScope.MachinePolicy: { scopeKey = Utils.RegLocalMachine; }; break; case ExecutionPolicyScope.UserPolicy: { scopeKey = Utils.RegCurrentUser; }; break; } Dictionary<string, object> groupPolicySettings = Utils.GetGroupPolicySetting(".", scopeKey); if (groupPolicySettings == null) { return null; } Object enableScriptsValue = null; if (groupPolicySettings.TryGetValue("EnableScripts", out enableScriptsValue)) { if (String.Equals(enableScriptsValue.ToString(), "0", StringComparison.OrdinalIgnoreCase)) { return "Restricted"; } else if (String.Equals(enableScriptsValue.ToString(), "1", StringComparison.OrdinalIgnoreCase)) { Object executionPolicyValue = null; if (groupPolicySettings.TryGetValue("ExecutionPolicy", out executionPolicyValue)) { return executionPolicyValue.ToString(); } } } return null; }
private static string GetGroupPolicyValue (string shellId, ExecutionPolicyScope scope) { switch (scope) { case ExecutionPolicyScope.UserPolicy: if (OSHelper.IsUnix) { return GetExecutionPolicy (ExecutionPolicy.Unrestricted); //TODO: REVIEW: URGENT: } else { try { using (RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"Software\Policies\Microsoft\Windows\PowerShell")) { switch (GetRegistryKeyFromGroupPolicyTest (@"Software\Policies\Microsoft\Windows\PowerShell", "EnableScripts", key2)) { case GroupPolicyStatus.Disabled: key2.Close (); return "Restricted"; case GroupPolicyStatus.Enabled: return (key2.GetValue ("ExecutionPolicy") as string); } } } catch (SecurityException) { } return null; } case ExecutionPolicyScope.MachinePolicy: if (OSHelper.IsUnix) { return GetExecutionPolicy (ExecutionPolicy.Unrestricted); //TODO: REVIEW: URGENT: } else { try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Policies\Microsoft\Windows\PowerShell")) { if (key != null) { switch (GetRegistryKeyFromGroupPolicyTest(@"Software\Policies\Microsoft\Windows\PowerShell", "EnableScripts", key)) { case GroupPolicyStatus.Disabled: key.Close(); return "Restricted"; case GroupPolicyStatus.Enabled: return (key.GetValue("ExecutionPolicy") as string); } } } } catch (SecurityException) { } return null; } } return null; }
private static string GetGroupPolicyValue(string shellId, ExecutionPolicyScope scope) { switch (scope) { case ExecutionPolicyScope.UserPolicy: if (OSHelper.IsUnix) { return(GetExecutionPolicy(ExecutionPolicy.Unrestricted)); //TODO: REVIEW: URGENT: } else { try { using (RegistryKey key2 = Registry.CurrentUser.OpenSubKey(@"Software\Policies\Microsoft\Windows\PowerShell")) { switch (GetRegistryKeyFromGroupPolicyTest(@"Software\Policies\Microsoft\Windows\PowerShell", "EnableScripts", key2)) { case GroupPolicyStatus.Disabled: key2.Close(); return("Restricted"); case GroupPolicyStatus.Enabled: return(key2.GetValue("ExecutionPolicy") as string); } } } catch (SecurityException) { } return(null); } case ExecutionPolicyScope.MachinePolicy: if (OSHelper.IsUnix) { return(GetExecutionPolicy(ExecutionPolicy.Unrestricted)); //TODO: REVIEW: URGENT: } else { try { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Policies\Microsoft\Windows\PowerShell")) { if (key != null) { switch (GetRegistryKeyFromGroupPolicyTest(@"Software\Policies\Microsoft\Windows\PowerShell", "EnableScripts", key)) { case GroupPolicyStatus.Disabled: key.Close(); return("Restricted"); case GroupPolicyStatus.Enabled: return(key.GetValue("ExecutionPolicy") as string); } } } } catch (SecurityException) { } return(null); } } return(null); }
internal static void SetExecutionPolicy(ExecutionPolicyScope scope, ExecutionPolicy policy, string shellId) { string str = "Restricted"; string registryConfigurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (policy) { case ExecutionPolicy.Unrestricted: str = "Unrestricted"; break; case ExecutionPolicy.RemoteSigned: str = "RemoteSigned"; break; case ExecutionPolicy.AllSigned: str = "AllSigned"; break; case ExecutionPolicy.Restricted: str = "Restricted"; break; case ExecutionPolicy.Bypass: str = "Bypass"; break; } switch (scope) { case ExecutionPolicyScope.Process: if (policy == ExecutionPolicy.Undefined) { str = null; } Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", str); return; case ExecutionPolicyScope.CurrentUser: if (policy != ExecutionPolicy.Undefined) { using (RegistryKey key2 = Registry.CurrentUser.CreateSubKey(registryConfigurationPath)) { key2.SetValue("ExecutionPolicy", str, RegistryValueKind.String); return; } } using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryConfigurationPath, true)) { if ((key != null) && (key.GetValue("ExecutionPolicy") != null)) { key.DeleteValue("ExecutionPolicy"); } } CleanKeyParents(Registry.CurrentUser, registryConfigurationPath); return; case ExecutionPolicyScope.LocalMachine: break; default: return; } if (policy == ExecutionPolicy.Undefined) { using (RegistryKey key3 = Registry.LocalMachine.OpenSubKey(registryConfigurationPath, true)) { if ((key3 != null) && (key3.GetValue("ExecutionPolicy") != null)) { key3.DeleteValue("ExecutionPolicy"); } } CleanKeyParents(Registry.LocalMachine, registryConfigurationPath); } else { using (RegistryKey key4 = Registry.LocalMachine.CreateSubKey(registryConfigurationPath)) { key4.SetValue("ExecutionPolicy", str, RegistryValueKind.String); } } }
internal static void SetExecutionPolicy( ExecutionPolicyScope scope, ExecutionPolicy policy, string shellId) { string str = "Restricted"; string configurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (policy) { case ExecutionPolicy.Unrestricted: str = "Unrestricted"; break; case ExecutionPolicy.RemoteSigned: str = "RemoteSigned"; break; case ExecutionPolicy.AllSigned: str = "AllSigned"; break; case ExecutionPolicy.Restricted: str = "Restricted"; break; case ExecutionPolicy.Bypass: str = "Bypass"; break; } switch (scope) { case ExecutionPolicyScope.Process: if (policy == ExecutionPolicy.Undefined) { str = (string)null; } Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", str); break; case ExecutionPolicyScope.CurrentUser: if (policy == ExecutionPolicy.Undefined) { using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(configurationPath, true)) { if (registryKey != null) { if (registryKey.GetValue("ExecutionPolicy") != null) { registryKey.DeleteValue("ExecutionPolicy"); } } } SecuritySupport.CleanKeyParents(Registry.CurrentUser, configurationPath); break; } using (RegistryKey subKey = Registry.CurrentUser.CreateSubKey(configurationPath)) { subKey.SetValue("ExecutionPolicy", (object)str, RegistryValueKind.String); break; } case ExecutionPolicyScope.LocalMachine: if (policy == ExecutionPolicy.Undefined) { using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(configurationPath, true)) { if (registryKey != null) { if (registryKey.GetValue("ExecutionPolicy") != null) { registryKey.DeleteValue("ExecutionPolicy"); } } } SecuritySupport.CleanKeyParents(Registry.LocalMachine, configurationPath); break; } using (RegistryKey subKey = Registry.LocalMachine.CreateSubKey(configurationPath)) { subKey.SetValue("ExecutionPolicy", (object)str, RegistryValueKind.String); break; } } }
public void SetExecutionPolicy(ExecutionPolicy policy, ExecutionPolicyScope scope) { string command = string.Format(CultureInfo.InvariantCulture, "Set-ExecutionPolicy {0} -Scope {1} -Force", policy.ToString(), scope.ToString()); Invoke(command, inputs: null, outputResults: false); }
internal static void SetExecutionPolicy(ExecutionPolicyScope scope, ExecutionPolicy policy, string shellId) { string str = "Restricted"; string registryConfigurationPath = Utils.GetRegistryConfigurationPath(shellId); switch (policy) { case ExecutionPolicy.Unrestricted: str = "Unrestricted"; break; case ExecutionPolicy.RemoteSigned: str = "RemoteSigned"; break; case ExecutionPolicy.AllSigned: str = "AllSigned"; break; case ExecutionPolicy.Restricted: str = "Restricted"; break; case ExecutionPolicy.Bypass: str = "Bypass"; break; } switch (scope) { case ExecutionPolicyScope.Process: if (policy == ExecutionPolicy.Undefined) { str = null; } Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", str); return; case ExecutionPolicyScope.CurrentUser: if (policy != ExecutionPolicy.Undefined) { using (RegistryKey key2 = Registry.CurrentUser.CreateSubKey(registryConfigurationPath)) { key2.SetValue("ExecutionPolicy", str, RegistryValueKind.String); return; } } using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryConfigurationPath, true)) { if ((key != null) && (key.GetValue("ExecutionPolicy") != null)) { key.DeleteValue("ExecutionPolicy"); } } CleanKeyParents(Registry.CurrentUser, registryConfigurationPath); return; case ExecutionPolicyScope.LocalMachine: break; default: return; } if (policy == ExecutionPolicy.Undefined) { using (RegistryKey key3 = Registry.LocalMachine.OpenSubKey(registryConfigurationPath, true)) { if ((key3 != null) && (key3.GetValue("ExecutionPolicy") != null)) { key3.DeleteValue("ExecutionPolicy"); } } CleanKeyParents(Registry.LocalMachine, registryConfigurationPath); } else { using (RegistryKey key4 = Registry.LocalMachine.CreateSubKey(registryConfigurationPath)) { key4.SetValue("ExecutionPolicy", str, RegistryValueKind.String); } } }
public static void SetExecutionPolicy(this Runspace runspace, ExecutionPolicy policy, ExecutionPolicyScope scope) { string command = string.Format(CultureInfo.InvariantCulture, "Set-ExecutionPolicy {0} -Scope {1} -Force", policy.ToString(), scope.ToString()); runspace.Invoke(command, null, false); }
public static ExecutionPolicy GetExecutionPolicy(this Runspace runspace, ExecutionPolicyScope scope) { return(GetExecutionPolicy(runspace, "Get-ExecutionPolicy -Scope " + scope)); }
internal static ExecutionPolicy GetExecutionPolicy(string shellId, ExecutionPolicyScope scope) { string groupPolicyValue; bool flag; switch (scope) { case ExecutionPolicyScope.Process: { string environmentVariable = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference"); if (string.IsNullOrEmpty(environmentVariable)) { return(ExecutionPolicy.Undefined); } return(ParseExecutionPolicy(environmentVariable)); } case ExecutionPolicyScope.CurrentUser: case ExecutionPolicyScope.LocalMachine: { string localPreferenceValue = GetLocalPreferenceValue(shellId, scope); if (string.IsNullOrEmpty(localPreferenceValue)) { return(ExecutionPolicy.Undefined); } return(ParseExecutionPolicy(localPreferenceValue)); } case ExecutionPolicyScope.UserPolicy: case ExecutionPolicyScope.MachinePolicy: { groupPolicyValue = GetGroupPolicyValue(shellId, scope); if (string.IsNullOrEmpty(groupPolicyValue)) { goto Label_00BA; } if (OSHelper.IsUnix) { flag = true; } else { Process currentProcess = Process.GetCurrentProcess(); string a = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe"); flag = false; try { while (currentProcess != null) { if (string.Equals(a, PsUtils.GetMainModule(currentProcess).FileName, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } currentProcess = PsUtils.GetParentProcess(currentProcess); } } catch (Win32Exception) { } } break; } default: return(ExecutionPolicy.Restricted); } if (!flag) { return(ParseExecutionPolicy(groupPolicyValue)); } Label_00BA: return(ExecutionPolicy.Undefined); }
public static void SetExecutionPolicy(this Runspace runspace, ExecutionPolicy policy, ExecutionPolicyScope scope) { string command = string.Format(CultureInfo.InvariantCulture, "Set-ExecutionPolicy {0} -Scope {1} -Force", policy.ToString(), scope.ToString()); runspace.Invoke(command, null, false); }