Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, October 15, 2012 10:51 AM
How can I get the name of a remote computer? Either on a PSSession or Invoke-Command session.
Paulo Morgado
All replies (16)
Monday, October 15, 2012 11:21 AM ✅Answered | 1 vote
Invoke-Command -comp name -script {$env:computername}
Monday, October 15, 2012 5:48 PM ✅Answered
You can't script a PSSession like that. A PSSession is interactive. This is why we have Invoke-Command, which will run a script on the remote machine in the context of that machine. Therefore, your script should be:
Invoke-Command -Scr { $env:computername } -cn london
I'll say it again: You can't script a PSSession. Once you are in the session, the script loses scope.
Grant Ward, a.k.a. Bigteddy
Monday, October 15, 2012 12:48 PM
But if I'm using a PSSession
$computers | % { Enter-PSSession -ComputerName $_ -Credential $credential; $env:computername; Exit-PSSession }
I allways get the local computer name.
Paulo Morgado
http://PauloMorgado.NET/
Monday, October 15, 2012 12:58 PM | 1 vote
But if I'm using a PSSession
$computers | % { Enter-PSSession -ComputerName $_ -Credential $credential; $env:computername; Exit-PSSession }
I allways get the local computer name.
Paulo Morgado
http://PauloMorgado.NET/
Are you understand what are you doing?
$computers | % { Invoke-Command -comp $_ -script {$env:computername} -Credential $credential }
Monday, October 15, 2012 1:16 PM
And what if I'm running some script inside a PSSession? How can I know the computer name?
Paulo Morgado
Monday, October 15, 2012 1:28 PM
And what if I'm running some script inside a PSSession? How can I know the computer name?
Paulo Morgado
This is a full script,the script return $env:computername from remote pc and no need to enter pssession.
Monday, October 15, 2012 1:53 PM
I know I don't need to enter a pssession to get the computer name. But what if I'm already on a pssession?
Enter-PSSession ...
...
<get computer name>
...
Exit-PSSession
Paulo Morgado
Monday, October 15, 2012 2:00 PM
I know I don't need to enter a pssession to get the computer name. But what if I'm already on a pssession?
Enter-PSSession ... ... <get computer name> ... Exit-PSSession
Paulo Morgado
You should type manual command if you use Enter-PSSession.If you want to automate this,use the Invoke-Command with or without New-PsSession.
Monday, October 15, 2012 2:07 PM
I'm sorry Kazun, but it either is possible or not. If I'm saying that I'm already on a pssession, Invoke-Command is no answer.
My script does several things inside a Enter-PSSession/Exit-PSSession and, at some point needs to know the computer name.
Paulo Morgado
Monday, October 15, 2012 2:09 PM
I'm sorry Kazun, but it either is possible or not. If I'm saying that I'm already on a pssession, Invoke-Command is no answer.
My script does several things inside a Enter-PSSession/Exit-PSSession and, at some point needs to know the computer name.
Paulo Morgado
$env:computername
Monday, October 15, 2012 2:16 PM
You can't use pssession in a loop.
jrich answered in this thread about similar problem
Monday, October 15, 2012 2:47 PM
I'm sorry Kazun, but it either is possible or not. If I'm saying that I'm already on a pssession, Invoke-Command is no answer.
My script does several things inside a Enter-PSSession/Exit-PSSession and, at some point needs to know the computer name.
Paulo Morgado
See transcript:
PS C:\scripts> Enter-PSSession london
[london]: PS C:\Users\Administrator\Documents> $env:computername
LONDON
[london]: PS C:\Users\Administrator\Documents>
Grant Ward, a.k.a. Bigteddy
Monday, October 15, 2012 4:55 PM
Hi,
May be we can able to get a remote computer name without Entering PSSession. But need to add some more scripts to get remote computer.
1. Need to check which remote machine is bind to your local machine with port 5985/5986 ,filter out the foreign address/IP of remote computer.
2. Then we can get the hostname form that IP address.
*******************************************************************************************************************
1. Get-NetTCPConnection | Where-Object { $_.RemotePort -eq "5985"} | select RemoteAddress -Unique | fw > c:\temp.txt
2. Get-Content C:\temp.txt | where {$_-ne""}| foreach {$_.TrimEnd()} | Out-File c:\IP.txt
3. $IP=Get-Content C:\IP.txt
4. [System.Net.Dns]::GetHostEntry($IP)
Regards, Nayan
Monday, October 15, 2012 5:23 PM
I think I found the problem (it was between my chair and my keyboard).
My test script was something like:
Enter-PSSession -ComputerName 'london' -Credential $credential; $env:computername; Exit-PSSession
If I'm running from the LISBON machine, the result is LISBON not LONDON.
What am I doing wrong here?
Paulo Morgado
Wednesday, October 17, 2012 7:05 AM
Hi,
Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.
If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.
Yan Li
TechNet Community Support
Wednesday, October 17, 2012 11:32 AM
Got it.
Paulo Morgado