How to restart docker for windows process in powershell? - powershell

I want to restart docker for windows (now known as Docker Desktop) in powershell.
I would like to do it with one command in PowerShell.
May I implement it?
When using Restart-Service *docker*:

Kill and restart the docker process:
$processes = Get-Process "*docker desktop*"
if ($processes.Count -gt 0)
{
$processes[0].Kill()
$processes[0].WaitForExit()
}
Start-Process "C:\Program Files\Docker\Docker\Docker Desktop.exe"
In the if clause I check if any running docker process has been found. There should never be more than 1 instance of "Docker Desktop" running so you can then kill the first one in the list.
To restart you need to know the full path of the "Docker Desktop.exe" file on your computer.

You can user in powershell:
restart-service *docker*
Or int the Docker QuickStart Terminal:
docker-machine restart

On windows, open Docker desktop and click on the debug icon then restart. You can also consider "reset to factory defaults"

Similar to Sebastian L's comment above, but slightly cleaner & faster if you know whether you are currently running Linux or Windows containers.
If running Linux Containers
Stop-Service *docker*
Start-Service *docker*
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine
If running Windows Containers
Stop-Service *docker*
Start-Service *docker*
&$Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchWindowsEngine
-SwitchDaemon toggles from one to the other (Linux to Windows or Windows to Linux) which is why you have to do it twice.

Related

Programs running on Hyper-V with Invoke-Command hang

I'm trying to run my software on Hyper-V VM using powershell Invoke-Command, without success. Host OS -Windows 10. Guest OS - also Windows 10. VM Generation 1.
I did some simple experiments and found out this:
If I run
Invoke-Command -VMName MY_VM -Credential $Cred -ScriptBlock { calc }
then I can see launched calc.exe on the guest system right with UI.
But if I run mspaint or any non-Microsoft program, nothing happens. The program just hangs in the VM TaskManager without any effect.
I also tried to run several different programs using CLI calling Invoke-Command several ways, but got the same result.
What could be the problem?
The basic answer is that powershell remote connections (or any remote connection like rdp, ssh, etc) take place in a separate logon session, and can't really interact with each other.
There are two reasonable ways to do this:
Use PsExec - part of the microsoft sysinternals tools group.
# List sessions - note the session ID of the session you want the process to start in
quser /server:$computername
# Run a process remotely, specifying the logon ID
PsExec.exe -s -i $ID notepad.exe
Use a scheduled task that runs when you are logged in and is visible. You can do this with powershell's various New-ScheduledTask commands to create one, or follow this guide by Scripting Guy! using WMI Win32_ScheduledJob methods.
See use powershell to start a gui program on a remote machine for more details on both options, and a well-written description of why it's hard to do in windows.

Hyper-V Start-VM stuck on "Connecting to..."

I want to start a VM from command line (running win server 2016),
but I found that the command Start-VM will cause the VM stuck on "Connecting to...",
the preview screen always shows black.
Until I opened the VM (double click on its name in Manager), it starts installing and works fine.
(It shows "connecting to" in the beginning.)
I can start the VM perfectly via Hyper-V Manager, but I can't start it using command line Start-VM. Why? How to solve this problem? Any help is appreciated!
Don't use CMD but Use powershell and type
Start-VM -Name "virtual machine name"

PsExec -i parameter works only with console

I am using PsExec to run a process on a remote computer but I want also to use the -i parameter in order the process interact with the computer desktop.
For example:
PsExec.exe \\computer1 -i -h powershell.exe -noexit -command hostname
When I RDP to the machine, I cannot see the "GUI" if the powershell, but I do see a new PowerShell process run under processes.
But if I am using the console feature in my virtualization environment (as this is a VM), I can see the PowerShell process pops up.
Tried to search online a "Console" like feature when using RDP to a machine but it seems like the /console argument has been deprecated...
What else can I try?
I found the solution thanks to hawaii:
I just need to add the session id after the i parameter.

Starting Service on remote server failed

I encounter strange behavior when trying to remotely start a service.
on Server A i'm running this line (it's part of bigger script named RunRemoteService.ps1):
Invoke-Command -ComputerName $B_comp -ScriptBlock {Powershell.exe -File "run_service.ps1"} -Credential $cred
And the script run_service.ps1 contains the following line (it's also part of bigger script):
$my_service_name.Start()
Now here's the strange thing, If I run RunRemoteService.ps1 when I have an open remote connection (mstsc) to server B then the script works perfectly and the required service on B is really starting.
However, if I run RunRemoteService.ps1 when there is no mstsc connection with server B then the script failed (service doesn't start).
Why is this happening and how can it be resolved?
EDIT: I explored this issue a bit more and found out that this occurs only when trying to run my specific service.
that means that my service must run from a session of already logged on user (that is why it's working if I mstsc to the server before).
So I guess my new question is - is there a way I can login to remote machine from powershell?
Thanks.
If you cannot use Credential delegation as suggested by #EBGreen. Try using psexec.exe for calling the script instead of PowerShell remoting.
psexec \\server "cmd /c powershell.exe -f c:\script.ps1"
Get psexec from sysinternals.com
Ok, so my question had evolved and modified on the fly so this solution is to the latest issue I had which is - I couldn't remotely start my service if no user is logged on to remote machine.
The solution is a workaround:
I configured auto-login (using sysinternals auto-login tool) on remote server.
I used Restart-Computer cmdlet at the beginning of the test.
Now after the restart is complete and ps-session is restored, user is logged in automatically to server and I can remotely start my service.
Thanks.

Running a script off local machine to remote using Invoke-Command

Here is the command I am trying to run
Invoke-Command -ComputerName DOG-02 C:\Users\user\Documents\PowerShell\PowerShellmenuz.ps1
I want to be able to run the script PowerShellmenuz.ps1 on a remote machine. I am very close but am stuck at this point.
It is hard to tell where is the problem without error output, but probably you've missed first configuration step on remote machine. There things that should be done before executing scripts on remote machine.
Enabling PowerShell Remoting on the computer that you want access remotely. Open a PowerShell window as Administrator – right click the PowerShell shortcut and select "Run as Administrator". And execute the following command:
Enable-PSRemoting -Force - it will start service WinRM to allow incoming connections.