Pssession- computername vs vmname for Enter-pssession
-
OK, start by verifying you don't currently have any remote sessions.
Type
Get-PSSession
and hit enter.You should get nothing back.
Then type
New-PSSession -VMName virt3datactr
and hit enter.It should prompt for creds, display info about your new PSSession, and then return you to a normal prompt. If not, stop here and let me know.
Type
hostname
and hit enterIt should return the name of your laptop.
Type
Get-PSSession
and hit enter
You should see info about the PSSession you just created.You have started a PSSession on virt3datactr, but you are not connected to it. It's like you went to virt3datactr and launched PowerShell and walked away. It's running, you're just not there.
-
I received different results. I was prompted for credentials, but now shows I'm connected to virt3datactr, and hostname returns virt3datactr. The error is my fat fingering my password.
[Virt3datactr]: PS C:\Users\Administrator\Documents> New-PSSession -VMName virt3datactr
Windows PowerShell Credential Request: cmdlet New-PSSession at command pipeline position 1
Warning: A script or application on the remote computer VIRT3DATACTR is requesting your credentials. Enter your
credentials only if you trust the remote computer and the application or script that is requesting them.Supply values for the following parameters:
Credential
Hyper-V was unable to find a virtual machine with name "virt3datactr".
New-PSSession : The input VMName virt3datactr does not resolve to a single virtual machine.
+ CategoryInfo : InvalidArgument: (:) [New-PSSession], ArgumentException
+ FullyQualifiedErrorId : InvalidVMNameNotSingle,Microsoft.PowerShell.Commands.NewPSSessionCommand[Virt3datactr]: PS C:\Users\Administrator\Documents> hostname
virt3datactr
[Virt3datactr]: PS C:\Users\Administrator\Documents>[Virt3datactr]: PS C:\Users\Administrator\Documents> get-pssession
[Virt3datactr]: PS C:\Users\Administrator\Documents> -
Your prompt indicates you were already connected to virt3datactr when you tried to create a new pssession
[virt3datactr]: PS
. You first need to exit any temp session and return to a normal ps prompt on your laptop. Then try the previous commands. -
@Mike-Rodrick I typed exit, and now it's returning the results you said it would. I don't understand what just happened though.
-
My fault, I needed to make sure you ended the temporary session you had before we started working with New-PSSession.
While your prompt started with [virt3datactr], all commands were being executed on virt3datactr. So when you typed New-PSSession you were creating the new session on virt3datactr, to itself.
Some kind of wierd inception stuff :P
-
Let's try again. Let me know when you are back to a normal prompt on your laptop. I need a quick break, brb.
-
Not your fault..it's weird inception stuff. I am now back to the regularly scheduled prompt.
PS C:\WINDOWS\system32> New-PSSession -VMName virt3datactr
cmdlet New-PSSession at command pipeline position 1
Supply values for the following parameters:
CredentialId Name ComputerName ComputerType State ConfigurationName Availability
1 WinRM1 Virt3datactr VirtualMachine Opened Available
PS C:\WINDOWS\system32>
-
Great! Not sure if this part worked or makes sense, let me know
Type
hostname
and hit enterIt should return the name of your laptop.
Type
Get-PSSession
and hit enter
You should see info about the PSSession you just created.You have started a PSSession on virt3datactr, but you are not connected to it. It's like you went to virt3datactr and launched PowerShell and walked away. It's running, you're just not there.
-
Got it. It's available
-
Yay!
Alright, so you now have a PSSession created and running on virt3datactr. But you are not connected to it. Lets connect.
Notice in the information returned by Get-PSSession in particular, the ID and Name columns. You can use these identifiers to connect to the PSSession. If you had other VMs running you could create PSSessions to them as well, and they would all be listed, and have unique names and IDs assigned to them.
Type the following to connect to an existing session
Enter-PSSession -Name WinRM1
and hit enter.I think thats the name from your results. You should be connected to virt3datactr, and your prompt should start with [virt3datactr]
-
Received those results. Now I'm connected to virt3
-
Awesome. Now lets look at persistence. Make sure you are still connected to the session, prompt starts with [virt3datactr]
Lets repeat what we did before...
Type the following
$h = hostname
and hit enter. You should get nothing back.This command stores the hostname in the variable $h.
We can now use the variable anywhere we need the hostname.
Type the following:
$h
and hit enter. It should return the hostname.This is exactly the same as when we had a temporary session
-
@Mike-Rodrick It does
-
Again as before, try this
Type :
exit
and hit enter.Once you are back to a normal PS prompt, you are no longer connected to virt3datactr.
Type the following:
$h
and hit enterYou should get nothing back. This is because $h was created and only existed in the PSSession on virt3datactr, to which you are no longer connected.
Still the same as a temporary session.
-
Ok I see that...
-
Here is where it gets interesting.
Type the following to reconnect to the PSSession
Enter-PSSession -Name WinRM1
and hit enter.Then type the following:
$h
and hit enter. It should return the hostname!Unlike the temporary session, the PSSession you created with the cmdlet New-PSSession didn't get removed just because you exited it, and the variable you created the first time you connected still exists! You can reconnect to the session any time, and to get really crazy, from anywhere!
This is great for long running jobs. If you start a long job in a temporary session, it will fail if you exit the session. With a PSSession however, you could start a job at work, like a backup. You could exit the session when the day ends. The session still exists and the task is still running. You could reconnect to the session from home and check on the job!
-
@Mike-Rodrick Got it
-
Since Exit doesn't kill the session, we have to remember to end it when we are done. Otherwise, we end up with these open sessions running on the remote machine.
To end the PSSession type
exit
and hit enter, to return to a local prompt, then type
Remove-PSSession -Name WinRM1
and hit enter.If you now type Get-PSSession, the list should be empty.
-
Ok, all that happened. Now i'm getting fuzzy on why the exit-pssession didn't remove the session.
It's all temporary??? -
Sorry, I went to fast there at the end, got exited, lol. Do you still have a PSSession? Get-PSSession