powershellpowershell-5.1

Why do I get "Access to a CIM resource was not available" even tough i run powershell as an administrator?


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

Solution

  • 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.