Powershell async launching - multithreading

I have a powershell script which executes from a c# call (ex.. Process.Start(powershell.exe file.ps1);)
notepad.exe filename
this however seems to block script completion, as the event of completion is not fired until the notepad dialog is closed.
Is there a way for me to start notepad.exe but let the script complete execution. Maybe a powershell thing I missed. Preferably it would be something that allows the rest of file.ps1 to be blocking while just that line is async.

If you can use PowerShell 2.0, then go for background jobs (PsJob).
A PsJob runs a command or expression asynchronously and "in the background"
without interacting with the console. The command prompt returns immediately and
you can query for the job results at your convenience. You can run background
jobs on a local or remote computer.
Also, check this:
Is it possible to call powershell cmdlets asynchronously?

Related

Powershell scripts going to sleep

I have a powershell script which loops over each file in a folder and does a lot of "high-computation" tasks on each of these files. I have multiple instances of this script running on my system.
I do use a write-host before starting each of these tasks to see when these tasks start and end.
I started these instances of the script last night and when I came today morning I noticed that some of my powershell terminal were stuck at a write-host command and when I pressed ENTER they wrote that content to terminal and continued the preocessing.
It looks like some of these terminals go to sleep. Why am i seeing this and how can i prevent this?
Disable the "QuickEdit" option in the "Windows PowerShell" Properties window.
Right click the PowerShell window to get the to the Properties window.

Task Scheduler loads script into editor instead of running it

My AutoIt script that executes from Windows Task Scheduler under Windows 7 works fine. On my Windows 10 machine with the identical task, instead of running the script, the Task Scheduler loads it into SciTE (an editor that accompanies AutoIt).
When I execute the script from within Explorer it runs. When I execute it from a command line it runs. It's only from Task Scheduler that I get this misbehavior. How do you tell Task Scheduler I want to run the script, not edit it?
If you do not wish to compile it, you can pass the script name to the AutoIt executable. For example:
"C:\Program Files (x86)\AutoIt3\AutoIt3.exe" "C:\Scripts\Test.au3"
This is how it would look in Task Scheduler:
And you can read more about AutoIt command line switches at this
online help page.

Cocoa = How to run script in background without launching terminal in C

In my Cocoa Application, I am running a script by using c functions like setenv() and popen(). It executes the script perfectly. But the issue is, At runtime, popen() opens the Terminal app and after the script is executed, It closes the Terminal app automatically. I want to execute the script without opening the Terminal app.
I found a solution to use NSAppleScript. But This class is available only in Foundation, but not available in Core Foundation. I want the same feature to be written in C. How to achieve this.
Please advice.
Use NSTask to execute the script. This allows you to have it run in background while still being able to specify environment variables, monitor output, etc.

Powershell script not running on Task Scheduler

I have a PS script that opens Excel (Com Object), processes a bunch of information, re-saves, and then sends some critical information via e-mail. The script runs great, and when I run it from the Run Console, it works great as well. However, when I schedule it as a task in the Task Scheduler it is not working properly. The task seems to "successfully run" every single time, but I do not get the output e-mail that I am supposed to get. I have run many other PS Scripts without a problem using the same configuration in the task scheduler. Could this have something to do with opening Excel as part of a script scheduled? Any thoughts are welcome.
I solved this issue using the answer from #briantist in PowerShell script won't execute as a Windows scheduled task, but I wanted to isolate exactly which switch was solving the problem.
It had nothing to do with -ExecutionPolicy, -Noninteractive, -NoLogo, -NoProfile or any other system privilege, user account running the script, etc.
Just needed to add -File in front of the script path in the Task Scheduler > Actions > Arguments field. Without this switch PowerShell was launching and the task history was showing Action Completed, but the script was not executing.

Fork process with CC .NET

Is there any way to fork a process inside Cruise Control .NET? I want CC .NET to launch a program when everything's done but right now, CC .NET insists on waiting for the program to close before saying that the build is complete.
The program that is launched must run for up to weeks at a time.
The way I would do it is to use a PowerShell or Batch script. The script can launch your process and return back normally to CC.NET after spawning the executable. This is the only way I can see doing it, as CC.NET does need to know it returned and the script can return, even with the process you spawned still out there running. A sample PowerShell script that will do what you want. Then you just call the powershell script with the path to the exe as a paratemeter.
param( [string] $exePath = $(throw "A path to executable required.")
)
Invoke-Item $exePath
here on CC.NET 1.5 would be how to set up the powershell task
<powershell>
<script>yourScriptName.ps1</script>
<scriptsDirectory>D:\CruiseControl</scriptsDirectory>
<buildArgs>full path to exe you want to kick-off</buildArgs>
</powershell>
How about this... 2 Small programs
1) A process that runs all the time (maybe a windows service?), and listens on a tcp socket, when it gets a connection, execute your 3 week process.
2) A process that can be called by CC.NET, and opens a tcp connection to process #1, and then exits.
You could use any form of inter-process communication, but TCP sockets are easy and reliable.
Put the launch of the program into a separate CCNET project and add a ForceBuildPublisher to the original project.
<project name="OriginalProject">
<!-- ... -->
<publishers>
<!-- ... -->
<forcebuild>
<project>LaunchProgram</project>
</forcebuild>
</publishers>
</project>
I haven't tried the solution with powershell(the selected answer), but I use ruby rake and whatever I tried to fork independent process group with ruby library, CruiseControl STILL INSIST on waiting for the asynchronous process to finish.
For me; The only way to fork independent daemon process /server application successfully with CCNET is using task scheduler (https://stackoverflow.com/a/14679992/423356)
so use schtasks.exe to create a throwaway run once task to start your process
you can call schtasks and set the start time dynamically using batch file, ruby rake or powershell.

Resources