Powershell: Test-Connection Problem
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
Answers (0)
Please log in to answer
Be the first to answer this question
$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 - Desktop Jockey 7 years ago
$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" }
} - Desktop Jockey 7 years ago