Close powershell window after script runs

Hi Don I've tried making use of the exit and also Exit-PSSession regulates at the end of my script to do this yet its not functioning. In my case the scenario is a little more facility because I am using the line &"$env:WINDIRsyswow64windowspowershellv1.0powershell.exe" -NonInteractive -NoProfile $myInvocation.Line in order to force my script to run in 32little bit mode. I suspect this may be resulting in my consingle window to continue to be open at the finish of my script run. I additionally am launching my manuscript from C# and also require the call to Invoke that I am making use of to execute the script to return so that I deserve to take the output of the script and carry out some stuff on it. Do you have actually any principle how I can get my manuscript to return properly?



Don Jonesover 8 years ago

Well, commonly, the shell will certainly cshed immediately if you use -Command to run a script. I think you're best in that it's the technique you're utilizing.

You watching: Close powershell window after script runs

I'm curious, though - if you're in C#, why are you shelling out to run a manuscript, quite than simply instantiating PowerShell's .NET engine straight in code? The means you're doing it is absolutely not what Microsoft intfinished or built to job-related...

See more: Viewing Details Of The Key Kaspersky Internet Security 2021, How To Get A Key File From An Activation Code


*

dharricover 8 years agoin reply to Don Jones

Thanks for responding to my query. Its really quite nice of you to take the moment on a weekfinish.Now if I understand your question,Iactually am utilizing the System.Management.Automation namearea in my .Net app and also calling the powershell script utilizing Invoke. However before the scenario that I have is I am using powershell to upday a 32little bit accessibility file (we're stuck on Office 2007), but our .Net application is 64little bit. So I necessary a way to switch my powershell setting to 32little at runtime, which is that one line I proved you prior to.

See more: My Ssd Is Full And I Don'T Know Why Is My Ssd Full For No Reason

Maybe if I show you some of the code I'm running this will make more sense. But to reiteprice the strangeness right here is that on my regional dev box this code returns without worry. Its just on a 2008 server that it does this weird not returning point. I think I might need to revert to using .Net's commandline consingle launcher to execute my powershell script. But I'm hoping you'll present me a method roughly this. Thanks aobtain.

This is the C# code that calls my script:

System.Management.Automation.Runspaces.Runspace lRunroom = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); lRunarea.Open();

making use of (System.Management.Automation.RunspaceInvoke lRunspaceInvoker = brand-new System.Management.Automation.RunspaceInvoke(lRunspace)) lRunspaceInvoker.Invoke("Set-ExecutionPolicy RemoteSigned");

utilizing (System.Management.Automation.PowerCovering lPowershell = System.Management.Automation.PowerCovering.Create()) bool lDoDebug = true; attempt LogWriter.LogInfo("LaunchPowershell Setting up powershell script execution."); var lAppSetups = System.Configuration.ConfigurationManager.AppSettings; string lAccessScriptPath = lAppSettings<"MtpAccessScriptPath">; string lMtpXmlScriptPath = lAppSettings<"MtpXmlDestinationPath"> + "\MtpToPertrac.xml"; string lPowershellScriptPath = lAppSettings<"MtpPowershellScriptPath">; string lDoDebugStr = lAppSettings<"MtpToPertracDebug">; lDoDebug = lDoDebugStr == "True" ? true : false; lPowershell.Runroom = lRunspace; lPowershell.Commands.AddScript(lPowershellScriptPath);

Collection lResults = new Collection(); Collection lErrors = new Collection();

try LogWriter.LogInfo("LaunchPowershell start to Invoke powershell."); lResults = lPowershell.Invoke(); lErrors = lPowershell.Streams.Error.ReadAll(); LogWriter.LogInfo("LaunchPowershell powershell script was invoked"); capture (Exception ex) lResults.Add(new System.Management.Automation.PSObject((object)ex.Message)); LogWriter.LogException(ex);

if (lDoDebug) // if in debug mode write log file StringBuilder lStr = brand-new StringBuilder(); foreach (var obj in lResults) lStr.AppendLine(obj.ToString()); foreach (var obj in lErrors) lStr.AppendLine(obj.ToString());

string lLog = lStr.ToString(); if (!string.IsNullOrEmpty(lLog)) making use of (MemoryStream lMs = new MemoryStream()) catch (Exception ex) LogWriter.LogException(ex); finally lRunspace.Close();

rerevolve null;

This is the powershell manuscript area that does the switch to 32bit:

if($env:Processor_Architecture -ne "x86") write-warning "Running x86 Powershell ...." &"$env:WINDIRsyswow64windowspowershellv1.0powershell.exe" -NonInterenergetic -NoProfile $myInvocation.Line exit