3

I try to switch from WMI to CIM, but there are methods missing: WMI way so select the next best VSS snapshot:

$SnapShot = (Get-WmiObject Win32_ShadowCopy)[0]

And then you have the working method:

$SnapShot.Delete()

However Get-CimInstance does not give me the methods by Design.

$SnapShot = (Get-CimInstance Win32_ShadowCopy)[0] and Get-CimClass -ClassName Win32_ShadowCopy only shows the methods "Create" and "Revert" - Where is the "Delete" method?

Normally if would be something like Invoke-CimMethod -ClassName Win32_ShadowCopy -MethodName "Delete" -Arguments @{ID="$SnapShot.ID"}. but no...

I've tried a lot of combinations to access or even see the "Delete" method, but where is it? WMI is not encouraged to use 'cause it is "old" when CIM is avail, but with CIM there are often methods missing or hidden somewhere non-obvious, like in this example.

Yes, I can use vssadmin.exe delete shadows /Shadow=$($SnapShot.ID) /Quiet but this is not the clean way, just a dirty workaround.

Joachim Otahal
  • 272
  • 2
  • 9
  • 3
    Probably pipe to remove-ciminstance like with win32_userprofile. – js2010 Mar 19 '22 at 14:26
  • 2
    As aside, `"$SnapShot.ID"` is not expanding the `ID` property of `$SnapShot` due to the double quotes. – Santiago Squarzon Mar 19 '22 at 15:18
  • @SantiagoSquarzon the $Snapshot.ID is expanded since it is in double quotes, I can see it at the error output of Invoke-CimMethod where the ID is listed. If I'd used single quotes you'd be right, since powershell does not expand $Snapshot.ID in single quotes. Just try it, and you will see. Maybe mix-up with another programming language with behaves the way you say? – Joachim Otahal Mar 20 '22 at 16:51
  • 1
    If `$snapshot` is an object and you wrap it in double quotes you're stringifying that object, you would need subexpression operator to expand it in double quotes. This is expanding the ID property of the object `"$($SnapShot.ID)"` yet is not needed in this case – Santiago Squarzon Mar 20 '22 at 17:00
  • @SantiagoSquarzon Ah, you referred to THAT line of my posting. It was not clear which you were referring to. I tried that as well, and tried the actual string com $SnapShot.ID, including the quoted variantes, and it did not work. js2010 had the right hint, – Joachim Otahal Mar 20 '22 at 18:08
  • @Santiago's comment was just an _aside_, pointing to an _unrelated_ problem in your question, with your attempt at string interpolation; to summarize: In order to embed the value of `$SnapShot.ID` inside `"..."`, you must enclosed it in `$(...)`, i.e. `"$($SnapShot.ID)"`; however, to use the value _by itself_, you don't need `"..."` at all (unless you must stringify it) - just use `$SnapShot.ID`. See [this answer](https://stackoverflow.com/a/40445998/45375) for an overview of PowerShell's expandable strings (string interpolation). – mklement0 Mar 20 '22 at 18:12

1 Answers1

3

Probably pipe to remove-ciminstance like with win32_userprofile. .delete() is a made up method by get-wmiobject that does something similar.

get-ciminstance win32_userprofile | ? localpath -match js2010 |remove-ciminstance
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    This is not Win32_ShadowCopy, but the hint worked. Just pipe a single snapshot from `Get-CimInstance Win32_ShadowCopy` into `Remove-CimInstance` works! Mutiple objects work too! Just tested with `(Get-CimInstance Win32_ShadowCopy)[3..5] | Remove-CimInstance` and those snapshots are indeed cleaned up! – Joachim Otahal Mar 20 '22 at 18:09
  • `Remove-CimInstance -InputObject (Get-CimInstance Win32_ShadowCopy)[3..5]` works as well, saves me an extra pipe. I try to avoid pipes since they are slow. – Joachim Otahal Mar 20 '22 at 18:33
  • The result of this final missing piece can be found here: A more efficient way for windows shadowcopy jobs, or having such a job at all since Microsoft remove the config-UI from Windows 10 and Windows 11. https://github.com/Joachim-Otahal/Windows-ShadowCopy – Joachim Otahal Mar 25 '22 at 12:31