Link to home
Start Free TrialLog in
Avatar of Adrian Raj
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

 
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

Open in new window


Please help. Very important for me to get the hostname.
SOLUTION
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adrian Raj
Adrian Raj

ASKER

Hi Raheman,

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.
$scriptblock = {

$pcname = Get-wmiobject win32_ComputerSystem -ComputerName $pc | ForEach-Object {$_.Name}

$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 $pcname,$_.Name,$_.Vendor,$_.Version,$_.InstallDate 
    } | out-file C:\"env:COMPUTERNAME"_SW01.txt

}


$computers = Get-Content c:\temp\computerlist.txt

foreach($pc in $computers)
{
    Invoke-Command -ComputerName $pc -ScriptBlock $scriptblock 
}

Open in new window

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
ForEach-Object {
                    '{0},{1},{2},{3},{4}' -f $env:COMPUTERNAME,$_.Name,$_.Vendor,$_.Version,$_.InstallDate 

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Callum,

Thanks for your reply. Will try and let you know.
HI Callum,

Is there anything i need to do to establish a remote powershell session. (e.g. services i need to turn on  ,etc.)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help experts