Created
May 29, 2013 17:56
-
-
Save jasongaylord/5672298 to your computer and use it in GitHub Desktop.
We have CD-Roms that float around and for some reason, we can never find it. This PowerShell script will connect to Active Directory, pull all of the computers, and check each for a CD-Rom. It will populate the machine name, drive letter, disk name (if available), and any errors that result. Keep in mind that a Windows XP machine reports the CD-…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-ADComputerCDRomInfo | |
{ | |
# Search Active Directory for computers | |
([adsisearcher]"objectcategory=computer").findall() | ForEach-Object { | |
# Set computer name | |
$c = ([adsi]$_.path).Name; | |
# Function variables | |
$deviceId = "" | |
$volumeName = "" | |
$err = "" | |
try | |
{ | |
# Grab WMI object | |
$w = Get-WmiObject -ComputerName $c -Class Win32_LogicalDisk -Filter "DriveType = 2 or DriveType = 5" -errorvariable MyErr -erroraction Stop; | |
# If more than one item is returned, loop through items | |
if ($w.Count -gt 0) | |
{ | |
$w | ForEach-Object { | |
# Populate the properties | |
$deviceId = $_.DeviceID | |
$volumeName = $_.VolumeName | |
# If the drive letter length is 0, populate the default error | |
if ($deviceId.Length -eq 0) | |
{ | |
$err = "No drive found." | |
} | |
# Build the object and write the output | |
$obj = new-object psobject | |
$obj | Add-Member noteproperty Computer ($c) | |
$obj | Add-Member noteproperty DriveLetter ($deviceId) | |
$obj | Add-Member noteproperty VolumeName ($volumeName) | |
$obj | Add-Member noteproperty Error ($err) | |
Write-Output $obj | |
} | |
} else { | |
# Populate the properties | |
$deviceId = $w.DeviceID | |
$volumeName = $w.VolumeName | |
# If the drive letter length is 0, populate the default error | |
if ($deviceId.Length -eq 0) | |
{ | |
$err = "No drive found." | |
} | |
# Build the object and write the output | |
$obj = new-object psobject | |
$obj | Add-Member noteproperty Computer ($c) | |
$obj | Add-Member noteproperty DriveLetter ($deviceId) | |
$obj | Add-Member noteproperty VolumeName ($volumeName) | |
$obj | Add-Member noteproperty Error ($err) | |
Write-Output $obj | |
} | |
} | |
Catch [system.exception] | |
{ | |
# Let's make sure we're populating the correct error | |
if ($MyErr.Count -gt 0) | |
{ | |
$err = $MyErr | |
} else { | |
$err = $error[0].tostring() | |
} | |
# Build the object and write the output | |
$obj = new-object psobject | |
$obj | Add-Member noteproperty Computer ($c) | |
$obj | Add-Member noteproperty DriveLetter ($deviceId) | |
$obj | Add-Member noteproperty VolumeName ($volumeName) | |
$obj | Add-Member noteproperty Error ($err) | |
Write-Output $obj | |
} | |
} | |
} | |
# Execute this function | |
Get-ADComputerCDRomInfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment