Exit PowerShell script in controlled fashion using keyboard shortcut - powershell

I have a PowerShell script that performs numerous file management tasks and occasionally I have the need to terminate the script before it has finished processing. In order to terminate the script cleanly and not leave any files dotted around, I have the script read in a variable from a config file every time the foreach of the gci results is processed, as below:
Get-ChildItem $fileDir -Filter *.doc | Foreach-Object {
Get-Content $confFile | Foreach-Object {
$var = $_.Split('=')
New-Variable -Name $var[0] -Value $var[1] -Force
}
if ($process -eq "TRUE") {
<process File operations>
}
}
This allows me to change the value of process in the config file to anything but TRUE and the script will skip the process (although still loop though until complete).
Is there anyway to use a keyboard shortcut to exit the script in a controlled fashion? i.e. after an iteration of the foreach loop has completed. e.g. Press Ctrl+Q and it will exit cleanly as opposed to Ctrl+C.

Related

Is there any way to geth less CPU Usage, by a powershell Script like mine

I wrote a PowerSehll Script, which reads a ini file and creats a new one with the Syntax my data needs, in an infinite Loop. It looks like this:
for(;;) {
$fileToCheck = "C:\test\test.ini"
if (Test-Path $fileToCheck -PathType leaf)
{
$ini = Get-Content -Path C:\test\test.ini
$out = "[Event]`nTop = DISPLAY`n[Display_00000]`nDisplay=" + $ini
Add-Content -Path C:\test\exit.txt -Value $out
Remove-Item C:\test\hallo.ini
}
}
I don't know if this is the best way, but it has to be a powershell Script. If it starts via the Task Scheduler by starting the Computer, I can see in the TaskManager that it takes more than 30% of the CPU Usage.
Is this normal for a script like this? And is there a way to reduce the amount of CPU Usage?
You're reading and writing files in infinite loop without any delay, so the script performs as many of those operations as it can in a unit of time.
If you want to monitor a file for changes, you can play around with something like this:
Get-Content C:\TMP\in.txt -Wait | Add-Content c:\tmp\out.txt -Force -PassThru
-Wait causes Get-Content to keep the file open and output any new lines to the pipeline. It checks for changes every second, so cpu usage will be low. -PassThru outputs the lines to console, so it's useful for testing and you can remove it later.

Event for when PowerShell finishes a command?

I am looking to create a InputObject for The Register-ObjectEvent cmdlet.
The object needs to hold the event for when a PowerShell command finishes.
So if I type dir into PowerShell it gets registered with the Register-ObjectEvent.
I am having trouble finding the event for when powershell finishes a command, or if there is even one?
My code at the moment is just:
PS C:\>$CommandFinishWatcher = /* Watches for a finished powershell command. */
PS C:\>register-objectEvent -InputObject $CommandFinishWatcher -EventName "PowerShellCommandFinished"
You can use the automatic variable $? to check the status of the most recent command that powershell ran. It contains the execution status of the last operation.
https://ss64.com/ps/syntax-automatic-variables.html
Get-Content -path C:\Test
if($? = "FALSE")
{Write-Host "The get-content command failed."}
if($? = "TRUE")
{Write-Host "The get-content command succeeded."}

PowerShell to monitor a text file

I have a PowerShell script running on a remote machine. I have it writing data to a text file as it completes its work. Once it's done it writes a specific line.
I have this in the local PowerShell script to monitor that file on the remote machine:
Get-Content -Path $Path -Tail 0 -Wait
It is working great, but how do I tell it to stop monitoring once that specific line is reached?
I tired putting it into a do while loop, but it never releases to complete the do while.
Here is a link to a simpler version of what I am asking:
How to monitor a text file in realtime
The first answer is good, but I don't want to just look for a certain line. I want to write them all VIA Write-Host till that phrase then break from Get-Content and continue with the remaining parts of the script.
Here is what I finally ended up with. It is not pretty due to the way I exited the ForEach-Object.
Get-Content -Path $path -Tail 0 -wait | ForEach-Object{if($_ -match $word){write-host "- $_" ;cjklnsrvf } else {write-host "- $_"} }
I used a Try and Catch for the cjklnsrvf in the if statement above. This is done because ForEach-Object cannot use the break or continue statements. It seems that when piping a ForEach loop it is turned into (alias) the ForEach-Object cmdlet. The ForEach-Object cmdlet doesn't use the break and continue commands like a foreach loop.
If you use a break in a ForEach-Object it will immediately exit the whole script. There was one guy on one site that brought up loop death by garbage, and it indeed does work here as well.
UPDATE: Here is what I finally ended up with. It is not pretty due to the way I exited the foreach-object.
Get-Content -Path $path -Tail 0 -wait | ForEach-Object{if($_ -match $word){write-host "- $_" ;cjklnsrvf } else {write-host "- $_"} }
I used a Try and Catch for the cjklnsrvf in the if statement above. This is done because ForEach-Object cannot use the break or continue statements. It seems that when piping a ForEach loop it is turned into(alias) the ForEach-Object cmdlet. The ForEach-Object cmdlet doesn't use the break and continue commands like a foreach loop. If you use a break in a ForEach-Object it will immediately exit the whole script. There was one guy on one site that brought up loop death by garbage and it indeed does work here as well.
Get-Content $path -Tail 0 -Wait | foreach { if ($_ -eq "Specific Line") { Write-Output $_ ; break } }
Or just the break, obviously, if you have no use of the output.
Do {
$content = get-content $path -tail 0 -ea 00 | where {$_ -like "*string found*"}
Sleep -milliseconds 1000
} until($content)
Invoke-Command -ComputerName xxx -Credential $cred -ScriptBlock {if(test-path 'c:\1.txt'){cat 'c:\1.txt'}else{'file not exist'}}

