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

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

Related

Run PowerShell script on remote server as different user without any pre-requisites

I need to trigger the execution of a powershell script on remote server under different user. This script uses 'gpg.exe' so the execution has to be on the remote server with a path pointing to 'gpg.exe'.
I created a bat file on remote server with following commands:
REM RUNTEST.BAT
time /T
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
echo started
start /wait powershell.exe "Start-Transcript -Path D:\logs\testlog.txt;import-module
'\\ServerName\FolderName\Script\AutomationScript.ps1';Stop-Transcript"
time /T
echo done
exit /b
Then invoked this bat file from PowerShell on another machine:
runas /user:DOMAIN\serviceuser "\\ServerName\FolderName\Script\Execute.bat"
These steps do execute the PowerShell script successfully and the output is also as expected. However, this requires 'gpg.exe' to be available on the path mentioned in the PowerShell script on the machine which will invoke the execution. Is there a way the script can look for pre-requisites (like gpg.exe, folder structure etc) only on the remote server, not the machine which invoked the bat file?
Not sure if OS makes any difference, the remote server has Windows Server 2019 and calling machine has Win 10. The remote server has the required folder structure, gpg.exe, bat file to execute the PowerShell script successfully. The calling machine will not have 'gpg.exe' & the folder structure.

Window opened via psexec is blank when using PowerShell Direct, but is OK when psexec starts locally

I am trying to build a simple environment to take screenshots automatically. I have several Hyper-V VMs and I need to use PowerShell Direct to run commands on them in a special sequence. When I run psexec to open notepad.exe via PowerShell Direct, Notepad's window opens, but it is blank. I can blindly click controls on the window (e.g., on the toolbar) and it appears that they work, but nothing is actually displayed besides a blank window.
The only simple option to open Windows in an existing session I see now is to run psexec (any other suggestions would be greatly appreciated because my attempts to stick to PowerShell-only approach hits a roadblock, see Starting a GUI application on a guest VM with PowerShell Direct).
I run psexec on one of the virtual machines to start notepad.exe in session 2. This PowerShell snippet runs on a host VM, it contacts guest VM via PowerShell Direct.
Invoke-Command -VMName 'client1.example.com' -Credential $credential -ScriptBlock {
C:\bin\psexec.exe \\client1.example.com -i 2 "notepad.exe"
}
notepad.exe starts, but the window is blank. Is this a bug in psexec?
Please see the screenshot:
The problem does not occur when I run psexec directly in a guest VM.
C:\bin\psexec.exe \\client1.example.com -i 2 "notepad.exe"
Solved by adding the -s option. I don't understand why the problem occurs in the first place and how running it "in the system account" helps.
-s Run the remote process in the System account.
The problem does not occur when I start explorer.exe, BTW.

Remotely execute `shutdown /sg`

I'm using a raspberry pi to shut down my Windows 10 Computer.
My RPI uses SSH with a private key to execute a shutdown /s /t 30 command on my computer.
But, for optimization reasons, I want to use the shutdown /sg command; by doing so my applications will open automatically before I unlock my session.
However, shutdown /sg needs to be executed on a Desktop Session.
How I can execute a script on my desktop from command communicated over SSH?
Thanks for your help.
I tried to use Invoke-Command but Powershell is not my cup of tea.
But, I find a solution :
I created a Task (named ShutdownSG_over_SSH_from_RPI) in Task Manager which execute shutdown \sg command.
From my RPI, I execute this command over SSH :
powershell -command "Start-ScheduledTask -TaskPath '\MyTaskPath\' -TaskName 'ShutdownSG_over_SSH_from_RPI'"
I know it isn't the best solution but it works.
If you have PowerShell 6.0, support for SSH connection has been added to New-PSSession, Enter-PSSession and Invoke-Command
This requires software to be installed on both your Raspberry PI and your computer.
Microsoft have provided a nice article explaining how to achieve this:
https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ssh-remoting-in-powershell-core?view=powershell-6
You should also look at the documentation for Invoke-Command, as that's the easiest way to execute commands and scripts remotely:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6

How to debug hidden powershell scripts?

I'm writing a script that normally will get called by another application (VMware vCenter Server). From that application I trigger a batch file (redirect.bat) and pass a variable which is the powershell script name (TestMe.ps1).
The script is placed on a Windows Server and when I go into the command prompt of the Windows Server and call the redirect script, I see that my PowerShell script runs as expected. However when I trigger it from the app the Powershell script is not run or doesn't produce output. I have confirmation that the redirect.bat is run, because the redirect.bat writes a line in a log file.
The vCenter Server app is running under Local System account. Could it be a permissions error? Is LocalSystem allowed to run Powershell scripts?
I now have no clue if the Powershell script even starts, because it (of course) is not visible in my console when running. The batch file always returns errorlevel = 0.
Any tips on how to insert debugging info in the script that should always give output? Tips on how to troubleshoot this?
redirect.bat:
set POWERSHELL=C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -nologo -noprofile -noninteractive
SET ERRORLEVEL =
echo %1 > G:\DataStoreAlarms\Log\Redirect-batch.txt
start %POWERSHELL% -command "&"%1""
echo Error level: %ERRORLEVEL% >> G:\DataStoreAlarms\Log\Redirect-batch.txt
I call redirect.bat from the command line and from the app like this:
redirect.bat G:\DataStoreAlarms\Scripts\TestGabrie.ps1
TestGabrie.ps1:
$String = "This is a test"
$String | Out-File -FilePath "G:\DataStoreAlarms\Log\Powershell.txt" -Append
Regards
Gabrie
Problem seemed to be the START command:
start %POWERSHELL% -command "&"%1""
After changing it to this, it worked:
%POWERSHELL% -command "&"%1""
Thanks for all your help.

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