Indicate command line activation successfully completed UWP - c#

I am working on a text-editor app that can be activated via command line. The argument provided is treated as path to file, i.e. pad launches the app but pad "C:\Sample.txt" launches the app and then displays the "Sample.txt" file. This works well in command prompt, but in power-shell after command is executed, the execution completes only after the app is closed. Also, the execution is recorded as failed. Is there any way I can notify the command to be completed successfully from my app itself??
Example of the difference in command line activation of Windows Terminal in both command prompt and power-shell:
https://drive.google.com/file/d/15gBiCcio3feGwxappgyBm68lezK5I0t2/view?usp=sharing

Since you are comparing cmd.exe vs powershell.exe uwpapp starts. Remember the cmd.exe simply does not hold/wait for the process/exe it started, whereas PowerShell does for uwp at least, by design.
As a workaround, if you want the PowerShell consolehost to immediately be available for more interactive steps, then you can use jobs to simulate what you are seeing when using cmd.exe. So, from a WT PowerShell session...
Start-Job -ScriptBlock {wt}

Related

SSH.NET responds differently to OpenSSH when calling a CLI application [duplicate]

I created a program using Renci SSH.NET library. Its sending all the commands and reading the result normally. However, when I send the command below:
client.RunCommand("cli");
The program hangs on this line indefinitely.
Any explanation of what is happening?
The cli is a command is used on Juniper switches/routers.
AFAIK, cli is a kind of a shell/interactive program. So I assume you have tried to do something like:
client.RunCommand("cli");
client.RunCommand("some cli subcommand");
That's wrong. cli will keep waiting for subcommands and never exit, until you explicitly close it with a respective command (like exit). And after it exits, the server will try to execute the cli subcommand as a separate top-level command, failing too.
You have to feed the "cli subcommand" to the input of the cli command. But SSH.NET unfortunately does not support providing an input with the SshClient.RunCommand/SshClient.CreateCommand interface. See Allow writing to SshCommand.
There are two solutions:
Use the appropriate syntax of the server's shell to generate the input on the server, like:
client.RunCommand("echo \"cli subcommand\" | cli");
Or use a shell session (what is otherwise a not recommended approach for automating a command execution).
Use SshClient.CreateShellStream or SshClient.CreateShell and send the commands to its input:
"cli\n" + "cli subcommand\n"
For a sample code see Providing subcommands to a command (sudo/su) executed with SSH.NET SshClient.CreateShellStream or C# send Ctrl+Y over SSH.NET.

Powershell script only running in ISE

Running a script that starts and kills/cleans two jobs via batch file before I package it for an EXE, works great in ISE elevated but immediately fails in console or command prompt with the following:
The code I have put together is here: https://pastebin.com/FWaZD249
I tested it with:
PS1 to EXE, get the same results
Non-elevated ISE same results
Elevated console same results
Elevated CMD same results
Elevated ISE works (only after saving?)
It's really close to being done, basically, it's just a little script that checks the 5900 port for established connections, updates a form of a list of connections, and sends a little notification if someone new has connected, it runs on our print server computer which is screen-shared remotely via TightVNC, so operators don't get surprised when their mouse starts moving on them.
cmd batch code looks like this
powershell.exe -NoExit ". C:\Users\VS-Print-Server\Desktop\Userchecker.PS1"
As I mentioned in the comment: you initialize the jobs $job1 and job2 with $FormLib respectively $LisLib before those variables are set/initialized. Moving line 1 through 30 (everything before Write-Verbose -Verbose 'Before:') to the very end should make it work.

Console application starting another process environment variables not accessible

