【问题标题】:Put() sometimes throws exception when trying to assign drive letter to partition尝试将驱动器号分配给分区时,Put() 有时会引发异常
【发布时间】:2015-07-07 09:57:35
【问题描述】:

我有一个简单的脚本,可以将驱动器号分配给任何未命名的分区,如下所示:

function GetNextAvailableLetter
{
  #returns an unused char for drive letter assignment, or $null if none are available
}

foreach ($disk in ( get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null } ) )
{
  $letter = GetNextAvailableLetter
  if ( $letter -ne $null )
  {
    $disk.DriveLetter = $letter + ":"
    $disk.Put()
  }
}

奇怪的是,有时它会起作用,有时Put() 会抛出异常:

Exception calling "Put" with "0" argument(s): "Not supported"

我不知道为什么Put() 会抛出。

【问题讨论】:

  • 我的第一个猜测是$disk 可能包含多个对象。当这种情况发生时,$disk$disk.Count 的输出是什么。想知道那里是否有多个元素
  • @Matt 不幸的是,这似乎不是问题所在。我已经使用 foreach 循环更新了我的问题来澄清。通过这些更改,我仍然遇到同样的错误。
  • 太糟糕了,但至少我们走得更远。也许这会帮助别人。如果有帮助,也看到了这个帖子stackoverflow.com/questions/3230277/powershell-and-diskpart。使用其他一些命令进行挂载。也调用 put 为.Put_。 $disk 的磁盘类型是什么。

标签: windows powershell wmi


【解决方案1】:

我在我的计算机上制作了几个空的无驱动器驱动器,并且能够重新创建此错误以及我认为您可能忽略提及的另一个错误。

Property 'DriveLetter' cannot be found on this object; make sure it exists and is settable.
At line:2 char:1
+ $disk.DriveLetter = "Q:"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Exception calling "Put" with "0" argument(s): "Access is denied.
"
At line:3 char:1
+ $disk.Put()
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

解决这个问题的方法是运行变量$disk,尽管对于每个循环或类似的东西。另一种方法是提前检查$disk.Count

$disk = get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null }
If (($disk) -and ($disk.Count -eq 1)){
    $disk.DriveLetter = "Q:"
    $disk.Put()
}

If 理论上应该在$disk 为空或返回多个对象时保护您免受错误的影响。

【讨论】:

    【解决方案2】:

    根据Scripting Guys

    此错误的原因是 Windows PowerShell 提示未以管理员权限运行。不幸的是,从 WMI 冒出的错误并没有告诉我们问题与权限有关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      相关资源
      最近更新 更多