0

I need to get some local server name comparison between the AD Computer Object and the local computer name using Powershell script.

The below script is what I have come up with but somehow does not show the result of the 'Local Computer Name' column for Windows Server 2003.

Get-ADComputer -Filter { (operatingSystem -like '*2003*') -or (operatingSystem -like '*2008*') } |
    Where-Object { Test-Connection -ComputerName $_.Name -Count 1 -Quiet } |
    Select-Object -Property `
        Name,
        @{ N = 'Invoke-Command result'; E = { Invoke-Command -ComputerName $_.Name -Scriptblock { HOSTNAME } } },
        @{ N = 'PsExec (32bit) result'; E = { PsExec.exe "\\$($_.Name)" /accepteula cmd /c "HOSTNAME" 2>&1} },
        @{ N = 'PsExec (64bit) result'; E = { PsExec64.exe "\\$($_.Name)" /accepteula cmd /c "HOSTNAME" 2>&1} } |
    Sort-Object Name |
    Out-GridView

I have updated the code to the above as suggested from some of the experts below. However, the result is now showing like:

enter image description here

Senior Systems Engineer
  • 1,061
  • 2
  • 27
  • 63
  • 1
    What if you do `$env:COMPUTERNAME` or `[System.Net.Dns]::GetHostName()` instead of `HOSTNAME` ? – Theo Oct 11 '20 at 14:08
  • @Theo, thank you for the response. However, I cannot see any limitations for Windows Server 2003 in https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7 THe result is still the same, no result or blank. – Senior Systems Engineer Oct 11 '20 at 15:01
  • As per https://stackoverflow.com/questions/58197936/remote-commands-on-windows-server-2003-standard it says that I must use PsExec, when the Destination is Windows Server 2003, so how can I use that as calculated property? – Senior Systems Engineer Oct 11 '20 at 15:02
  • 1
    Test with psexec alone until you figure it out and then put the working command in place of this one. Nothing crazy, just do it. – Doug Maurer Oct 11 '20 at 15:39
  • 2
    Its likely just a typo, you've spelt `Invoke-Command` incorrectly – jfrmilner Oct 11 '20 at 21:37
  • Hi Everyone, I've updated the code as above. SO not sure how can I capture just the result of the PsExec command? – Senior Systems Engineer Oct 15 '20 at 06:08
  • 1
    Apparently, PsExec 2.2 has a switch `-nobanner`. Another option would be to return only the 6th line of the string array PsExec outputs. I found that [here](https://stackoverflow.com/questions/30193070/psexec-copyright-output) – Theo Nov 08 '20 at 15:30
  • 1
    @Theo Thanks for the suggested parameter :-) yes it helps – Senior Systems Engineer Nov 09 '20 at 09:26

0 Answers0