Execute application on remote computer - windows

I want to start a process on remote computer(i know administrative credentials of remote computer). for starting a application on remote machine i used
Start-Process -FilePath 'C:\pqr.exe' -ArgumentList '/a' -Verb runas -WindowStyle Normal
command with "Invoke-command" or "Enter PsSession" which will start process on remote machine. Now problem is, I am able to start process but soon process starves for CPU allocation(it becomes 0%) and suddenly the launced application become not respoding. Is there any other way to allocate it CPU or run above commandlet with admin rights.

your should start-process then change priority of process
The command line syntax:
wmic process where name="AppName" CALL setpriority ProcessIDLevel
Example:
wmic process where name="notepad.exe" CALL setpriority 32768
or
wmic process where name="notepad.exe" CALL setpriority "above normal"
Priority:
idle: 64
below normal: 16384
normal: 32
above normal: 32768
high priority: 128
real time: 256
or use powershell [System.Diagnostics.ProcessPriorityClass]
Specify one of the following enumeration values "Normal, Idle, High, RealTime, BelowNormal, AboveNormal"
example
$a = (Get-Process -Name powershell)
$a.PriorityClass = [System.Diagnostics.ProcessPriorityClass]::RealTime

Have you tried this:
PsExec
Maybe this work for you

Related

Limit the number of CPU cores used by an Windows service running from net start command

