/// <summary> /// /// </summary> protected override void BeginProcessing() { //create an instance of the ProcessStartInfo Class ProcessStartInfo startInfo = new ProcessStartInfo(); string message = String.Empty; //Path = Mandatory parameter -> Will not be empty. try { CommandInfo cmdinfo = CommandDiscovery.LookupCommandInfo( FilePath, CommandTypes.Application | CommandTypes.ExternalScript, SearchResolutionOptions.None, CommandOrigin.Internal, this.Context); startInfo.FileName = cmdinfo.Definition; } catch (CommandNotFoundException) { startInfo.FileName = FilePath; } //Arguments if (ArgumentList != null) { StringBuilder sb = new StringBuilder(); foreach (string str in ArgumentList) { sb.Append(str); sb.Append(' '); } startInfo.Arguments = sb.ToString(); ; } //WorkingDirectory if (WorkingDirectory != null) { //WorkingDirectory -> Not Exist -> Throw Error WorkingDirectory = ResolveFilePath(WorkingDirectory); if (!Directory.Exists(WorkingDirectory)) { message = StringUtil.Format(ProcessResources.InvalidInput, "WorkingDirectory"); ErrorRecord er = new ErrorRecord(new DirectoryNotFoundException(message), "DirectoryNotFoundException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } startInfo.WorkingDirectory = WorkingDirectory; } else { //Working Directory not specified -> Assign Current Path. startInfo.WorkingDirectory = ResolveFilePath(this.SessionState.Path.CurrentFileSystemLocation.Path); } if (this.ParameterSetName.Equals("Default")) { if (_isDefaultSetParameterSpecified) { startInfo.UseShellExecute = false; } //UseNewEnvironment if (_UseNewEnvironment) { ClrFacade.GetProcessEnvironment(startInfo).Clear(); LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)); LoadEnvironmentVariable(startInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)); } #if !CORECLR // 'WindowStyle' not supported in CoreCLR if (_nonewwindow && _windowstyleSpecified) { message = StringUtil.Format(ProcessResources.ContradictParametersSpecified, "-NoNewWindow", "-WindowStyle"); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } //WindowStyle startInfo.WindowStyle = _windowstyle; #endif //NewWindow if (_nonewwindow) { startInfo.CreateNoWindow = _nonewwindow; } //LoadUserProfile. if (Platform.IsWindows) { startInfo.LoadUserProfile = _loaduserprofile; } if (_credential != null) { //Gets NetworkCredentials NetworkCredential nwcredential = _credential.GetNetworkCredential(); startInfo.UserName = nwcredential.UserName; if (String.IsNullOrEmpty(nwcredential.Domain)) { startInfo.Domain = "."; } else { startInfo.Domain = nwcredential.Domain; } #if CORECLR startInfo.PasswordInClearText = ClrFacade.ConvertSecureStringToString(_credential.Password); #else startInfo.Password = _credential.Password; #endif } //RedirectionInput File Check -> Not Exist -> Throw Error if (_redirectstandardinput != null) { _redirectstandardinput = ResolveFilePath(_redirectstandardinput); if (!File.Exists(_redirectstandardinput)) { message = StringUtil.Format(ProcessResources.InvalidInput, "RedirectStandardInput '" + this.RedirectStandardInput + "'"); ErrorRecord er = new ErrorRecord(new FileNotFoundException(message), "FileNotFoundException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } } //RedirectionInput == RedirectionOutput -> Throw Error if (_redirectstandardinput != null && _redirectstandardoutput != null) { _redirectstandardinput = ResolveFilePath(_redirectstandardinput); _redirectstandardoutput = ResolveFilePath(_redirectstandardoutput); if (_redirectstandardinput.Equals(_redirectstandardoutput, StringComparison.CurrentCultureIgnoreCase)) { message = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardInput", "RedirectStandardOutput"); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } } //RedirectionInput == RedirectionError -> Throw Error if (_redirectstandardinput != null && _redirectstandarderror != null) { _redirectstandardinput = ResolveFilePath(_redirectstandardinput); _redirectstandarderror = ResolveFilePath(_redirectstandarderror); if (_redirectstandardinput.Equals(_redirectstandarderror, StringComparison.CurrentCultureIgnoreCase)) { message = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardInput", "RedirectStandardError"); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } } //RedirectionOutput == RedirectionError -> Throw Error if (_redirectstandardoutput != null && _redirectstandarderror != null) { _redirectstandarderror = ResolveFilePath(_redirectstandarderror); _redirectstandardoutput = ResolveFilePath(_redirectstandardoutput); if (_redirectstandardoutput.Equals(_redirectstandarderror, StringComparison.CurrentCultureIgnoreCase)) { message = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardOutput", "RedirectStandardError"); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); WriteError(er); return; } } } #if !CORECLR // 'UseShellExecute' is not supported in CoreCLR else if (ParameterSetName.Equals("UseShellExecute")) { startInfo.UseShellExecute = true; //Verb if (Verb != null) { startInfo.Verb = Verb; } //WindowStyle startInfo.WindowStyle = _windowstyle; } #endif //Starts the Process Process process; if (Platform.IsWindows) { process = start(startInfo); } else { process = new Process(); process.StartInfo = startInfo; SetupInputOutputRedirection(process); process.Start(); if (process.StartInfo.RedirectStandardOutput) { process.BeginOutputReadLine(); } if (process.StartInfo.RedirectStandardError) { process.BeginErrorReadLine(); } if (process.StartInfo.RedirectStandardInput) { WriteToStandardInput(process); } } //Wait and Passthru Implementation. if (PassThru.IsPresent) { if (process != null) { WriteObject(process); } else { message = StringUtil.Format(ProcessResources.CannotStarttheProcess); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } } if (Wait.IsPresent) { if (process != null) { if (!process.HasExited) { if (Platform.IsWindows) { _waithandle = new ManualResetEvent(false); // Create and start the job object ProcessCollection jobObject = new ProcessCollection(); if (jobObject.AssignProcessToJobObject(process)) { // Wait for the job object to finish jobObject.WaitOne(_waithandle); } else if (!process.HasExited) { // WinBlue: 27537 Start-Process -Wait doesn't work in a remote session on Windows 7 or lower. process.Exited += new EventHandler(myProcess_Exited); process.EnableRaisingEvents = true; process.WaitForExit(); } } else { process.WaitForExit(); } } } else { message = StringUtil.Format(ProcessResources.CannotStarttheProcess); ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } } }
protected override void BeginProcessing() { string str; ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.ErrorDialog = false; try { CommandInfo commandInfo = CommandDiscovery.LookupCommandInfo(this._path, CommandTypes.ExternalScript | CommandTypes.Application, SearchResolutionOptions.None, CommandOrigin.Internal, base.Context); processStartInfo.FileName = commandInfo.Definition; } catch (CommandNotFoundException commandNotFoundException) { processStartInfo.FileName = this._path; } if (this._argumentlist != null) { StringBuilder stringBuilder = new StringBuilder(); string[] strArrays = this._argumentlist; for (int i = 0; i < (int)strArrays.Length; i++) { string str1 = strArrays[i]; stringBuilder.Append(str1); stringBuilder.Append(' '); } processStartInfo.Arguments = stringBuilder.ToString(); } if (this._workingdirectory == null) { processStartInfo.WorkingDirectory = this.ResolveFilePath(base.SessionState.Path.CurrentFileSystemLocation.Path); } else { this._workingdirectory = this.ResolveFilePath(this._workingdirectory); if (Directory.Exists(this._workingdirectory)) { processStartInfo.WorkingDirectory = this._workingdirectory; } else { str = StringUtil.Format(ProcessResources.InvalidInput, "WorkingDirectory"); ErrorRecord errorRecord = new ErrorRecord(new DirectoryNotFoundException(str), "DirectoryNotFoundException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord); return; } } if (!base.ParameterSetName.Equals("Default")) { if (base.ParameterSetName.Equals("UseShellExecute")) { processStartInfo.UseShellExecute = true; if (this._verb != null) { processStartInfo.Verb = this._verb; } processStartInfo.WindowStyle = this._windowstyle; } } else { if (this.IsDefaultSetParameterSpecified) { processStartInfo.UseShellExecute = false; } if (this._UseNewEnvironment) { processStartInfo.EnvironmentVariables.Clear(); this.LoadEnvironmentVariable(processStartInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)); this.LoadEnvironmentVariable(processStartInfo, Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)); } if (!this._nonewwindow || !this._windowstyleSpecified) { if (this._nonewwindow) { processStartInfo.CreateNoWindow = this._nonewwindow; } processStartInfo.WindowStyle = this._windowstyle; processStartInfo.LoadUserProfile = this._loaduserprofile; if (this._credential != null) { NetworkCredential networkCredential = this._credential.GetNetworkCredential(); processStartInfo.UserName = networkCredential.UserName; if (!string.IsNullOrEmpty(networkCredential.Domain)) { processStartInfo.Domain = networkCredential.Domain; } else { processStartInfo.Domain = "."; } processStartInfo.Password = this._credential.Password; } if (this._redirectstandardinput != null) { this._redirectstandardinput = this.ResolveFilePath(this._redirectstandardinput); if (!File.Exists(this._redirectstandardinput)) { str = StringUtil.Format(ProcessResources.InvalidInput, string.Concat("RedirectStandardInput '", this.RedirectStandardInput, "'")); ErrorRecord errorRecord1 = new ErrorRecord(new FileNotFoundException(str), "FileNotFoundException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord1); return; } } if (this._redirectstandardinput != null && this._redirectstandardoutput != null) { this._redirectstandardinput = this.ResolveFilePath(this._redirectstandardinput); this._redirectstandardoutput = this.ResolveFilePath(this._redirectstandardoutput); if (this._redirectstandardinput.Equals(this._redirectstandardoutput, StringComparison.CurrentCultureIgnoreCase)) { str = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardInput", "RedirectStandardOutput"); ErrorRecord errorRecord2 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord2); return; } } if (this._redirectstandardinput != null && this._redirectstandarderror != null) { this._redirectstandardinput = this.ResolveFilePath(this._redirectstandardinput); this._redirectstandarderror = this.ResolveFilePath(this._redirectstandarderror); if (this._redirectstandardinput.Equals(this._redirectstandarderror, StringComparison.CurrentCultureIgnoreCase)) { str = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardInput", "RedirectStandardError"); ErrorRecord errorRecord3 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord3); return; } } if (this._redirectstandardoutput != null && this._redirectstandarderror != null) { this._redirectstandarderror = this.ResolveFilePath(this._redirectstandarderror); this._redirectstandardoutput = this.ResolveFilePath(this._redirectstandardoutput); if (this._redirectstandardoutput.Equals(this._redirectstandarderror, StringComparison.CurrentCultureIgnoreCase)) { str = StringUtil.Format(ProcessResources.DuplicateEntry, "RedirectStandardOutput", "RedirectStandardError"); ErrorRecord errorRecord4 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord4); return; } } } else { str = StringUtil.Format(ProcessResources.ContradictParametersSpecified, "-NoNewWindow", "-WindowStyle"); ErrorRecord errorRecord5 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.WriteError(errorRecord5); return; } } Process process = this.start(processStartInfo); if (this._passthru.IsPresent) { if (process == null) { str = StringUtil.Format(ProcessResources.CannotStarttheProcess, new object[0]); ErrorRecord errorRecord6 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord6); } else { base.WriteObject(process); } } if (this._wait.IsPresent) { if (process == null) { str = StringUtil.Format(ProcessResources.CannotStarttheProcess, new object[0]); ErrorRecord errorRecord7 = new ErrorRecord(new InvalidOperationException(str), "InvalidOperationException", ErrorCategory.InvalidOperation, null); base.ThrowTerminatingError(errorRecord7); } else { process.EnableRaisingEvents = true; if (!process.HasExited) { ProcessCollection processCollection = new ProcessCollection(process); processCollection.Start(); processCollection.WaitOne(); return; } } } }