/build/static/layout/Breadcrumb_cap_w.png
07/19/2017 2689 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" }
        }

There are no answers at this time