$knowndrives = @("A:","B:") $knowndrvice = "\.CompanyAuthorizedDevices.txt" $outstr = "" $usbdrives = (Get-WmiObject CIM_LogicalDisk | Where-Object DriveType -eq 2).DeviceID if ($usbdrives.Length -eq 0) { $outstr = $outstr + (Get-Date -Format "yyyy/MM/dd HH:mm") + ", Information, No USB removable device found.`n" } else { foreach ($usbdrive in $usbdrives){ if ( -not ($knowndrives.ToUpper() -contains ($usbdrive).ToUpper())){ $outstr = $outstr + (Get-Date -Format "yyyy/MM/dd HH:mm") + ", Warinig, USB removable device found. (" + $usbdrive + ")`n" if (Test-Path $usbdrive$knowndrvice) { $outstr = $outstr + (Get-Date -Format "yyyy/MM/dd HH:mm") + ", Information, The device is authorized by the company. (" + $usbdrive + ")`n" } else { mountvol $usbdrive /d if ($LASTEXITCODE -eq 0) { $outstr = $outstr + (Get-Date -Format "yyyy/MM/dd HH:mm") + ", Information, Successfully unmounted. (" + $usbdrive + ")`n" } else { $outstr = $outstr + (Get-Date -Format "yyyy/MM/dd HH:mm") + ", Error, Unmounting failure. (" + $usbdrive + ")`n" } } } } } $outstr