Skip to content

Instantly share code, notes, and snippets.

@elovelan
Forked from jasongaylord/Get-ADComputerCDRomInfo.ps1
Last active August 29, 2015 13:57
Show Gist options
  • Save elovelan/9900582 to your computer and use it in GitHub Desktop.
Save elovelan/9900582 to your computer and use it in GitHub Desktop.
Function PrintDetails
{
Param (
$ComputerName,
$DriveLetter = "",
$VolumeName = "",
$Error = ""
)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name Computer ($ComputerName)
$obj | Add-Member -MemberType NoteProperty -Name DriveLetter ($DriveLetter)
$obj | Add-Member -MemberType NoteProperty -Name VolumeName ($VolumeName)
$obj | Add-Member -MemberType NoteProperty -Name Error ($Error)
Write-Output $obj
}
Function PrintDriveDetails
{
Param ( $ComputerName = 'localhost' )
# Get all removable (2) or cd-rom (5) drives
Get-WmiObject `
-ComputerName $computerName `
-Class Win32_LogicalDisk `
-Filter "DriveType = 2 or DriveType = 5" `
-ErrorAction Stop `
| ForEach-Object {
# Populate the properties
$driveLetter = $_.DeviceID
$volumeName = $_.VolumeName
$err = ""
if (!$driveLetter)
{
$err = "No drive found."
}
PrintDetails `
-ComputerName $computerName `
-DriveLetter $driveLetter `
-VolumeName $volumeName `
-Error $err
}
}
Function Get-ADComputerCDRomInfo
{
# Search Active Directory for computers
([adsisearcher]"objectcategory=computer").findall() | ForEach-Object {
$computerName = ([adsi]$_.path).Name;
Try {
PrintDriveDetails -ComputerName $computerName
}
Catch
{
PrintDetails `
-ComputerName $computerName `
-Error $Error[0].ToString()
}
}
}
# Execute this function
Get-ADComputerCDRomInfo
@elovelan
Copy link
Author

DRYed up a bit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment