filtering hostname from the output of a powershell command

Welcome Forums General PowerShell Q&A filtering hostname from the output of a powershell command

Viewing 2 reply threads
  • Author
    Posts
    • #162192
      Participant
      Topics: 4
      Replies: 0
      Points: 34
      Rank: Member

      Hi All,

      Below command will give us the list of machines which are associated with account “john.hilton” from the domain “ags.com”. This command is working pretty fine and giving us the list of host names.

      Now i want to check whether the host where i am running this script is also in the list or not. If the host name is in the list it should return hostname. if host name is not in the list then it should not return anything. How can i achieve this?

      below is the command i am running.

      ((Get-ADUser -Server ags.com -Identity "john.hilton" -Properties LogonWorkstations).LogonWorkstations).split(",") | findstr hostname
      
    • #162291
      Participant
      Topics: 5
      Replies: 2373
      Points: 6,010
      Helping Hand
      Rank: Community MVP

      You can get the hostname with the environment variable $ENV:ComputerName. You should read the help about_Comparison_Operators to learn how to compare objects in Powershell … read the examples as well please.

    • #162399
      Participant
      Topics: 0
      Replies: 36
      Points: 408
      Helping Hand
      Rank: Contributor

      Using your example code:

      $Hostnames = ((Get-ADUser -Server ags.com -Identity “john.hilton” -Properties LogonWorkstations).LogonWorkstations).split(“,”) |
          findstr hostname
      
      If ($Env:COMPUTERNAME -in $hostnames) {
          return $Env:COMPUTERNAME
      }
      
Viewing 2 reply threads
  • The topic ‘filtering hostname from the output of a powershell command’ is closed to new replies.