'Powershell Invoke-Command with IP address instead of Hostname

I need to check existence of a specific service on a group of hosts by means of Invoke-Command. but i got the error mentioned that i can't use IP address in Invoke-Command. Following is snippet of my code :

`$Server_List = Import-Csv -Path "C:\Asset Discovery\Assets2.csv" | 
 select -ExpandProperty IP
 #Hash Tables
 $Installed=@{}
 Not_Installed=@{}


 foreach ($Computer in $Server_List) {
         
        #Check Wazuh existence block. 
         $Wazuh = $null
         $Wazuh = Invoke-Command -ComputerName $Computer -ScriptBlock {Get-Service -Name 
         "WazuhSvc"}     
  
         if ($Wazuh){
           #  Write-Host "Service $Wazuh found on $Computer" -ForegroundColor Yellow
              $Installed.Add("$Computer","Installed")
         }
    
         else{
  
              Write-Host "Service $Wazuh not found on $Computer." -ForegroundColor Red
              $Not_Installed.Add("$Computer","Not Installed")
                         
         }`   

I've decided to convert IP addresses to Hostnames using "([System.Net.DNS]::GetHostByAddress" but there is not any PTR record for some IPs.How can i run command on a remote host using IP address ?



Solution 1:[1]

I think You forgot to set the -delimiter parameter when You import-csv. First line should look like this:

$Server_List = Import-Csv -Path "C:\Asset Discovery\Assets2.csv" -delimiter "," |select -ExpandProperty IP

Now Your $Server_List contains all IP as a list and You will be able to run the foreach loop

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RelaxDK