2

I'm trying to create a script that takes a list of a few hundred machines, and I'd like to have it output me three things: IP address, VMware version, and of course computername. I can't quite put it all together and thought I'd ask for help.

Below is what I have which outputs to a CSV file, but I'm not sure how to make it display the IP. I've tried some different syntax with "ping" but the script will hang on the first machine and never go past it.

for /f %%i in (C:\Temp\Computers.txt) do ( 

   echo Software on %%i >> C:\Temp\psinfovmware15.csv 

   psinfo64 -s \\%%i | find /i "VMware" >> C:\Temp\psinfovmware15.csv 

   echo IP Address on %%i >> C:\Temp\psinfovmware15.csv 

   ping \\%%i >> C:\Temp\psinfovmware15.csv 

) 

excel C:\Temp\psinfovmware15.csv

Also, I found this powershell script below that displays the computername and IP address, but I'd like for it to display the VMware version:

$servers = Get-Content "computers.txt"
$report = @()
ForEach ($server in $servers) {
  Try {
    $tempreport = New-Object PSObject
    $IP = ((Test-Connection -ea stop -Count 1 -comp $server).IPV4Address).IPAddresstoString
    $tempreport | Add-Member NoteProperty Server $server
    $tempreport | Add-Member NoteProperty Status "Up"
    $tempreport | Add-Member NoteProperty IP $ip
    $report += $tempreport
    } 
  Catch {
    $tempreport = New-Object PSObject
    $tempreport | Add-Member NoteProperty Server $server
    $tempreport | Add-Member NoteProperty Status "Down"    
    }
  }
$report | Export-Csv -NoTypeInformation "report.csv"

Any ideas on how to have a timeout if the machine isn't on the network? Thanks!

  • psinfo64 won't work with vmware, it uses rpc from the windows protocol stack. get the vmware powershell modules, it will open another world for you. https://www.powershellgallery.com/packages?q=vmware – Citizen Apr 24 '20 at 02:47

1 Answers1

0

Of course, you can grab desired information from psinfo64.exe in PowerShell as follows:

$psinfo64 = psinfo64.exe -s -nobanner \\$server 2>$Null|
    Where-Object { $_ -match "VMware" }

The following script could help (sorry, I can't test above psinfo using my current testing data collection):

$servers = Get-Content -Path 'D:\bat\files\30852528.txt' |
    Where-Object { $_.Substring( 0,1 ) -ne ';' } # refuse comment lines
$report = [System.Collections.ArrayList]::new()
ForEach ($server in $servers) {
    $IpTry = Measure-Command -Expression {
        $IpTest = Test-Connection -ErrorAction SilentlyContinue -Count 1 -ComputerName $server
    }
    if ( $null -eq $IpTest ) {
        $IpStatus = $Error[0].Exception.InnerException.Message
        $psinfo64, $IpV4Addr = [string]::Empty
    } else {

        $IpStatus = 'On'
        if ( $IpTest.Address -eq $IpTest.ProtocolAddress ) {
            $IpV4Addr = $IpTest.ProtocolAddress
        } else {
            $IpV4Addr = $IpTest.IpV4Address.IPAddressToString
        }
        $IpTest.Dispose()
        $psinfo64 = psinfo64.exe -s -nobanner \\$server 2>$Null|
            Where-Object { $_ -match "VMware" }
    }
    $null = $report.Add( 
        [PSCustomObject]@{
            server   = $server;
            IpV4Addr = $IpV4Addr;
            IpTimeMs = $IpTry.TotalMilliseconds;
            IpStatus = $IpStatus;
            psinfo64 = $psinfo64;
        }
    )
}
$report #| Export-Csv -NoTypeInformation "report.csv"

Output:

D:\PShell\SF\1011340.ps1
server         IpV4Addr        IpTimeMs IpStatus  
------         --------        -------- --------  
google.com     216.58.201.110  115,6865 On 
77.75.79.53    77.75.79.53      21,5296 On 
192.168.1.1    192.168.1.1      13,8537 On 
192.168.1.12                  1252,0167 A non-recoverable error occurred during...
bmw.com                       3999,3424 Error due to lack of resources
160.46.244.131                3999,6292 Error due to lack of resources
foo.bar                         41,9088 No such host is known

Testing data collection comes from this my answer.

JosefZ
  • 1,514
  • 1
  • 10
  • 18