How do I launch powershell task with some script - conemu

I'm a little confused how do I configure a new task in ConEmu to run launch powershell console with -File parameter. if I add -file "filename.ps1" then that file is executed and then console is closed, I'd like to console to rename open.

Related

paramiko ssh does not work with start cmd.exe "" "application.exe" commands in batch script

I am trying to run a batch script from paramiko ssh.
the batch script is like this:
start cmd.exe /k "cd C:\\Users\\Administrator\\iometer\\ && C:\\Users\\Administrator\\iometer\\IOmeter.exe /c C:\\Users\\Administrator\\iometer\\new_config.icf /r C:\\Users\\Administrator\\iometer\\iometer_result_v.csv"
If I run the batch script locally it works, but when I trigger the same batch script from paramiko exec_command() it doesn't start the IOMeter.exe application.
I tried giving relative path and then with absolute path. It did not help.
I tried running the whole batch script command from exec_command() itself and even that did not start the application in remote machine.
The issue is, if we run any batch script with exec_command in paramiko python, it executes command and immediately kills the session and all dependent sessions (or child sessions) opened by that exec_command sessions.
To resolve this issue, you can trigger the same batch script from powershell and add wait at the end. Or create a PowerShell script (temp.ps1) and add the command to run the batch script as a process.
sample batch file looks like this:(I am using notepad here for security concerns, but actually I am running a tool which runs IO stress on different disks parallelly and logs output to the file)
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
start cmd.exe /k \"cd C:\\Users\\Administrator\\automate && C:\\Users\\Administrator\\automate\\notepad.exe &exit"
Sample powershell script looks like this:
start-process 'cmd.exe' -ArgumentList "/c C:\\Users\\Administrator\\automate\\temp_batch.bat &exit" -Wait
So, total we have a batch script and a powershell run the batch script as a process.
In python just run this powershell script using paramiko exec_command.
cmd = "powershell -file C:\\Users\\Administrator\\automate\\temp3.ps1"
Sample python script would look something like this:
import paramiko
hostname = 'x.x.x.x'
port = 22
username = 'user'
password = 'pwd'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,port,username,password)
cmd = "powershell -file C:\\Users\\Administrator\\diskio\\temp3.ps1"
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
result=''.join(outlines)
print (result)
print(stderr.readlines())
ssh.close()

VSTS, Launching Android emulator from a batch file not working as expected

I am trying to launch an Android emulator from a batch file inside my build definition, with the next command:
start /WAIT "Start Emulator" "C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe" launch /sku:Android /id:97522427-7A5E-4F3B-96A8-B9F9F0C0423A
I tried to add the build step as a command line, and a batch script.
Problem: The script is working right, and opening the emulator and wait for it to fully open, but once script finishes executing and console closes, the emulator is closing as well.
I tried to run the script directly on build server, and it works fine without closing emulator, but when queued as a build step, I am facing the above problem.
Question: How can I force the emulator to stay open after batch file finishes executing?
EDIT:
It looks like the build definition task terminates all processes it created in the defined step, I have tried multiple script, tried cmd /k and tried the /b and tried to create another batch file that actually calls this one or start it, yet no results. I am still waiting for any possible solution.
Alright, I tried a lot of scripts in batch files, and I tried to run it from command line, after a lot of time waste and getting tired, I decided to give PowerShell task a try to fix my problem. I ended up with this:
Start-Process -FilePath "C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe" -ArgumentList "launch /sku:Android /id:97522427-7A5E-4F3B-96A8-B9F9F0C0423A" -Verb runas
Start-Sleep -s 60
This made the emulator start, and stays running even after PowerShell script ends.

Powershell running as administrator refuses to redirect STDOUT to file

As an exercise, I am trying to make a web control panel for Hyper-V. It will use Powershell to interact with Hyper-V.
First thing I tried to do was to use the Get-VM command to create a table with the status of all the VM's on the local machine. I ran Get-VM from PHP:
$output = `powershell Get-VM`;
$output became NULL. After a lot of troubleshooting, I realized that Get-VM doesn't give output unless Powershell is running as an administrator (I was running the Powershell instance that I was testing with as administrator, but the one that was created by PHP was not run as administrator).
Then, I went to the powershell.exe file, right-clicked on it, went to Properties -> Compatibility -> Change settings for all users, and checked "Run as administrator". This should make it run as administrator no matter where it is started from, and it appears that it does exactly that in this case as well. However, once Powershell is running as an administrator, it does not output to a file, even if told to do so by the > operator.
If I run powershell Get-VM > out.txt, while powershell is running as an administrator, it creates the file out.txt, but it is 0kb and contains nothing. powershell Get-Process > out.txt has the same result, a 0kb file.
Running powershell Get-Process > out.txt with a non-elevated powershell.exe yields the expected result, namely the output from Get-Process in out.txt. However, you can't do this with powershell Get-VM > out.txt, since Get-VM returns no data unless powershell is run as administrator.
To wrap it up, my question is why does Powershell refuse to redirect its STDOUT to a file if it is running as administrator, but not if it is running as a normal user?
My diligent father has solved the problem, by not using redirection operators, but rather piping the output into Powershell's own out-file or export-csv, so that the command becomes powershell Get-VM | export-csv out.csv. I then parse this in PHP and convert it to a table. Works like a charm.

Batch File fails when launched from Outlook

I have .bat files that execute PowerShell scripts. They run fine from Task Scheduler or when running the batch files myself, but when I run them from Outlook (either from VBA script or using a Rule) - they just exit immediately without completing.
they look like this:
*PowerShell -file C:\Users\tenba1\Documents\Scripts\Account_Recon.ps1*
I also tried this:
*Call PowerShell -file C:\Users\tenba1\Documents\Scripts\Account_Recon.ps1*
Any idea why this happens?
You need to unrestrict your execution policy in the script call.
Powershell.exe -ExecutionPolicy Unrestricted -File filedir\filename.ps1

Batch file to run .exe and output to file

I have an .exe file that when run opens a cmd window that outputs a unique ID and waits for input. I have to run this .exe file on about 100 PC's to unlock their settings.
I want to run this .exe and output the ID to a text file as I don't want to have to run it manually on all machines. I have tried using a batch file but it doesn't work. What seems to be catching me is it's waiting until it receives input before it outputs to the file .
start "C:\unlock\Unlock.exe" >> ./Unlock.txt
cmd /K "C:\unlock\Unlock.exe" >> ./Unlock.txt
Attempting to feed parameters to the exe using the cmd command doesn't seem to work either.
cmd /k "C:\unlock\Unlock.exe" 11 >> ./Unlock.txt
I'm open to all suggestions for automating this. A batch file, vbs, powershell or python.
I've also tried using psexec but it does output to the cmd window I'm running the remote command from.

Resources