Count number of scripts running and wait for them to finish

I'm looking for the best way to count the number of PowerShell scripts that are currently running.
I run .ps1 scripts from windows batch files. The script I am working on now is launched when a particular email is received from a client - but I want this script to first of all check that no other scripts are busy running at the moment, and if they are it must wait for them to finish before it continues.
I'm sure there are a few ways to go about this, but what would be the safest? I am still learning.
If it is possible to move away from batch files to launch PowerShell then I would suggest using Start-Process to launch your scripts. This will allow you to wait for your processes to exit using where-object and Measure-Object to filter the scripts that have not yet completed.
So your script might look something like this:
# create a loop
foreach ($item in $reasontoloop) {
$arguments = "define script names and arguments"
# Start the powershell script
$procs += Start-Process powershell -PassThru -argumentlist $arguments
}
Write-Host -message "Waiting for Processes to complete"
while( $procs | Where-Object { $_.hasExited -eq $false } )
{
# Display progress
$measureInfo = $procs | Where-Object { $_.hasExited -eq $true } | Measure-Object
write-host "$($measureInfo.count) of $($procs.Length) still running"
Start-Sleep 1
}
Write-Host -message "Processes complete"
If you are simply interested in the number of PowerShell instances executing then the following one liner using Get-Process will help.
#(Get-Process | where-object {$_.ProcessName -like 'powershell'}).count

Powershell pipe into exe and wait

I am piping an array of data into a executable program but I need it to block after every call in the foreach loop. It will leave the loop before it even opens the program from the first call.
Set-Alias program "whatever.exe"
foreach ($data in $all_data)
{
$data| %{ program /command:update /path:"$_" /closeonend:2 }
}
I like PowerShell but I never really learned Invoke-Command. So whenever I need to run an EXE I always use cmd. If you type cmd /? you get its help, look at the "c" switch. I'd do something like this:
foreach ($data in $all_data){
$data |
Foreach-Object{
cmd /c "whatever.exe" /command:update /path:"$_" /closeonend:2
}
}
If you don't like the cmd /c thing you could use Jobs.
foreach ($data in $all_data){
$data |
Foreach-Object{
$job = Start-Job -InitializationScript {Set-Alias program "whatever.exe"} -ScriptBlock {program /command:update /path:"$($args[0])" /closeonend:2} -ArgumentList $_
while($job.Status -eq 'Running'){
Start-Sleep -Seconds 3
#Could make it more robust and add some error checking.
}
}
}
I can think of two ways to tackle this:
pipe your executable call to Out-Null
shell out the call to cmd.exe /c (as shown in #BobLobLaw's answer)
I made your sample code a little more specific so I could run and test my solutions; hopefully it'll translate. Here's what I started with to be equivalent to your sample code, i.e. the script executes with no waiting on the executable to finish.
# I picked a specific program
Set-Alias program "notepad.exe"
# And put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# This opens each file in notepad; three instances of notepad are running
# when the script finishes executing.
$all_data | %{ program "$_" }
Here's the same code as above, but piping to Out-Null forces the script to wait on each iteration of the loop.
# I picked a specific program
Set-Alias program "notepad.exe"
# And put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# Piping the executable call to out-null forces the script execution to wait
# for the program to complete. So in this example, the first document opens
# in notepad, but the second won't open until the first one is closed, and so on.
$all_data | %{ program "$_" | Out-Null}
And, lastly, the same code (more or less) using cmd /c to call the executable and make the script wait.
# Still using notepad, but I couldn't work out the correct call for
# cmd.exe using Set-Alias. We can do something similar by putting
# the program name in a plain old variable, though.
#Set-Alias program "notepad.exe"
$program = "notepad.exe"
# Put some values in $all_data, specifically the paths to three text files.
$all_data = Get-Item B:\matt\Documents\*.txt
# This forces script execution to wait until the call to $program
# completes. Again, the first document opens in notepad, but the second
# won't open until the first one is closed, and so on.
$all_data | %{ cmd /c $program "$_" }
Depending on your scenario, wait-job might be overkill. If you have a programmatic way to know that whatever.exe has done its thing, you could try something like
do {start-sleep -sec 2} until ($done -eq $true)
Oh and.