-3

I found the below code to search a Window 7 workstation for any USB Removable Drive (a memory stick really) on the internet years ago. I still don't fully understand the "/F", "tokens=1*", "tokens=3", "in (fsutil fsinfo drives)" and "in (fsutil fsinfo drivetype %%c)".

Now I have a new Windows 10 workstation and need to convert to PowerShell 5. Can anyone help me get started on how to convert this .bat to .ps1?

I am on... Major 5 Minor 1 Build 16299
Revision 98

@echo off
echo Workstation backup
:tryAgain
set isUSBfound=false
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
   for %%c in (%%b) do (
      for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
         if %%d equ Removable (
            echo Drive %%c is Removable (USB^)
        call "F:\scripts\backups\Backup.bat" %%c
            set isUSBfound=true
         ) 
      )
   )
)
if not %isUSBfound%==true ( 
   echo USB drive not found.  Enter USB drive and press enter to try again. 
   pause
   goto tryAgain
)
date /t
time /t
pause
paulhr
  • 339
  • 4
  • 17
  • I think you're getting downvoted because you just asked people to write code for you instead of attempting to do it yourself and asking questions along the way. And it sounds like you don't even understand what the original code does, and you haven't communicated to us what you want it to do. Saying "search a Window 7 workstation for any USB Removable Drive" is not a full specification. – David Grayson Dec 29 '17 at 06:08
  • Thanks for the guidance David Grayson. It seems I was not clear. All I wanted was something to start my search. The question was "Can anyone help me get **started** on how to convert this .bat to .ps1?" Not please re-write this code for me. Sorry it came across like I was asking people to write code for me. When you don't know what you don't know is hard to word the question correctly. – paulhr Dec 29 '17 at 19:10

2 Answers2

0

You can perform a wmi call that will give you that information:

$UsbDrives = Get-WmiObject -Class Win32_DiskDrive -Filter 'InterfaceType = "USB"'

If ($UsbDrives)
{
    ForEach ($Drive in $UsbDrives)
    {
        Write-Output "Drive '$($Drive.Caption)' is removable"
    }
}
Else
{
    Write-Output 'USB drive not found. Enter a USB and try again.'
}
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • The original .bat file returned a drive letter for later use in the script. The snippet TheIcrrrigible1 gave gives 'DeviceID : \\.\PHYSICALDRIVE2'. How is that mapped to a drive letter? – paulhr Dec 29 '17 at 02:29
0

Bit of a pain due to how far you have to walk from a disk drive where the interface type is to a logical disk where the drive letter is, but the newer CIM cmdlets are still much easier to use than doing everything with Get-WMIObject:

Get-CimInstance -Class Win32_DiskDrive -Filter 'InterfaceType = "USB"' | 
    Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition |
    Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk |
    Format-List *

Or:

Get-CimInstance -Class Win32_DiskDrive -Filter 'InterfaceType = "USB"' |
    Get-CimAssociatedInstance -Association Win32_DiskDriveToDiskPartition |
    Get-CimAssociatedInstance -Association Win32_LogicalDiskToPartition |
    Format-List *

The commands are equivalent.

If you're on PowerShell v2.0, you won't have access to the Get-Cim* cmdlets. Try this instead:

Get-WmiObject -Class Win32_DiskDrive -Filter 'InterfaceType = "USB"' |
    ForEach-Object {
        Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""$($_.DeviceID.Replace('\','\\'))""} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
    } | ForEach-Object {
        Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""$($_.DeviceID.Replace('\','\\'))""} WHERE AssocClass = Win32_LogicalDiskToPartition"
    } |
    Format-List *

You may also want some information that is present within Win32_Volume, but there is no way to map the above three classes (Win32_DiskDrive, Win32_DiskPartition, and Win32_LogicalPartition) directly to a Win32_Volume. This is because a Win32_Volume represents a volume mount point, which isn't a concept in the other three classes. The first three classes use the WinCIM32 provider, while the latter uses the storage volume provider. Be careful that you understand the type of system you're looking at before you start matching the two up because they don't necessarily refer to the same types of things. The storage volume provider was introduced in Windows 7 and Server 2008 R2, so the Win32_Volume class won't be available prior to that.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66