在Windows Server上为USB设备分配固定的驱动器号(2012) [英] Assigning fixed drive letters to USB Devices on Windows Server (2012)
问题描述
你好朋友Stackoverflowers:)
我正在寻找方法来分配固定的Driveletter到Windows Server 2012(基础)上的USB驱动器。
场景:
我的一个较小的客户有2个USB驱动器用于他的服务器备份,每天更换以获得异地备份。目前他们是手动重新分配的Driveletters如果不匹配。
我已经有一个计划,以实现这一点,但希望在实施之前请求建议,因为我不确定这是最好的解决方案。
我会这样做:
- 启用DriverFramework-UserMode日志(如果未启用)
- 创建由EventID 2106触发的预定任务
- 运行powershell脚本因此问题是:
是否有可能为一组usb设备分配一个固定的驱动盘,而不必在每次连接设备时运行脚本?
如果没有,是否有更好的方法来检测设备何时连接并触发任务?
当然,我做了一些研究,但只找到解决方案为Windows7或Server2008。
预先感谢您对这个主题可以提供的任何洞察。
尊敬的保罗
解决方案我不知道这是否有帮助,但我发现了一个不错的帖子 https://social.technet.microsoft.com/Forums/windowsserver/en-US/09c9814a-38fa-4b16-bc8f-01329882a791/powershell-wmi-get-usb-storage-devices-only 其中以下代码帮助检测连接的USB设备:
$ diskdrive = gwmi win32_diskdrive | ?{$ _。interfacetype -eqUSB}
$ letters = $ diskdrive | %{gwmi -QueryASSOCIATORS OF {Win32_DiskDrive.DeviceID =`$($ _。DeviceID.replace('\','\\'))`} WHERE AssocClass = Win32_DiskDriveToDiskPartition %{gwmi -QueryASSOCIATORS OF {Win32_DiskPartition.DeviceID =`$($ _。DeviceID)`} WHERE AssocClass = Win32_LogicalDiskToPartition} | %{$ _。 deviceid}
$ drive = gwmi win32_volume | ? {$ letters -contains($ _。name -replace\\)}
您可以用以下(必须是管理员)更改驱动器号。
$ drive.DriveLetter =O:
$ drive.Put()
EDIT
OK这里是另一个尝试。以下代码(我在这里找到: http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/13/hey-scripting-guy-april-13-2010.aspx )允许创建当插入USB驱动器时会触发WMI事件。
我向该事件添加了一个操作,以便执行脚本块。
脚本块检查USB驱动器的字母,如果没有,说O:,它会设置它。
使用Wait-Job,因为只要事件存在,作业就会继续运行,我想看到一些输出用于测试。
在任何情况下,更接近你想要做的:
$ scriptblock = {
$ driveLetter =O:
$ diskdrive = gwmi win32_diskdrive | ?{$ _。interfacetype -eqUSB}
$ letters = $ diskdrive | %{gwmi -QueryASSOCIATORS OF {Win32_DiskDrive.DeviceID =`$($ _。DeviceID.replace('\','\\'))`} WHERE AssocClass = Win32_DiskDriveToDiskPartition %{gwmi -QueryASSOCIATORS OF {Win32_DiskPartition.DeviceID =`$($ _。DeviceID)`} WHERE AssocClass = Win32_LogicalDiskToPartition} | %{$ _。 deviceid}
$ drive = gwmi win32_volume | ? {$ letters -contains($ _。name -replace\\)}
if($ drive.DriveLetter -ne $ driveLetter)
{
$驱动器.DriveLetter = $ driveLetter
$ drive.Put()
}
$ drive.DriveLetter
}
$ job = Register-WmiEvent -Query选择* from __InstanceCreationEvent within 5 where targetinstance isa'win32_logicaldisk'-SourceIdentifier usb -Timeout 1000 -Action $ scriptblock
while($ job.State -ne'Stopped')
{
$ job |接收作业
sleep 5
}
注销事件-SourceIdentifier usb -Force | Out-Null
$ job | Remove-Job -force
值得注意的是:
脚本块也会接收$ args作为$事件。我没有使用它们,但他们可能会帮助优化脚本进一步。Hello fellow Stackoverflowers :)
I am looking for ways to assign a fixed Driveletter to USB Drives on Windows Server 2012 (Foundation).
The Scenario: One of my smaller customers has 2 USB Drives for his serverbackup, which are swapped every day to have an offsite backup. Currently they are manually reassigning the Driveletters if there is a mismatch.
I already have a plan to realize this but wanted to ask for advice before implementing it since i am not sure this is the best possible solution.
I would go about it the following way:
- Enable the DriverFramework-UserMode log if it is not enabled
- Create a scheduled task triggered by EventID 2106
- run a powershell script (via the scheduled task) that assigns the driveletter if the DeviceID matches one of the backup drives
So the question is:
Is it somehow possible to assign a fixed driveletter to a set of usb devices without having to run a script everytime a device is attached?
And if not is there a better way to detect when a device is attached and trigger the task?
Of course i did some research but only found solutions for Windows7 or Server2008. I would prefer to solve this without 3rd party tools.
Thank you in advance for any insight you can give on this topic
Regards Paul
解决方案I don't know if this helps much, but I found a nice post at https://social.technet.microsoft.com/Forums/windowsserver/en-US/09c9814a-38fa-4b16-bc8f-01329882a791/powershell-wmi-get-usb-storage-devices-only where the following code helps detect a connected USB device:
$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} $letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_. deviceid} $drive = gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")}
Then you can change the drive letter with the following (must be admin)
$drive.DriveLetter = "O:" $drive.Put()
EDIT
OK here is another try. The following code (which I found here: http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/13/hey-scripting-guy-april-13-2010.aspx) allows to create a WMI event which will fire when a USB drive is plugged in.
I've added an action to that event so that a script block executes. The script block checks the letter of the USB drive and if it's not, say, "O:", it will set it.
I didn't use Wait-Job, as the job will continue running for as long as the event exists, and I wanted to see some output for testing.
In anycase, this should be closer to what you want to do:
$scriptblock = { $driveLetter = "O:" $diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} $letters = $diskdrive | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_. deviceid} $drive = gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")} if ($drive.DriveLetter -ne $driveLetter) { $drive.DriveLetter = $driveLetter $drive.Put() } $drive.DriveLetter } $job = Register-WmiEvent -Query "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'" -SourceIdentifier usb -Timeout 1000 -Action $scriptblock while ($job.State -ne 'Stopped') { $job | Receive-Job sleep 5 } Unregister-Event -SourceIdentifier usb -Force | Out-Null $job | Remove-Job -force
Worth noting: The script block will receive $args as well as $event. I didn't use them but they will probably help optimize the script further.
这篇关于在Windows Server上为USB设备分配固定的驱动器号(2012)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!