Pssession- computername vs vmname for Enter-pssession
-
OK. Now let's try to create a temporary session first. Type the following command on your Hyper-V host (your laptop):
Enter-PSSession -VMName virt3datactr
Notice we did not use
New-PSSession
That will come later.
Let me know what happens. -
@Mike-Rodrick said in Pssession- computername vs vmname for Enter-pssession:
Enter-PSSession -VMName virt3datactr
Worked like a charm, returned a credentials window and allowed me to login. Ok at least that has been consistently available.
PS C:\WINDOWS\system32> Enter-PSSession -VMName virt3datactr
cmdlet Enter-PSSession at command pipeline position 1
Supply values for the following parameters:
Credential
[Virt3datactr]: PS C:\Users\Administrator\Documents> -
Great!
Now we have a temporary PSSession to virt3datactr. As long as the PS prompt starts with [Virt3datactr], any commands issued will be sent to the remote machine.
Lets first verify the hostname of the VM. Then we can look at what I mean by temporary session.
Type the following command
hostname
Does it return virt3datactr?
-
@Mike-Rodrick yes it does
-
Good.
Now lets look at what is meant by a temporary session, and how variables are deleted at the end of a temporary session (not persistent)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.Type the following:
ping $h
and hit enter. It should ping virt3datactr.After all of this, you are still connected in a temporary session to virt3datactr (your prompt should indicate this)
Let me know if this works and makes sense.
-
@Mike-Rodrick Set the variable to hostname and pinged. Yes it makes sense so far
-
Good.
Everything you have been typing is happening on the remote machine. Even though you are looking at the same PowerShell window on your laptop. You are really in a PowerShell window (session) that exists on virt3datactr. It's like you were logged on to virt3datactr and launched PowerShell.
Now type this:
exit
and hit enter.Exit is an alias for the command Exit-PSSession. This should end your temporary session, and return you to a normal PS prompt.
Once you are back to a normal PS prompt, you are no longer connected to virt3datactr.
Type the following:
$h
and hit enter.You 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.
Let me know if that worked and makes sense
-
@John-DeWilde And I think I get the temporary part. After reading your explanations about 15 times...not kidding it began to sink in. I guess I was my own worst enemy, and thought well, I'm creating a variable, and it will be there whenever i need it.
-
After running exit I received nothing for $h
-
Great! Easy to do, it can get really confusing.
Now let's reconnect to virt3datactr.
Type the following:
Enter-PSSession -VMName virt3datactr
and hit enter.Your prompt should change to show that you are connected again.
Type the following:
$h
and hit enter. It shouldn't return anything. Even though you are connected again. The variable was deleted when you exited your session, and you would need to recreate it. It's like you closed the PowerShell window on the remote machine.Once you are good with that concept. exit the session, and return to a normal prompt. Next we can look at creating a persistent connection.
-
It returned nothing as you said it would
-
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