/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


Powershell: Test-Connection Problem

07/19/2017 4622 views
I have been tasked with creating a report that will tell me which machines in our environment have Office 365 installed as long as OneDrive is NOT installed.  I am still learning Powershell so I'm not sure exactly what the problem is.  Any help would be appreciated.

Current Script:

$OutPath = "c:\Machines_with_O365.csv"
$Software = "Microsoft Office 365 ProPlus*"
$OneDrive = "Microsoft OneDrive*"

$Computers = Get-AdComputer -Filter * 
foreach ($Computer in $Computers)
{
    $Ping = Test-Connection -ComputerName $Computer.Name -Count 1 
    if ($Ping -eq $true)
        {
        try
            {
            $MachineName = $Computer.Name 
            $softwareList = Get-WmiObject -Class Win32_Product -Computer $MachineName -ErrorAction Stop
            if ($softwareList -contains $Software -and $softwareList -notcontains $OneDrive) {
              $softwareList | Select-Object @{N="ComputerName";E={$Computer.Name}},Vendor,Name | Export-Csv -Path $OutPath -NoTypeInformation -Append
              }
            }
        Catch
            {
            Write-Host "Unable to Obtain WMI Object of $MachineName" 
            }
        }
}
If ($Ping -eq $False)
    {
    Write-Host "The $MachineName is not pingable" 
    }

Here are the errors I am receiving:

Test-Connection : Testing connection to computer 'XXXXX' failed: Error due to lack of resources
At C:\Users\XXXX\Documents\Office365Query_NoOneDrive.ps1:8 char:13
+     $Ping = Test-Connection -ComputerName $Computer.Name -Count 1
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (XXXXX:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
 
Test-Connection : Testing connection to computer 'XXXXX' failed: The requested name is valid, but no data of the requested type was found
At C:\Users\XXXX\Documents\Office365Query_NoOneDrive.ps1:8 char:13
+     $Ping = Test-Connection -ComputerName $Computer.Name -Count 1
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (XXXXX:String) [Test-Connection], PingException
    + FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
1 Comment   [ + ] Show comment

Comments

  • I think your problem is in this:
    $Ping = Test-Connection -ComputerName $Computer.Name -Count 1
    If you check the output of "Ping" it is not $true / $false. Add a -Quiet and it should give you your desired result.

    Also on the Get-ADComputer, I would hone in on which OU you are looking for. Something more like:

    $Computers = Get-ADComputer -SearchBase 'OU=Computers,DC=Yoursite,DC=com' -Filter * | Select Name
    • I originally had -Quiet in there and it didn't give any result unfortunately. I'll give it another try and give it some more time to try and populate. I can't hone in on a specific OU in this case, because the machines are all scattered (don't ask).
      • This works for me:
        $Computers = Get-ADComputer -SearchBase 'OU=Computers,DC=Yoursite,DC=com' -Filter * | Select Name
        foreach ($Computer in $Computers)
        {
        Write-Host $Computer.name
        if ((Test-Connection -ComputerName $Computer.name -Count 1 -Quiet) -eq $true)
        {
        try
        { write-host "We got to Try" }
        Catch
        { Write-Host "We Got to Catch" }
        } Else { Write-Host "Computer not online" }
        }

Be the first to answer this question

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