Results 1 to 11 of 11

Thread: Problem: USB drive letter auto assignment on insert

  1. #1
    Steven Guest

    Problem: USB drive letter auto assignment on insert

    Does anyone know of a way to assign a specific drive letter automatically to
    multiple USB drives at the time it is attached?

    I have a situation where two USB drives are swapped in sequence (for daily
    backups) and I need them to be assigned the same drive letter each time.
    Both drives will never be attached to the same system at the same time.

    Scenario:
    1-USB_Drive1 is attached and assigned W:
    2-USB_Drive1 is removed
    3-USB_Drive2 is attached and assigned W:
    4-USB_Drive2 is removed
    5-repeat 1-4

    I thought I could assign both drives a letter via disk manager one at a
    time, but when I attach the previous drive Windows assigns it drive letter
    F: instead of W:. I think Windows only allows one drive letter assignment
    per drive regardless of whether the letter is available or not (no
    multi-drive reservations) [maybe there is a way to do it in the registry?].
    Normally I'd be OK with using two drive letters but in this case it won't
    work as I have no direct control over which of the 2 drives is actually
    attached on which day.

    I saw something (a script I think) a while back for detecting when a USB
    device is attached and notifying a specific user, but I can't find it now
    ("of course"). I was hoping I could just modify 'it' to check for a
    signature file (ie: "DriveSig.txt"), or something on any newly inserted USB
    drives and re-assign the letter accordingly, but I'm at a loss.

    Is there a reliable way to accomplish this goal?



  2. #2
    over@my.place Guest

    Re: Problem: USB drive letter auto assignment on insert

    "Steven" <sdixon@247networks.ca> wrote in
    news:12tuu1alghvgl61@corp.supernews.com:

    > Does anyone know of a way to assign a specific drive letter
    > automatically to multiple USB drives at the time it is attached?
    >
    > I have a situation where two USB drives are swapped in sequence (for
    > daily backups) and I need them to be assigned the same drive letter
    > each time. Both drives will never be attached to the same system at
    > the same time.
    >
    > Scenario:
    > 1-USB_Drive1 is attached and assigned W:
    > 2-USB_Drive1 is removed
    > 3-USB_Drive2 is attached and assigned W:
    > 4-USB_Drive2 is removed
    > 5-repeat 1-4
    >
    > I thought I could assign both drives a letter via disk manager one at
    > a time, but when I attach the previous drive Windows assigns it drive
    > letter F: instead of W:. I think Windows only allows one drive letter
    > assignment per drive regardless of whether the letter is available or
    > not (no multi-drive reservations) [maybe there is a way to do it in
    > the registry?]. Normally I'd be OK with using two drive letters but in
    > this case it won't work as I have no direct control over which of the
    > 2 drives is actually attached on which day.
    >
    > I saw something (a script I think) a while back for detecting when a
    > USB device is attached and notifying a specific user, but I can't find
    > it now ("of course"). I was hoping I could just modify 'it' to check
    > for a signature file (ie: "DriveSig.txt"), or something on any newly
    > inserted USB drives and re-assign the letter accordingly, but I'm at a
    > loss.
    >
    > Is there a reliable way to accomplish this goal?
    >
    >
    >


    One of the freeware utilities available at:

    http://www.uwe-sieber.de

    may be of use.

  3. #3
    urkec Guest

    RE: Problem: USB drive letter auto assignment on insert

    This will detect when a removable drive (type 2) is attached to a computer
    and echo it's letter:



    intRemovableDisk = 2
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")

    Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
    & "Targetinstance ISA 'Win32_LogicalDisk' " & _
    "AND TargetInstance.DriveType=" & intRemovableDisk)

    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.DeviceID


    You could then use Win32_Volume WMI class to change the drive letter (This
    class is available only in Windows 2003 - did not test this):



    Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = '" & DeviceID & "\\'")

    for each objVolume in colVolumes
    objVolume.DriveLetter = "X:"
    objVolume.Put_
    Next



    I am not sure what can be used instead of this in other Windows versions.
    Perhaps you can try DiskPart.exe. How is your backup set up? Maybe there is a
    simpler solution.
    --
    urkec


    "Steven" wrote:

    > Does anyone know of a way to assign a specific drive letter automatically to
    > multiple USB drives at the time it is attached?
    >
    > I have a situation where two USB drives are swapped in sequence (for daily
    > backups) and I need them to be assigned the same drive letter each time.
    > Both drives will never be attached to the same system at the same time.
    >
    > Scenario:
    > 1-USB_Drive1 is attached and assigned W:
    > 2-USB_Drive1 is removed
    > 3-USB_Drive2 is attached and assigned W:
    > 4-USB_Drive2 is removed
    > 5-repeat 1-4
    >
    > I thought I could assign both drives a letter via disk manager one at a
    > time, but when I attach the previous drive Windows assigns it drive letter
    > F: instead of W:. I think Windows only allows one drive letter assignment
    > per drive regardless of whether the letter is available or not (no
    > multi-drive reservations) [maybe there is a way to do it in the registry?].
    > Normally I'd be OK with using two drive letters but in this case it won't
    > work as I have no direct control over which of the 2 drives is actually
    > attached on which day.
    >
    > I saw something (a script I think) a while back for detecting when a USB
    > device is attached and notifying a specific user, but I can't find it now
    > ("of course"). I was hoping I could just modify 'it' to check for a
    > signature file (ie: "DriveSig.txt"), or something on any newly inserted USB
    > drives and re-assign the letter accordingly, but I'm at a loss.
    >
    > Is there a reliable way to accomplish this goal?
    >
    >
    >


  4. #4
    Steven Guest

    Re: Problem: USB drive letter auto assignment on insert

    I will try out your suggestion ASAP as it just so happens they are running
    Server 2003. Do you know if these WMI classes are also in XP?

    The exact backup is as follows:
    - Use ntbackup.exe to create either a differential or full backup to
    W:\Full\???.bkf (or W:\Diff\???.bkf)
    - Full backup once a week, with differentials the remaining days
    - Drive to be swapped daily by assigned person (non-technical)

    With a better tool like BackupExec I could have just added two drive letters
    to the same media pool and been done with it, but ntbackup (or the RSM
    really) does not seem to support this option.


    "urkec" <urkec@discussions.microsoft.com> wrote in message
    news:F0D9E452-2E57-40C5-B8EE-4914AFBEF972@microsoft.com...
    > This will detect when a removable drive (type 2) is attached to a computer
    > and echo it's letter:
    >
    >
    >
    > intRemovableDisk = 2
    > strComputer = "."
    >
    > Set objWMIService = GetObject("winmgmts:" _
    > & "{impersonationLevel=impersonate}!\\" & _
    > strComputer & "\root\cimv2")
    >
    > Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    > ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
    > & "Targetinstance ISA 'Win32_LogicalDisk' " & _
    > "AND TargetInstance.DriveType=" & intRemovableDisk)
    >
    > Set objLatestEvent = colMonitoredEvents.NextEvent
    > Wscript.Echo objLatestEvent.TargetInstance.DeviceID
    >
    >
    > You could then use Win32_Volume WMI class to change the drive letter (This
    > class is available only in Windows 2003 - did not test this):
    >
    >
    >
    > Set colVolumes = objWMIService.ExecQuery _
    > ("Select * from Win32_Volume Where Name = '" & DeviceID & "\\'")
    >
    > for each objVolume in colVolumes
    > objVolume.DriveLetter = "X:"
    > objVolume.Put_
    > Next
    >
    >
    >
    > I am not sure what can be used instead of this in other Windows versions.
    > Perhaps you can try DiskPart.exe. How is your backup set up? Maybe there
    > is a
    > simpler solution.
    > --
    > urkec
    >
    >
    > "Steven" wrote:
    >
    >> Does anyone know of a way to assign a specific drive letter automatically
    >> to
    >> multiple USB drives at the time it is attached?
    >>
    >> I have a situation where two USB drives are swapped in sequence (for
    >> daily
    >> backups) and I need them to be assigned the same drive letter each time.
    >> Both drives will never be attached to the same system at the same time.
    >>
    >> Scenario:
    >> 1-USB_Drive1 is attached and assigned W:
    >> 2-USB_Drive1 is removed
    >> 3-USB_Drive2 is attached and assigned W:
    >> 4-USB_Drive2 is removed
    >> 5-repeat 1-4
    >>
    >> I thought I could assign both drives a letter via disk manager one at a
    >> time, but when I attach the previous drive Windows assigns it drive
    >> letter
    >> F: instead of W:. I think Windows only allows one drive letter
    >> assignment
    >> per drive regardless of whether the letter is available or not (no
    >> multi-drive reservations) [maybe there is a way to do it in the
    >> registry?].
    >> Normally I'd be OK with using two drive letters but in this case it won't
    >> work as I have no direct control over which of the 2 drives is actually
    >> attached on which day.
    >>
    >> I saw something (a script I think) a while back for detecting when a USB
    >> device is attached and notifying a specific user, but I can't find it now
    >> ("of course"). I was hoping I could just modify 'it' to check for a
    >> signature file (ie: "DriveSig.txt"), or something on any newly inserted
    >> USB
    >> drives and re-assign the letter accordingly, but I'm at a loss.
    >>
    >> Is there a reliable way to accomplish this goal?
    >>
    >>
    >>




  5. #5
    urkec Guest

    Re: Problem: USB drive letter auto assignment on insert

    You can use Win32_LogicalDisk in XP, but not Win32_Volume .

    Have you considered creating backup folder on a local drive, then copy it to
    removable drives regardles of drive letters?


    --
    urkec


    "Steven" wrote:

    > I will try out your suggestion ASAP as it just so happens they are running
    > Server 2003. Do you know if these WMI classes are also in XP?
    >
    > The exact backup is as follows:
    > - Use ntbackup.exe to create either a differential or full backup to
    > W:\Full\???.bkf (or W:\Diff\???.bkf)
    > - Full backup once a week, with differentials the remaining days
    > - Drive to be swapped daily by assigned person (non-technical)
    >
    > With a better tool like BackupExec I could have just added two drive letters
    > to the same media pool and been done with it, but ntbackup (or the RSM
    > really) does not seem to support this option.
    >



  6. #6
    Steven Guest

    Re: Problem: USB drive letter auto assignment on insert

    Local backup file is not an option (low available disk space).
    I've been working on the static drive letter script and nearly have it
    complete.
    It runs in the background continuously right now but has to loaded manually
    (or via startup item).

    Question:
    -can I do Boolean logic inside an IF statement: as in "if (x=something OR
    x=somethingelse) then dosomething"
    --if so, what is the syntax (keyword OR, || (pipes) or something else)?

    Also, does anyone know of a clean way to make my script run as a service
    instead (I know there's something in the resource kit to do this but don't
    remember the name of it).

    "urkec" <urkec@discussions.microsoft.com> wrote in message
    news:B763DFD6-8984-410E-95E9-3780ED0F69FC@microsoft.com...
    > You can use Win32_LogicalDisk in XP, but not Win32_Volume .
    >
    > Have you considered creating backup folder on a local drive, then copy it
    > to
    > removable drives regardles of drive letters?
    >
    >
    > --
    > urkec
    >
    >
    > "Steven" wrote:
    >
    >> I will try out your suggestion ASAP as it just so happens they are
    >> running
    >> Server 2003. Do you know if these WMI classes are also in XP?
    >>
    >> The exact backup is as follows:
    >> - Use ntbackup.exe to create either a differential or full backup to
    >> W:\Full\???.bkf (or W:\Diff\???.bkf)
    >> - Full backup once a week, with differentials the remaining days
    >> - Drive to be swapped daily by assigned person (non-technical)
    >>
    >> With a better tool like BackupExec I could have just added two drive
    >> letters
    >> to the same media pool and been done with it, but ntbackup (or the RSM
    >> really) does not seem to support this option.
    >>

    >




  7. #7
    urkec Guest

    Re: Problem: USB drive letter auto assignment on insert

    > Local backup file is not an option (low available disk space).
    > I've been working on the static drive letter script and nearly have it
    > complete.
    > It runs in the background continuously right now but has to loaded manually
    > (or via startup item).
    >
    > Question:
    > -can I do Boolean logic inside an IF statement: as in "if (x=something OR
    > x=somethingelse) then dosomething"
    > --if so, what is the syntax (keyword OR, || (pipes) or something else)?


    If you want to select drives you can use:

    Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk where Name='X:' or Name='Y:'")

    or:

    if objDisk.Name = "X:" or objDisk.Name = "Y:" then
    ....
    end if

    >
    > Also, does anyone know of a clean way to make my script run as a service
    > instead (I know there's something in the resource kit to do this but don't
    > remember the name of it).


    I think it is srvany.exe

    Also check this from Windows 2000 scripting guide (permanent event
    subscription )

    http://www.microsoft.com/technet/scr....mspx?mfr=true

  8. #8
    Steven Guest

    Re: Problem: USB drive letter auto assignment on insert

    As promised I made the script and posted it here for others to use. I've
    got my version (modified for two drives) running via the task manager "at
    startup" and it seems to be working. This version uses the drive's
    VolumeName (Label) property to identify the drive. If someone wants to
    modify it to work under Windows 2000 please go ahead and post the mods here.

    PS: I used the WMI code creator wizard to make most of the code here
    (exposes nearly all WMI options, except the "Put_" function)

    Here's my script 'as is' (use task manager to end the process "wscript.exe"
    to end it):
    'wscript.echo "start."
    'only works under Windows Server 2003
    'Static_USB_Letter.VBS

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set MySink = WScript.CreateObject( _
    "WbemScripting.SWbemSink","SINK_")

    objWMIservice.ExecNotificationQueryAsync MySink, _
    "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    ISA 'Win32_LogicalDisk'"

    'WScript.Echo "Waiting for events..."

    While (True)
    Wscript.Sleep(1000)
    Wend

    Sub SINK_OnObjectReady(objObject, objAsyncContext)
    if ("Drive_Label" = objObject.TargetInstance.volumename) then
    'only server 2003 supports these two lines
    objVolume.DriveLetter = "W:"
    objVolume.Put_
    end if
    End Sub

    Sub SINK_OnCompleted(objObject, objAsyncContext)
    WScript.Echo "Event call complete."
    End Sub

    'wscript.echo "end."

    "urkec" <urkec@discussions.microsoft.com> wrote in message
    news:66D23FA8-C77F-46E8-ABE4-B64EA3445EF0@microsoft.com...
    >> Local backup file is not an option (low available disk space).
    >> I've been working on the static drive letter script and nearly have it
    >> complete.
    >> It runs in the background continuously right now but has to loaded
    >> manually
    >> (or via startup item).
    >>
    >> Question:
    >> -can I do Boolean logic inside an IF statement: as in "if (x=something OR
    >> x=somethingelse) then dosomething"
    >> --if so, what is the syntax (keyword OR, || (pipes) or something else)?

    >
    > If you want to select drives you can use:
    >
    > Set colDisks = objWMIService.ExecQuery _
    > ("Select * from Win32_LogicalDisk where Name='X:' or Name='Y:'")
    >
    > or:
    >
    > if objDisk.Name = "X:" or objDisk.Name = "Y:" then
    > ...
    > end if
    >
    >>
    >> Also, does anyone know of a clean way to make my script run as a service
    >> instead (I know there's something in the resource kit to do this but
    >> don't
    >> remember the name of it).

    >
    > I think it is srvany.exe
    >
    > Also check this from Windows 2000 scripting guide (permanent event
    > subscription )
    >
    > http://www.microsoft.com/technet/scr....mspx?mfr=true




  9. #9
    Steven Guest

    Re: Problem: USB drive letter auto assignment on insert

    Opps. Posted wrong version (the last one will not work), I'm sure this can
    be optimized some but here's what I got:

    'only works under Windows Server 2003
    'wscript.echo "start."
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set MySink = WScript.CreateObject( _
    "WbemScripting.SWbemSink","SINK_")

    objWMIservice.ExecNotificationQueryAsync MySink, _
    "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    ISA 'Win32_LogicalDisk'"

    'WScript.Echo "Waiting for events..."
    While (True)
    Wscript.Sleep(1000)
    Wend

    Sub SINK_OnObjectReady(objObject, objAsyncContext)
    if ("Disk1" = objObject.TargetInstance.volumename) then
    Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = '" &
    objObject.TargetInstance.DeviceID & "\\'")

    for each objVolume in colVolumes
    objVolume.DriveLetter = "W:"
    objVolume.Put_
    Next
    end if
    if ("Disk2" = objObject.TargetInstance.volumename) then
    Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = '" &
    objObject.TargetInstance.DeviceID & "\\'")

    for each objVolume in colVolumes
    objVolume.DriveLetter = "W:"
    objVolume.Put_
    Next
    end if
    End Sub

    Sub SINK_OnCompleted(objObject, objAsyncContext)
    WScript.Echo "Event call complete."
    End Sub

    'wscript.echo "end."

    "Steven" <sdixon@247networks.ca> wrote in message
    news:12urc13841iaqf0@corp.supernews.com...
    > As promised I made the script and posted it here for others to use. I've
    > got my version (modified for two drives) running via the task manager "at
    > startup" and it seems to be working. This version uses the drive's
    > VolumeName (Label) property to identify the drive. If someone wants to
    > modify it to work under Windows 2000 please go ahead and post the mods
    > here.
    >
    > PS: I used the WMI code creator wizard to make most of the code here
    > (exposes nearly all WMI options, except the "Put_" function)
    >
    > Here's my script 'as is' (use task manager to end the process
    > "wscript.exe" to end it):
    > 'wscript.echo "start."
    > 'only works under Windows Server 2003
    > 'Static_USB_Letter.VBS
    >
    > strComputer = "."
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    > Set MySink = WScript.CreateObject( _
    > "WbemScripting.SWbemSink","SINK_")
    >
    > objWMIservice.ExecNotificationQueryAsync MySink, _
    > "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    > ISA 'Win32_LogicalDisk'"
    >
    > 'WScript.Echo "Waiting for events..."
    >
    > While (True)
    > Wscript.Sleep(1000)
    > Wend
    >
    > Sub SINK_OnObjectReady(objObject, objAsyncContext)
    > if ("Drive_Label" = objObject.TargetInstance.volumename) then
    > 'only server 2003 supports these two lines
    > objVolume.DriveLetter = "W:"
    > objVolume.Put_
    > end if
    > End Sub
    >
    > Sub SINK_OnCompleted(objObject, objAsyncContext)
    > WScript.Echo "Event call complete."
    > End Sub
    >
    > 'wscript.echo "end."
    >
    > "urkec" <urkec@discussions.microsoft.com> wrote in message
    > news:66D23FA8-C77F-46E8-ABE4-B64EA3445EF0@microsoft.com...
    >>> Local backup file is not an option (low available disk space).
    >>> I've been working on the static drive letter script and nearly have it
    >>> complete.
    >>> It runs in the background continuously right now but has to loaded
    >>> manually
    >>> (or via startup item).
    >>>
    >>> Question:
    >>> -can I do Boolean logic inside an IF statement: as in "if (x=something
    >>> OR
    >>> x=somethingelse) then dosomething"
    >>> --if so, what is the syntax (keyword OR, || (pipes) or something else)?

    >>
    >> If you want to select drives you can use:
    >>
    >> Set colDisks = objWMIService.ExecQuery _
    >> ("Select * from Win32_LogicalDisk where Name='X:' or Name='Y:'")
    >>
    >> or:
    >>
    >> if objDisk.Name = "X:" or objDisk.Name = "Y:" then
    >> ...
    >> end if
    >>
    >>>
    >>> Also, does anyone know of a clean way to make my script run as a service
    >>> instead (I know there's something in the resource kit to do this but
    >>> don't
    >>> remember the name of it).

    >>
    >> I think it is srvany.exe
    >>
    >> Also check this from Windows 2000 scripting guide (permanent event
    >> subscription )
    >>
    >> http://www.microsoft.com/technet/scr....mspx?mfr=true

    >
    >




  10. #10
    Steven Guest

    Re: Problem: USB drive letter auto assignment on insert

    I might have found a slight problem with this script:
    -seems to loop infinitely (what I want it to do) but every 5 to 10 seconds
    or so the floppy drive lights up like windows is scanning for new drives
    -I put in some markers (debug lines) and each drive triggers the routine
    twice (once after the drive is attached, once after the drive letter is
    changed), I think a simple check to see if it the drive letter is correct
    before changing it should fix this though
    -I'll do some more testing to see if anything can be optimized or if there
    is a memory leak somewhere as I've noticed the task scheduler indicated a
    80015000 error (number may be wrong as it now says "1") last week.

    Anyone else found any errors or weird issues in this script?

    "Steven" <sdixon@247networks.ca> wrote in message
    news:12vdo191ads6mc7@corp.supernews.com...
    > Opps. Posted wrong version (the last one will not work), I'm sure this can
    > be optimized some but here's what I got:
    >
    > 'only works under Windows Server 2003
    > 'wscript.echo "start."
    > strComputer = "."
    > Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    > Set MySink = WScript.CreateObject( _
    > "WbemScripting.SWbemSink","SINK_")
    >
    > objWMIservice.ExecNotificationQueryAsync MySink, _
    > "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    > ISA 'Win32_LogicalDisk'"
    >
    > 'WScript.Echo "Waiting for events..."
    > While (True)
    > Wscript.Sleep(1000)
    > Wend
    >
    > Sub SINK_OnObjectReady(objObject, objAsyncContext)
    > if ("Disk1" = objObject.TargetInstance.volumename) then
    > Set colVolumes = objWMIService.ExecQuery _
    > ("Select * from Win32_Volume Where Name = '" &
    > objObject.TargetInstance.DeviceID & "\\'")
    >
    > for each objVolume in colVolumes
    > objVolume.DriveLetter = "W:"
    > objVolume.Put_
    > Next
    > end if
    > if ("Disk2" = objObject.TargetInstance.volumename) then
    > Set colVolumes = objWMIService.ExecQuery _
    > ("Select * from Win32_Volume Where Name = '" &
    > objObject.TargetInstance.DeviceID & "\\'")
    >
    > for each objVolume in colVolumes
    > objVolume.DriveLetter = "W:"
    > objVolume.Put_
    > Next
    > end if
    > End Sub
    >
    > Sub SINK_OnCompleted(objObject, objAsyncContext)
    > WScript.Echo "Event call complete."
    > End Sub
    >
    > 'wscript.echo "end."
    >
    > "Steven" <sdixon@247networks.ca> wrote in message
    > news:12urc13841iaqf0@corp.supernews.com...
    >> As promised I made the script and posted it here for others to use. I've
    >> got my version (modified for two drives) running via the task manager "at
    >> startup" and it seems to be working. This version uses the drive's
    >> VolumeName (Label) property to identify the drive. If someone wants to
    >> modify it to work under Windows 2000 please go ahead and post the mods
    >> here.
    >>
    >> PS: I used the WMI code creator wizard to make most of the code here
    >> (exposes nearly all WMI options, except the "Put_" function)
    >>
    >> Here's my script 'as is' (use task manager to end the process
    >> "wscript.exe" to end it):
    >> 'wscript.echo "start."
    >> 'only works under Windows Server 2003
    >> 'Static_USB_Letter.VBS
    >>
    >> strComputer = "."
    >> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    >> "\root\CIMV2")
    >> Set MySink = WScript.CreateObject( _
    >> "WbemScripting.SWbemSink","SINK_")
    >>
    >> objWMIservice.ExecNotificationQueryAsync MySink, _
    >> "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    >> ISA 'Win32_LogicalDisk'"
    >>
    >> 'WScript.Echo "Waiting for events..."
    >>
    >> While (True)
    >> Wscript.Sleep(1000)
    >> Wend
    >>
    >> Sub SINK_OnObjectReady(objObject, objAsyncContext)
    >> if ("Drive_Label" = objObject.TargetInstance.volumename) then
    >> 'only server 2003 supports these two lines
    >> objVolume.DriveLetter = "W:"
    >> objVolume.Put_
    >> end if
    >> End Sub
    >>
    >> Sub SINK_OnCompleted(objObject, objAsyncContext)
    >> WScript.Echo "Event call complete."
    >> End Sub
    >>
    >> 'wscript.echo "end."
    >>
    >> "urkec" <urkec@discussions.microsoft.com> wrote in message
    >> news:66D23FA8-C77F-46E8-ABE4-B64EA3445EF0@microsoft.com...
    >>>> Local backup file is not an option (low available disk space).
    >>>> I've been working on the static drive letter script and nearly have it
    >>>> complete.
    >>>> It runs in the background continuously right now but has to loaded
    >>>> manually
    >>>> (or via startup item).
    >>>>
    >>>> Question:
    >>>> -can I do Boolean logic inside an IF statement: as in "if (x=something
    >>>> OR
    >>>> x=somethingelse) then dosomething"
    >>>> --if so, what is the syntax (keyword OR, || (pipes) or something else)?
    >>>
    >>> If you want to select drives you can use:
    >>>
    >>> Set colDisks = objWMIService.ExecQuery _
    >>> ("Select * from Win32_LogicalDisk where Name='X:' or Name='Y:'")
    >>>
    >>> or:
    >>>
    >>> if objDisk.Name = "X:" or objDisk.Name = "Y:" then
    >>> ...
    >>> end if
    >>>
    >>>>
    >>>> Also, does anyone know of a clean way to make my script run as a
    >>>> service
    >>>> instead (I know there's something in the resource kit to do this but
    >>>> don't
    >>>> remember the name of it).
    >>>
    >>> I think it is srvany.exe
    >>>
    >>> Also check this from Windows 2000 scripting guide (permanent event
    >>> subscription )
    >>>
    >>> http://www.microsoft.com/technet/scr....mspx?mfr=true

    >>
    >>

    >
    >




  11. #11
    Steve Guest

    Re: Problem: USB drive letter auto assignment on insert

    I confirmed there is a problem with binding to the "creation instance"
    object in the previous code.
    I reworked the code to react to different events:
    1-listen for any new drive letter change events
    2-find out if any drive has a label matching a specific string
    3-change that drives letter
    4-repeat steps 1-3

    I've now confirmed the drive access every 10 seconds issue is no more and it
    now runs in the background properly, even if no one even logs in. Still
    only works under Server 2003 unless someone else has figured out a way to
    programmatically do it under XP/2000.

    After looking at the code a bit more I now realize that I may have
    introduced a memory leak of sorts because I create several new objects each
    time a drive is attached but I never destroy them when the routine exits.
    They may get released each time I reuse the variables/objects but I'm not
    sure.

    Q--Can someone suggest how to explicitly release an object when I'm done
    with it?

    Here is the revised code (a bit messy but I left the comments in place for
    troubleshooting):
    kingston and cruzer are my test flash keys but the code works for USB HDD's
    too.
    [start]
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2")

    'Wscript.Echo "Waiting for events ..."
    Do While(True)
    Set objReceivedEvent = objEvents.NextEvent

    'report an event
    'Wscript.Echo "Win32_VolumeChangeEvent event has occurred."

    Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_LogicalDisk WHERE VolumeName='KINGSTON' OR
    VolumeName='CRUZER'",,48)
    For Each objItem in colItems
    ' Wscript.Echo "Win32_LogicalDisk instance"
    ' Wscript.Echo objItem.Name & objItem.VolumeName

    'Set drive letter
    Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Volume WHERE Label = 'CRUZER' OR
    Label='KINGSTON'",,48)
    For Each volumeItem in colItems
    'Wscript.Echo "Win32_Volume instance"
    volumeItem.DriveLetter = "W:"
    volumeItem.Put_
    Next

    Next

    Loop
    [end]

    "Steven" <sdixon@247networks.ca> wrote in message
    news:130m0e5ijuhf227@corp.supernews.com...
    >I might have found a slight problem with this script:
    > -seems to loop infinitely (what I want it to do) but every 5 to 10 seconds
    > or so the floppy drive lights up like windows is scanning for new drives
    > -I put in some markers (debug lines) and each drive triggers the routine
    > twice (once after the drive is attached, once after the drive letter is
    > changed), I think a simple check to see if it the drive letter is correct
    > before changing it should fix this though
    > -I'll do some more testing to see if anything can be optimized or if there
    > is a memory leak somewhere as I've noticed the task scheduler indicated a
    > 80015000 error (number may be wrong as it now says "1") last week.
    >
    > Anyone else found any errors or weird issues in this script?
    >
    > "Steven" <sdixon@247networks.ca> wrote in message
    > news:12vdo191ads6mc7@corp.supernews.com...
    >> Opps. Posted wrong version (the last one will not work), I'm sure this
    >> can be optimized some but here's what I got:
    >>
    >> 'only works under Windows Server 2003
    >> 'wscript.echo "start."
    >> strComputer = "."
    >> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    >> "\root\CIMV2")
    >> Set MySink = WScript.CreateObject( _
    >> "WbemScripting.SWbemSink","SINK_")
    >>
    >> objWMIservice.ExecNotificationQueryAsync MySink, _
    >> "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    >> ISA 'Win32_LogicalDisk'"
    >>
    >> 'WScript.Echo "Waiting for events..."
    >> While (True)
    >> Wscript.Sleep(1000)
    >> Wend
    >>
    >> Sub SINK_OnObjectReady(objObject, objAsyncContext)
    >> if ("Disk1" = objObject.TargetInstance.volumename) then
    >> Set colVolumes = objWMIService.ExecQuery _
    >> ("Select * from Win32_Volume Where Name = '" &
    >> objObject.TargetInstance.DeviceID & "\\'")
    >>
    >> for each objVolume in colVolumes
    >> objVolume.DriveLetter = "W:"
    >> objVolume.Put_
    >> Next
    >> end if
    >> if ("Disk2" = objObject.TargetInstance.volumename) then
    >> Set colVolumes = objWMIService.ExecQuery _
    >> ("Select * from Win32_Volume Where Name = '" &
    >> objObject.TargetInstance.DeviceID & "\\'")
    >>
    >> for each objVolume in colVolumes
    >> objVolume.DriveLetter = "W:"
    >> objVolume.Put_
    >> Next
    >> end if
    >> End Sub
    >>
    >> Sub SINK_OnCompleted(objObject, objAsyncContext)
    >> WScript.Echo "Event call complete."
    >> End Sub
    >>
    >> 'wscript.echo "end."
    >>
    >> "Steven" <sdixon@247networks.ca> wrote in message
    >> news:12urc13841iaqf0@corp.supernews.com...
    >>> As promised I made the script and posted it here for others to use.
    >>> I've got my version (modified for two drives) running via the task
    >>> manager "at startup" and it seems to be working. This version uses the
    >>> drive's VolumeName (Label) property to identify the drive. If someone
    >>> wants to modify it to work under Windows 2000 please go ahead and post
    >>> the mods here.
    >>>
    >>> PS: I used the WMI code creator wizard to make most of the code here
    >>> (exposes nearly all WMI options, except the "Put_" function)
    >>>
    >>> Here's my script 'as is' (use task manager to end the process
    >>> "wscript.exe" to end it):
    >>> 'wscript.echo "start."
    >>> 'only works under Windows Server 2003
    >>> 'Static_USB_Letter.VBS
    >>>
    >>> strComputer = "."
    >>> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
    >>> "\root\CIMV2")
    >>> Set MySink = WScript.CreateObject( _
    >>> "WbemScripting.SWbemSink","SINK_")
    >>>
    >>> objWMIservice.ExecNotificationQueryAsync MySink, _
    >>> "SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE TargetInstance
    >>> ISA 'Win32_LogicalDisk'"
    >>>
    >>> 'WScript.Echo "Waiting for events..."
    >>>
    >>> While (True)
    >>> Wscript.Sleep(1000)
    >>> Wend
    >>>
    >>> Sub SINK_OnObjectReady(objObject, objAsyncContext)
    >>> if ("Drive_Label" = objObject.TargetInstance.volumename) then
    >>> 'only server 2003 supports these two lines
    >>> objVolume.DriveLetter = "W:"
    >>> objVolume.Put_
    >>> end if
    >>> End Sub
    >>>
    >>> Sub SINK_OnCompleted(objObject, objAsyncContext)
    >>> WScript.Echo "Event call complete."
    >>> End Sub
    >>>
    >>> 'wscript.echo "end."
    >>>
    >>> "urkec" <urkec@discussions.microsoft.com> wrote in message
    >>> news:66D23FA8-C77F-46E8-ABE4-B64EA3445EF0@microsoft.com...
    >>>>> Local backup file is not an option (low available disk space).
    >>>>> I've been working on the static drive letter script and nearly have it
    >>>>> complete.
    >>>>> It runs in the background continuously right now but has to loaded
    >>>>> manually
    >>>>> (or via startup item).
    >>>>>
    >>>>> Question:
    >>>>> -can I do Boolean logic inside an IF statement: as in "if (x=something
    >>>>> OR
    >>>>> x=somethingelse) then dosomething"
    >>>>> --if so, what is the syntax (keyword OR, || (pipes) or something
    >>>>> else)?
    >>>>
    >>>> If you want to select drives you can use:
    >>>>
    >>>> Set colDisks = objWMIService.ExecQuery _
    >>>> ("Select * from Win32_LogicalDisk where Name='X:' or Name='Y:'")
    >>>>
    >>>> or:
    >>>>
    >>>> if objDisk.Name = "X:" or objDisk.Name = "Y:" then
    >>>> ...
    >>>> end if
    >>>>
    >>>>>
    >>>>> Also, does anyone know of a clean way to make my script run as a
    >>>>> service
    >>>>> instead (I know there's something in the resource kit to do this but
    >>>>> don't
    >>>>> remember the name of it).
    >>>>
    >>>> I think it is srvany.exe
    >>>>
    >>>> Also check this from Windows 2000 scripting guide (permanent event
    >>>> subscription )
    >>>>
    >>>> http://www.microsoft.com/technet/scr....mspx?mfr=true
    >>>
    >>>

    >>
    >>

    >
    >




Similar Threads

  1. Replies: 4
    Last Post: 02-11-2012, 12:36 AM
  2. Please insert a disk into drive problem
    By budha in forum Hardware Peripherals
    Replies: 5
    Last Post: 09-08-2010, 05:18 PM
  3. Replies: 3
    Last Post: 08-05-2009, 01:41 PM
  4. Replies: 1
    Last Post: 25-09-2007, 01:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,391,652.56097 seconds with 16 queries