Adrian Raj
asked on
Get remote computer hostname in Powershell
Hi Experts,
I want to get the remote computer hostname from powerhshell as when i use $env:COMPUTERNAME I am getting my local machine hostname. Below is my script to get the list of installed software from the remote machine and i have a list of hostnames/IP in a txt file. Why am i asking for this is sometimes I use IP to get this list of software installed , so i need the remote hostname as well. As you can see below in my script, i need the hostname to be in the for-each object section below to replace the $env:COMPUTERNAME
Please help. Very important for me to get the hostname.
I want to get the remote computer hostname from powerhshell as when i use $env:COMPUTERNAME I am getting my local machine hostname. Below is my script to get the list of installed software from the remote machine and i have a list of hostnames/IP in a txt file. Why am i asking for this is sometimes I use IP to get this list of software installed , so i need the remote hostname as well. As you can see below in my script, i need the hostname to be in the for-each object section below to replace the $env:COMPUTERNAME
foreach($pc in $computers){$properties = "name", "vendor","version" ,"installdate"
Get-ChildItem HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\*\Products | ForEach-Object {
$root = $_.PsPath
$_.GetSubKeyNames() | ForEach-Object {
try {
$RegKeyPath = (Join-Path -Path (Join-Path -Path $root -ChildPath $_) -ChildPath InstallProperties)
$obj = Get-ItemProperty -Path $RegKeyPath -ErrorAction Stop
if ($obj.UninstallString) {
[PSCustomObject]@{
Path = $RegKeyPath;
Name = $obj.DisplayName ;
Vendor = $obj.Publisher ;
Version = $obj.DisplayVersion ;
InstallDate = $obj.InstallDate ;
} | where vendor -like 'microsoft*'
}
} catch {
}
}
} | ForEach-Object {
'{0},{1},{2},{3},{4}' -f $env:COMPUTERNAME,$_.Name,$_.Vendor,$_.Version,$_.InstallDate
} | out-file C:\"env:COMPUTERNAME"_SW01.txt
Please help. Very important for me to get the hostname.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Callum,
Thanks for your reply.
Would like to know the result of the script as I want to have the remote computer hostname to be inserted in the text file , however would like to know if the code below, "$env:COMPUTERNAME" does it return the local machine hostname or the remote machine hostname
Thanks for your reply.
Would like to know the result of the script as I want to have the remote computer hostname to be inserted in the text file , however would like to know if the code below, "$env:COMPUTERNAME" does it return the local machine hostname or the remote machine hostname
ForEach-Object {
'{0},{1},{2},{3},{4}' -f $env:COMPUTERNAME,$_.Name,$_.Vendor,$_.Version,$_.InstallDate
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Callum,
Thanks for your reply. Will try and let you know.
Thanks for your reply. Will try and let you know.
ASKER
HI Callum,
Is there anything i need to do to establish a remote powershell session. (e.g. services i need to turn on ,etc.)
Is there anything i need to do to establish a remote powershell session. (e.g. services i need to turn on ,etc.)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help experts
ASKER
Thanks for your reply. I have made minor change to your code. Im not sure whether this will work as i dont have access to a test environment yet. Just asking if this would work logically. I want the computer hostname to be inserted in the text file as well.
Open in new window