You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

管理员运行PowerShell仍提示CIM资源不可访问如何解决

PowerShell执行Clear-Disk/New-Partition报CIM访问权限拒绝问题

问题背景

我正在编写PowerShell脚本,用于实现将U盘格式化为FAT32、复制ISO镜像内容至U盘的功能。该脚本此前可正常运行,但重启电脑1天后,即便以管理员身份启动PowerShell,也无权限执行Clear-Disk、New-Partition命令。逐行排查代码后,确认报错输出如下:

Clear-Disk : L’accès à une ressource CIM n’était pas disponible pour le client.
Au caractère C:\Users\Lemaitre\Desktop\stage\script\ISOFastInstall.ps1:36 : 5
+     Clear-Disk -Number 1 -RemoveData -PassThru -RemoveOEM
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : PermissionDenied: (MSFT_Disk (Obje...ows/Storage...):ROOT/Microsoft/Windows/Storage/MSFT_Disk) [Clear-Disk], CimException
    + FullyQualifiedErrorId : MI RESULT 2,Clear-Disk
 New-Partition : L’accès à une ressource CIM n’était pas disponible pour le client.
Au caractère C:\Users\Lemaitre\Desktop\stage\script\ISOFastInstall.ps1:41 : 1
+ New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter E -MbrType F ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo          : PermissionDenied: (MSFT_Disk (Obje...ows/Storage...):ROOT/Microsoft/Windows/Storage/MSFT_Disk) [New-Partition], CimException
    + FullyQualifiedErrorId : MI RESULT 2,New-Partition

注:这是我第一次发布技术问询,也是我的第一个PowerShell项目,若代码编写存在问题欢迎指出。我是法语使用者,英文表述水平有限。

环境信息

  • PowerShell版本:5.1

出错脚本片段

#formatage
$result = [System.Windows.Forms.MessageBox]::Show("Launch l'install? (erase USB drive)", "ISOFastInstall" , 4, 48) 
if ($result -eq 'Yes') {
Clear-Disk -Number 1 -RemoveData -PassThru -RemoveOEM #error here
}
else {exit} 

#chose usb drive letter and create partition 
New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter E -MbrType FAT32 #error here too

排查与解决方法

核心原因排查

这个报错本质是存储管理CIM接口拒绝了操作请求,即便以管理员身份运行也触发,优先按以下顺序排查:

  • 磁盘编号硬编码错误:脚本里写死操作磁盘号1,重启后Windows磁盘编号可能重新分配,Disk 1大概率变成了本地系统盘,系统会直接拦截所有对系统盘的清空、重分区操作,返回权限错误。绝对不要在磁盘操作脚本里硬编码磁盘号。
  • CIM/WMI服务异常:重启后WMI服务、存储服务加载异常会导致CIM资源访问失败,执行以下命令修复即可:
    # 强制重启WMI和存储服务
    Restart-Service Winmgmt,defragsvc -Force
    # 修复损坏的WMI仓库
    winmgmt /salvagerepository
    
    执行完成后关闭当前PowerShell窗口,重新以管理员身份启动再运行脚本。
  • 磁盘被占用:如果U盘正被资源管理器、杀毒软件、虚拟光驱类程序占用,CIM接口无法获取磁盘独占访问权也会报这个错,可以先在资源管理器右键U盘选择「弹出」,临时关闭杀毒软件实时防护后再尝试。

代码逻辑修正

你现有脚本除了触发报错的问题,还有两处参数错误和逻辑风险,修正参考如下:

  1. -MbrType FAT32是无效参数:该参数仅支持FAT12/FAT16/Extended/Huge/IFS几个值,FAT32是文件系统格式,需要创建分区后单独调用Format-Volume执行格式化
  2. 硬编码盘符E可能和现有驱动器冲突,建议让系统自动分配可用盘符
  3. 增加可移动磁盘判断逻辑,避免误操作本地硬盘
    修正后的参考代码:
# 枚举所有可移动、非系统引导的磁盘,让用户手动选择目标U盘
$targetDisk = Get-Disk | Where-Object { $_.Bustype -eq 'USB' -and $_.IsBoot -eq $false } | Out-GridView -Title "选择要操作的目标U盘" -OutputMode Single
if (-not $targetDisk) { exit }

$confirm = [System.Windows.Forms.MessageBox]::Show("确认操作?将清空所选U盘的全部数据", "ISOFastInstall" , 4, 48) 
if ($confirm -ne 'Yes') { exit }

# 清空磁盘,跳过二次确认
Clear-Disk -Number $targetDisk.Number -RemoveData -RemoveOEM -Confirm:$false
# 创建MBR分区,自动分配可用盘符
$newPartition = New-Partition -DiskNumber $targetDisk.Number -UseMaximumSize -AssignDriveLetter -MbrType IFS
# 格式化为FAT32文件系统
Format-Volume -DriveLetter $newPartition.DriveLetter -FileSystem FAT32 -NewFileSystemLabel "ISO_INSTALL" -Confirm:$false

内容的提问来源于stack exchange,提问作者Frenchineer Gaming

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.02 03:03:49