Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem: USB drive letter auto assignment on insert

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 24-02-2007
Steven
 
Posts: n/a
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?



Reply With Quote
  #2  
Old 24-02-2007
over@my.place
 
Posts: n/a
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.
Reply With Quote
  #3  
Old 25-02-2007
urkec
 
Posts: n/a
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?
>
>
>

Reply With Quote
  #4  
Old 26-02-2007
Steven
 
Posts: n/a
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?
>>
>>
>>



Reply With Quote
  #5  
Old 27-02-2007
urkec
 
Posts: n/a
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.
>


Reply With Quote
  #6  
Old 03-03-2007
Steven
 
Posts: n/a
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.
>>

>



Reply With Quote
  #7  
Old 03-03-2007
urkec
 
Posts: n/a
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
Reply With Quote
  #8  
Old 06-03-2007
Steven
 
Posts: n/a
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



Reply With Quote
  #9  
Old 13-03-2007
Steven
 
Posts: n/a
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

>
>



Reply With Quote
  #10  
Old 29-03-2007
Steven
 
Posts: n/a
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

>>
>>

>
>



Reply With Quote
  #11  
Old 16-05-2007
Steve
 
Posts: n/a
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
>>>
>>>

>>
>>

>
>



Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem: USB drive letter auto assignment on insert"
Thread Thread Starter Forum Replies Last Post
How to disable auto-capitalization of first letter in Outlook eMail writing? Illeana Windows XP Support 4 02-11-2012 12:36 AM
Please insert a disk into drive problem budha Hardware Peripherals 5 09-08-2010 05:18 PM
Start Explorer in Drive View and hide drive letter in Windows 7 Alexis25 Windows Software 3 08-05-2009 01:41 PM
Assigning a Drive letter to External USB connected Hard Drive Ushakanta TroubleShoot 98 1 25-09-2007 01:38 AM


All times are GMT +5.5. The time now is 06:25 PM.