internal static SafeTransactionHandle Create(Transaction managedTransaction) { if (managedTransaction == null) { throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_NeedTransaction); } // MSDTC is not available on WinPE machine. // CommitableTransaction will use DTC APIs under the covers to get KTM transaction manager interface. // KTM is kernel Transaction Manager to handle file, registry etc and MSDTC provides an integration support // with KTM to handle transaction across kernel resources and MSDTC resources like SQL, MSMQ etc. // We need KTMRM service as well. WinPE doesn’t have these services installed if (Utils.IsWinPEHost() || PsUtils.IsRunningOnProcessorArchitectureARM()) { throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions); } IDtcTransaction dtcTransaction = TransactionInterop.GetDtcTransaction(managedTransaction); IKernelTransaction ktmInterface = dtcTransaction as IKernelTransaction; if (null == ktmInterface) { throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions); } IntPtr ktmTxHandle; int hr = ktmInterface.GetHandle(out ktmTxHandle); HandleError(hr); return(new SafeTransactionHandle(ktmTxHandle)); }
/// <summary> /// Initializes a new instance of the InformationRecord class. /// </summary> /// <param name="messageData">The object to be transmitted to the host.</param> /// <param name="source">The source of the message (i.e.: script path, function name, etc.).</param> public InformationRecord(object messageData, string source) { this.MessageData = messageData; this.Source = source; this.TimeGenerated = DateTime.Now; this.NativeThreadId = PsUtils.GetNativeThreadId(); this.ManagedThreadId = (uint)Environment.CurrentManagedThreadId; }
/// <summary> /// Each Shell ( minishell ) will have its own path specified by the /// application base folder, which should be the same as $pshome. /// </summary> /// <returns>String representing base directory of the executing shell.</returns> internal string GetDefaultShellSearchPath() { string shellID = this.HelpSystem.ExecutionContext.ShellID; // Beginning in PowerShell 6.0.0.12, the $pshome is no longer registry specified, we search the application base instead. // We use executing assemblies location in case registry entry not found return(Utils.GetApplicationBase(shellID) ?? Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName)); }
internal static bool SkipSystemRestoreOperationForARMPlatform(PSCmdlet cmdlet) { bool flag = false; if (PsUtils.IsRunningOnProcessorArchitectureARM()) { InvalidOperationException invalidOperationException = new InvalidOperationException(ComputerResources.SystemRestoreNotSupported); ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "SystemRestoreNotSupported", ErrorCategory.InvalidOperation, null); cmdlet.WriteError(errorRecord); flag = true; } return(flag); }
private static SystemEnforcementMode GetDebugLockdownPolicy(string path) { if (PsUtils.IsRunningOnProcessorArchitectureARM()) { return(SystemEnforcementMode.Enforce); } wasSystemPolicyDebugPolicy = true; if (path != null) { if (path.IndexOf("System32", StringComparison.OrdinalIgnoreCase) >= 0) { return(SystemEnforcementMode.None); } using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default)) { using (RegistryKey key2 = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\CI\TRSData")) { if (key2 != null) { object obj2 = key2.GetValue("TestPath"); key2.Close(); key.Close(); if (obj2 != null) { string[] strArray = (string[])obj2; foreach (string str in strArray) { if (path.IndexOf(str, StringComparison.OrdinalIgnoreCase) >= 0) { return(SystemEnforcementMode.None); } } } } } } return(GetSystemLockdownPolicy()); } object environmentVariable = Environment.GetEnvironmentVariable("__PSLockdownPolicy", EnvironmentVariableTarget.Machine); if (environmentVariable != null) { return(GetLockdownPolicyForResult(LanguagePrimitives.ConvertTo <int>(environmentVariable))); } return(SystemEnforcementMode.None); }
internal static SafeTransactionHandle Create(Transaction managedTransaction) { IntPtr ptr; if (managedTransaction == null) { throw new InvalidOperationException(RegistryProviderStrings.InvalidOperation_NeedTransaction); } if (RemotingCommandUtil.IsWinPEHost() || PsUtils.IsRunningOnProcessorArchitectureARM()) { throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions); } IKernelTransaction dtcTransaction = TransactionInterop.GetDtcTransaction(managedTransaction) as IKernelTransaction; if (dtcTransaction == null) { throw new NotSupportedException(RegistryProviderStrings.NotSupported_KernelTransactions); } HandleError(dtcTransaction.GetHandle(out ptr)); return(new SafeTransactionHandle(ptr)); }
private static string GetBaseFolder(RunspaceConfiguration runspaceConfiguration, Collection <string> independentErrors) { string shellPathFromRegistry = CommandDiscovery.GetShellPathFromRegistry(runspaceConfiguration.ShellId); if (shellPathFromRegistry == null) { return(Path.GetDirectoryName(PsUtils.GetMainModule(Process.GetCurrentProcess()).FileName)); } if (OSHelper.IsWindows) { shellPathFromRegistry = Path.GetDirectoryName(shellPathFromRegistry); } if (!Directory.Exists(shellPathFromRegistry)) { string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string item = StringUtil.Format(TypesXmlStrings.CannotFindRegistryKeyPath, new object[] { shellPathFromRegistry, Utils.GetRegistryConfigurationPath(runspaceConfiguration.ShellId), @"\Path", directoryName }); independentErrors.Add(item); shellPathFromRegistry = directoryName; } return(shellPathFromRegistry); }
private IntPtr GetMainWindowHandle() { #if CORECLR // No System.Diagnostics.Process.MainWindowHandle on CoreCLR; // Returned WindowHandle is used only in 1 case - prompting for credential using GUI dialog, which is not used on Nano, // because on Nano we prompt for credential using console (different code path in 'PromptForCredential' function) return(IntPtr.Zero); #else System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess(); IntPtr mainWindowHandle = currentProcess.MainWindowHandle; while ((mainWindowHandle == IntPtr.Zero) && (currentProcess != null)) { currentProcess = PsUtils.GetParentProcess(currentProcess); if (currentProcess != null) { mainWindowHandle = currentProcess.MainWindowHandle; } } return(mainWindowHandle); #endif }
private static string GetBaseFolder( RunspaceConfiguration runspaceConfiguration, Collection <string> independentErrors) { string pathFromRegistry = CommandDiscovery.GetShellPathFromRegistry(runspaceConfiguration.ShellId); string path; if (pathFromRegistry == null) { path = Path.GetDirectoryName(PsUtils.GetMainModule(Process.GetCurrentProcess()).FileName); } else { path = Path.GetDirectoryName(pathFromRegistry); if (!Directory.Exists(path)) { string directoryName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string str = ResourceManagerCache.FormatResourceString("TypesXml", "CannotFindRegistryKeyPath", (object)path, (object)Utils.GetRegistryConfigurationPath(runspaceConfiguration.ShellId), (object)"\\Path", (object)directoryName); independentErrors.Add(str); path = directoryName; } } return(path); }
private static string GetBaseFolder( RunspaceConfiguration runspaceConfiguration, Collection <string> independentErrors) { string returnValue = CommandDiscovery.GetShellPathFromRegistry(runspaceConfiguration.ShellId); if (returnValue == null) { returnValue = Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); } else { returnValue = Path.GetDirectoryName(returnValue); if (!Directory.Exists(returnValue)) { string newReturnValue = Path.GetDirectoryName(typeof(FormatAndTypeDataHelper).GetTypeInfo().Assembly.Location); string error = StringUtil.Format(TypesXmlStrings.CannotFindRegistryKeyPath, returnValue, Utils.GetRegistryConfigurationPath(runspaceConfiguration.ShellId), "\\Path", newReturnValue); independentErrors.Add(error); returnValue = newReturnValue; } } return(returnValue); }
private static string GetBaseFolder(Collection <string> independentErrors) { return(Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName)); }
private static SystemEnforcementMode GetDebugLockdownPolicy(string path) { if (PsUtils.IsRunningOnProcessorArchitectureARM()) { return(SystemEnforcementMode.Enforce); } s_wasSystemPolicyDebugPolicy = true; // Support fall-back debug hook for path exclusions on non-WOA platforms if (path != null) { // Assume everything under SYSTEM32 is trusted, with a purposefully sloppy // check so that we can actually put it in the filename during testing. if (path.IndexOf("System32", StringComparison.OrdinalIgnoreCase) >= 0) { return(SystemEnforcementMode.None); } using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default)) { using (RegistryKey wldpPolicy = hklm.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\CI\\TRSData")) { if (wldpPolicy != null) { object exclusionPathsKey = wldpPolicy.GetValue("TestPath"); wldpPolicy.Close(); hklm.Close(); if (exclusionPathsKey != null) { string[] exclusionPaths = (string[])exclusionPathsKey; foreach (string exclusionPath in exclusionPaths) { if (path.IndexOf(exclusionPath, StringComparison.OrdinalIgnoreCase) >= 0) { return(SystemEnforcementMode.None); } } } } } } // No explicit debug allowance for the file, so return the system policy if there // is one. return(s_systemLockdownPolicy.GetValueOrDefault(SystemEnforcementMode.None)); } // Support fall-back debug hook for system-wide policy on non-WOA platforms uint pdwLockdownState = 0; Object result = Environment.GetEnvironmentVariable("__PSLockdownPolicy", EnvironmentVariableTarget.Machine); if (result != null) { pdwLockdownState = LanguagePrimitives.ConvertTo <uint>(result); return(GetLockdownPolicyForResult(pdwLockdownState)); } // If the system-wide debug policy had no preference, then there is no enforcement. return(SystemEnforcementMode.None); }
protected override void ProcessRecord() { if ((int)this.ComputerName.Length > 0 && (this._fileversioninfo.IsPresent || this._module.IsPresent)) { Exception invalidOperationException = new InvalidOperationException(ProcessResources.NoComputerNameWithFileVersion); ErrorRecord errorRecord = new ErrorRecord(invalidOperationException, "InvalidOperationException", ErrorCategory.InvalidOperation, this.ComputerName); base.ThrowTerminatingError(errorRecord); } foreach (Process process in base.MatchingProcesses()) { if (!this._module.IsPresent || !this._fileversioninfo.IsPresent) { if (!this._module.IsPresent) { if (!this._fileversioninfo.IsPresent) { base.WriteObject(process); } else { try { base.WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true); } catch (InvalidOperationException invalidOperationException2) { InvalidOperationException invalidOperationException1 = invalidOperationException2; base.WriteNonTerminatingError(process, invalidOperationException1, ProcessResources.CouldnotEnumerateFileVer, "CouldnotEnumerateFileVer", ErrorCategory.PermissionDenied); } catch (ArgumentException argumentException1) { ArgumentException argumentException = argumentException1; base.WriteNonTerminatingError(process, argumentException, ProcessResources.CouldnotEnumerateFileVer, "CouldnotEnumerateFileVer", ErrorCategory.PermissionDenied); } catch (Win32Exception win32Exception3) { Win32Exception win32Exception = win32Exception3; try { if (win32Exception.ErrorCode != 0x12b) { base.WriteNonTerminatingError(process, win32Exception, ProcessResources.CouldnotEnumerateFileVer, "CouldnotEnumerateFileVer", ErrorCategory.PermissionDenied); } else { base.WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true); } } catch (Win32Exception win32Exception2) { Win32Exception win32Exception1 = win32Exception2; base.WriteNonTerminatingError(process, win32Exception1, ProcessResources.CouldnotEnumerateFileVer, "CouldnotEnumerateFileVer", ErrorCategory.PermissionDenied); } } catch (Exception exception1) { Exception exception = exception1; CommandsCommon.CheckForSevereException(this, exception); base.WriteNonTerminatingError(process, exception, ProcessResources.CouldnotEnumerateFileVer, "CouldnotEnumerateFileVer", ErrorCategory.PermissionDenied); } } } else { try { base.WriteObject(process.Modules, true); } catch (Win32Exception win32Exception7) { Win32Exception win32Exception4 = win32Exception7; try { if (win32Exception4.ErrorCode != 0x12b) { base.WriteNonTerminatingError(process, win32Exception4, ProcessResources.CouldnotEnumerateModules, "CouldnotEnumerateModules", ErrorCategory.PermissionDenied); } else { base.WriteObject(process.Modules, true); } } catch (Win32Exception win32Exception6) { Win32Exception win32Exception5 = win32Exception6; base.WriteNonTerminatingError(process, win32Exception5, ProcessResources.CouldnotEnumerateModules, "CouldnotEnumerateModules", ErrorCategory.PermissionDenied); } } catch (Exception exception3) { Exception exception2 = exception3; CommandsCommon.CheckForSevereException(this, exception2); base.WriteNonTerminatingError(process, exception2, ProcessResources.CouldnotEnumerateModules, "CouldnotEnumerateModules", ErrorCategory.PermissionDenied); } } } else { ProcessModule processModule = null; try { ProcessModuleCollection modules = process.Modules; foreach (ProcessModule module in modules) { processModule = module; base.WriteObject(module.FileVersionInfo, true); } } catch (InvalidOperationException invalidOperationException4) { InvalidOperationException invalidOperationException3 = invalidOperationException4; base.WriteNonTerminatingError(process, invalidOperationException3, ProcessResources.CouldnotEnumerateModuleFileVer, "CouldnotEnumerateModuleFileVer", ErrorCategory.PermissionDenied); } catch (ArgumentException argumentException3) { ArgumentException argumentException2 = argumentException3; base.WriteNonTerminatingError(process, argumentException2, ProcessResources.CouldnotEnumerateModuleFileVer, "CouldnotEnumerateModuleFileVer", ErrorCategory.PermissionDenied); } catch (Win32Exception win32Exception11) { Win32Exception win32Exception8 = win32Exception11; try { if (win32Exception8.ErrorCode != 0x12b) { base.WriteNonTerminatingError(process, win32Exception8, ProcessResources.CouldnotEnumerateModuleFileVer, "CouldnotEnumerateModuleFileVer", ErrorCategory.PermissionDenied); } else { base.WriteObject(processModule.FileVersionInfo, true); } } catch (Win32Exception win32Exception10) { Win32Exception win32Exception9 = win32Exception10; base.WriteNonTerminatingError(process, win32Exception9, ProcessResources.CouldnotEnumerateModuleFileVer, "CouldnotEnumerateModuleFileVer", ErrorCategory.PermissionDenied); } } catch (Exception exception5) { Exception exception4 = exception5; CommandsCommon.CheckForSevereException(this, exception4); base.WriteNonTerminatingError(process, exception4, ProcessResources.CouldnotEnumerateModuleFileVer, "CouldnotEnumerateModuleFileVer", ErrorCategory.PermissionDenied); } } } }