I have a VB6 executable which is accessing some system environment variables. I have implemented a .NET console application which checks if those environment variables exist, creates them if needed, and then runs the VB6 application by calling Process.Start.
Doing this, the VB6 application cannot find the environment variables and it says they don't exist.
If I run the VB6 application from Windows Explorer it works fine and can find the variables.
So it seems the VB6 app is running under the context of .NET console app and cannot access the system environment variables!
Code to set the environment vars .NET Cosnole app:
foreach(var varObject in Variables)
{
var envVar = Envrionment.GetEnvironmentVariable(varObject.Name ,
EnvironmentVariableTarget.Machine);
if(string.IsNullOrEmpty(envVar)
{
Environment.SetEnvironmentVariable(varObject.Name,varObject.Value,
EnvironmentVariableTarget.Machine);
}
}
Code to run the VB6 app from .NET Cosnole app:
var processInfo = new ProcessStartInfo(VB6ApplicationFilePath);
processInfo.UseShellExecute = true
processInfo.WindwoStyle= ProcessWindowStyle.Hidden;
Process.Start(processInfo);
A copy of a program's environment is passed to a program that it starts. As it is a copy the second program only sees the state it was in when given it (and changes it made). No other program can change another program's environment.
When using ShellExecute (which you tell ProcessStart to) you are asking Explorer to start the program for you. The program will get a copy of Explorer's environment.
When changing the system environment, programs can send a message to all windows open saying environment has changed (as setx does - see setx /?). But ONLY Explorer.exe pays attention to this message. So only programs started by explorer after explorer receives this message will see the changes.
These are the API calls that .NET calls. In Windows all programs are started by CreateProcessEx (or older programs CreateProcess). Shellexecute and ShellexecuteEx process the command like you typed it in Explorer's Start - Run dialog (Winkey + R) then changes it and calls CreateProcessEx.
At the command prompt. Type
set MyCat=PewResearch
cmd /k echo %MyCat%
We set an environment variable, start a new command prompt that prints that variable.
This is the message that notifies
WM_SETTINGCHANGE
The system sends the WM_SETTINGCHANGE message to all
top-level windows when the SystemParametersInfo function changes a
system-wide setting or when policy settings have changed.
Applications should send WM_SETTINGCHANGE to all top-level windows
when they make changes to system parameters. (This message cannot be
sent directly to a window.) To send the WM_SETTINGCHANGE message to
all top-level windows, use the SendMessageTimeout function with the
hwnd parameter set to HWND_BROADCAST.

C# How to disable linux terminal user input during the execution of a Console Application

When I run my console application in windows, unless I'm actively reading input (i.e. via Console.Read...) nothing happens when I type in the console.
When I'm running the same console application in linux, whatever I type is inserted in the middle of what the application is writing to the console at the time.
Is there any way to prevent/capture/flush the user input in the terminal during the execution of the program?
You can disable echo on the shell before running your program and re-enable it afterwards:
$ stty_bak=`stty -g`
$ stty -echo
$ <run your command / program>
$ stty $stty_bak

Launch a command shell from a console application run via Windows Task Scheduler

I have a Windows console application that is launched via a schedule setup in Task Scheduler. This console application, as part of its normal runtime, will launch a command prompt in order to run a java program. No, I have no control over the design of the Java program. It was supplied to me as is and I have no rights or access to make changes to it. I also cannot implement it in another language. I must use what was given to me.
At any rate, when my console application tries to run the command prompt it will work just fine if I'm launching the application manually. However, when I try it as an action within Task Scheduler, my console application will start and run as expected until it needs to launch the command prompt. At this point, the console application exits. No error message or code is provided.
How do I get the command prompt window to start as a new window from within my console application when no one is logged into the server?
Thanks for any hints or suggestions you can provide.
* UPDATE *
Here is the code snippet that launches the program from within my console application:
string parameter_save_path = #"C:\output\folder"
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo start_info = new System.Diagnostics.ProcessStartInfo();
start_info.WorkingDirectory = #"C:\mtselect-client";
start_info.FileName = "cmd.exe";
start_info.Arguments = "/C run.bat \"" + parameter_save_path + "\"";
process.StartInfo = start_info;
process.Start();
process.WaitForExit();
The run.bat is what launches the java program.
I think it's too late for this message, but...
Maybe in your batch file you are running your java application with something like: java -jar ApplicationName
First I would do should be comment out the "#echo off" from the batch file, next trace out the batch lines with one echo "x" (being x a natural number starting from 1 and increasing by 1 in each ocurrence). Next I will add a line with java -version, and so I will be sure java app is installed and accesible.
Maybe java needs be ran by an authenticated user and so have java_home defined. Maybe the application needs some JVM parameters like memory size, etc.
Have good luck, tell me and I will try to help

Categories