Home > Mobile >  Why do I get "Access to a CIM resource was not available" even tough i run powershell as a
Why do I get "Access to a CIM resource was not available" even tough i run powershell as a

Time:06-10

I'm trying to make a script to format a usb drive to fat32 and copy paste an iso content to it. It worked but after a day, I restarted my PC and I no longer have the right to use Clear-Disk and New-Partition commands. I checked my code line by line and the error occurs here:

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-D 
   isk], 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-Par 
   tition], CimException
      FullyQualifiedErrorId : MI RESULT 2,New-Partition

This is my first post and powershell project do not hesitate to tell me if i did something wrong. Also i'm french, sorry for the poor english quality. (by the way im using powershell 5.1 if this can help)

    #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

CodePudding user response:

I found why it isn't working properly, the first time I tried running the script I did it in powershell (running as admin).

The problem is that I used Powershell ISE (also running as admin) to edit my script and even if it run as admin Powershell ISE isn't able to run clear-disk and new-partition.

If someone know how to fix this in Powershell ISE i would be glad to here it, for now i will use the normal Powershell and notepad . Thanks for the help.

CodePudding user response:

I received an Access Denied when the drive was in use other than that it worked.

New-Partition -DiskNumber 1 -UseMaximumSize -DriveLetter E -MbrType FAT32 | Format-Volume

This "Format-Volume" formats the drive at the end

Update

  • Change drive letter to access USB drive (Disk Management)
    • Try to run Scripts again.
  • Run disk checking to check and fix USB drive
    • Right click the USB drive unable to run the script and choose “Properties”.
    • In the pop-up window, click “Tools” tab and click “Check”.
    • After checking, try to run Scripts again.
    • Run check disk from Command Prompt
    • Run Command Prompt as administrator
      • chkdsk g: /f command, where "g" refers to the drive letter of the partition on the USB drive.
  • Related