I am running an benchmark for PostgreSQL 12 on Windows 10. I want to limit the number of CPU cores used by PostgreSQL service to test how does the CPU performance affect TPU.
Now I am starting PostgreSQL service with following command:
net start postgresql-x64-12
and I know how to limit the number of CPU cores for ordinary Windows application like:
start /affinity 1 "" "C:\Windows\System32\calc.exe"
How can I limit the number of CPU cores used by an Windows service running from net start command? Is there an /affinity option equivalent in net start command?
I found a solution. First, you cannot set CPU affinity to Windows system processes or services (see https://www.atmarkit.co.jp/ait/articles/0703/16/news151.html (Japanese)).
In my situation, I can run PostgreSQL process from pg_ctl command from cmd.exe with /affinity option like:
cmd.exe /c "start /affinity 1F /B c:\path\to\PostgreSQL\12\bin\pg_ctl.exe start -w -s -D C:\path\to\PostgreSQL\12\data"
Note that you cannot use Start-Process cmdlet and ProcessorAffinity property like this:
$app = Start-Process 'c:\path\to\PostgreSQL\12\bin\pg_ctl.exe' 'start -D C:\path\to\PostgreSQL\12\data' -PassThru -NoNewWindow
$app.ProcessorAffinity = 0x3
This causes SetValueInvocationException because pg_ctl.exe is immediately exit after it starts PostgreSQL instance.

Using a shell command to set docker's VM memory size to 6GB

I have a docker image that only runs if there are 6GB of RAM available. Unfortunately on Windows and Macs the docker VM is limited to 2GB by default. You need to go to the Docker advanced settings to change this. This is something I'd rather not have my users do.
Is there a shell command (or Powershell command on Windows) that changes this limit? I'd much rather tell my users they should run one command from the terminal, than have them mess with the advanced Docker settings.
With docker for windows, gui settings are stored in C:\Users\$YOUR_USER\AppData\Roaming\Docker\settings.json, also could be simplified as ~\AppData\Roaming\Docker\settings.json, in it you will find something like next:
{
......
"memoryMiB": 2048,
......
}
The default memory hyper-v machine used is 2048M, you can change it to any value you needed then restart the docker to meet your requirement.
You can use any method to modify the file & restart docker just as you like. Next is a sample powershell script for your reference:
a.ps1:
(get-content ~\AppData\Roaming\Docker\settings.json) | foreach-object {$_ -replace '"memoryMiB":.*', '"memoryMiB": 3072,'} | set-content ~\AppData\Roaming\Docker\settings.json
net stop docker
net stop com.docker.service
taskkill /IM "dockerd.exe" /F
taskkill /IM "Docker for Windows.exe" /F
net start docker
net start com.docker.service
& "c:\program files\docker\docker\Docker for Windows.exe"
Execute in powershell (NOTE: need to run as administrator):
powershell.exe -File "C:\abc\a.ps1" -ExecutionPolicy Bypass
Above will run a.ps1 in which it will change memory value & restart the docker, after that, the memory will just what you needed. You may change some path in the script based on your real situations on your computer.

Resizing a PowerShell window on a remote computer using a batch file

Wondering if anyone knows how to/if it is even possible to resize a PowerShell windows that I open on a remote computer using the batch file I used to run it.
1 #ECHO OFF
2 title Remote Start ProgramName Server
3 cd "C:\IT\FOLDER"
4
5 #ECHO OFF
6 SET /p _computer=Computer Name:
7
8 #ECHO "Launching Program Server On Remote PC %_computer%"
9
10 start PowerShell.exe .\psexec -i \\%_computer% '"C:\Program Files\ProgramFolder\programserver.exe"'
11
12 #ECHO "Launching ClientSideProgram on Local Machine"
13 start "" "C:\Program Files\ProgramFolder\programclient.exe"
14
15 PAUSE
16
17 start "" PowerShell.exe .\pskill \\%_computer% programserver.exe
18 ECHO "Program Server Terminated"
19
20 PAUSE
So as far as I have tested this, it works, but when I do start PowerShell.exe on line 10, it, as expected opens a powershell. THIS is the powershell I would like to resize to the smallest possible size, so that it's not as big of a nuisance when it opens.
Thanks in advance for whatever help anyone has
Edit: If resizing is not possible, can I just 'hide' it? or move it to the background at all?
This is a batch file that is just using PowerShell to call psexec.
Either use PowerShell Remoting ---
Running Remote Commands
Start an Interactive Session To start an interactive session with a
single remote computer, use the Enter-PSSession cmdlet. For example,
to start an interactive session with the Server01 remote computer,
type:
Enter-PSSession Server01
Run a Remote Command To run a command on one or more computers, use
the Invoke-Command cmdlet. For example, to run a Get-UICulture command
on the Server01 and Server02 remote computers, type:
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {Get-UICulture}
--- or use PSexec and target that remote host.
PsExec - Windows Sysinternals | Microsoft Docs
Using psExec to Open a Remote Command Window
https://blogs.technet.microsoft.com/systemcenteressentials/2009/09/01/using-psexec-to-open-a-remote-command-window
psExec \computer cmd
PsExec (SysInternals)
Execute a command-line process on a remote machine.
Much of PSRemoting requires that you use an account that is in the local admin group on the target. However, there are some that do not.
The only reason to use PSExec over PSRemoting is if PSRemoting is not enabled and if you need to run Code in the context of the logged on user.
# PowerShell only via PSRemoting
$TargetComputerName = Read-Host -Prompt 'Enter a computer name.'
Invoke-Command -ComputerName $TargetComputerName -ScriptBlock {
'C:\Program Files\ProgramFolder\programserver.exe'
} -Credential 'contoso\administrator'
# PSExec only
psexec \SomeTargetComputerName -c C:\Program Files\ProgramFolder\programserver.exe
With PSRemoting nothing is ever displayed on the targethost.
With that PSExec command, nothing is ever displayed on the targethost.
If you use PSExec to run in the context of the logged on user on that target host, then stuff will display when you call PowerShell.exe unless you use the minimize or hide switches.
This indicates that you are new to PowerShell and PowerShell remoting commands. Please hit up YouTube and view some of the talks on PSRemoting, running remote command, running remote programs, well, PowerShell in general.
Found the answer from a fellow colleague of mine. Apparently, very easy to just star minimized.
start /MIN
will do just that

PowerShell session not running more than 100 processes in remote machine

I have an application on a remote machine which spawns about 300 instances of different executable. It works fine when I double click on it, but when I try to execute the main application from a remote machine using PowerShell it does not spawn over 100 instances. One major difference is that when I execute the application through PowerShell it runs as a background process. Below is the command used.
Invoke-Command -Session $Session -ScriptBlock {
Set-Location 'C:\LatestCode'
& Start-Process -FilePath C:\LatestCode\ImageManager-Service.exe -Wait
}
It's the same behaviour when I try yo run the application using the Task Scheduler.

How to keep remote powershell command alive after session end?

I use the following command to run setup_server.exe on remote Windows box:
powershell -command "$encpass=convertto-securestring -asplaintext RPASSWORD -force;$cred = New-Object System.Management.Automation.PSCredential -ArgumentList RUSER,$encpass; invoke-command -computername RCOMPUTERNAME -scriptblock {setup_server.exe} -credential $cred;"
setup_server.exe's task is to create some configuration files and start my_server.exe (some daemon process), then it finishes. And I want my_server.exe to keep running after setup_server.exe is finished.
So when I do it via CMD on local box (i.e. just run setup_server.exe from CMD) it works, but when I do it via powershell on remote host it doesn't work. Namely the my_server.exe gets started, but right after setup_server.exe is closed the server also gets closed(killed).
So the question is following:
Which powershell flags/cmdlets should I use to make the described scenario to work as in local mode?
NOTE: I want synchronously get output of setup_server.exe, so running remote command with -AsJob flag, probably wouldn't work for me, though I even don't know if it will keep the server alive after setup_server.exe's end.
The way to keep the remote PowerShell session running after the command has finished is to use a PSSession e.g.:
$s = new-PSSession computername
Invoke-Command -session $s { ..script.. }
... do other stuff, remote powershell.exe continues to run
Remove-PSSession $s # when you're done with the remote session
Generally though exes should run independently from the app that launched them.
Why are you using Invoke-Command. If you want a persistent Session, use Enter-PSSession.
$s = New-PSSession -Computername "Computername";
Enter-PSSession -Session $s;
setup_server.exe
# Once you are finnished
Exit-PSSession
With 'Enter-PSSession' you are not just Invoking some Command on the Server, you are directly logged-in like you probably know from SSH.
If you want your powershell session to keep running because you are running an exe, try using the -InDisconnectedSession switch. From what I understand, it will run the executable on the remote machine in a session that isn't actually connected to your computer. In essence, your computer will not destroy the session, when it disconnects, allowing the exe to continue to run.
invoke-command -computername RCOMPUTERNAME -scriptblock {start-process setup_server.exe} -InDisconnectedSession
If you need to do this on multiple computers. Setup an array of all the computer names.
Note: I don't believe this works with sessions that are already created.
In order to keep a powershell code running on the session exit it should be a process. And the windows way to keep the process is running a .exe or a windows service.
To keep a Powershell shell open after executing a command, I use the -NoExit switch, e.g. this script starts a remote interactive PS session on servername with user administrator
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit
-Command "Enter-PSSession -ComputerName servername -Credential administrator"
http://powershell-guru.com/powershell-tip-13-prevent-powershell-from-exiting-once-script-finished/

Resources