Search code examples
vb.netpowershell-2.0

In vb.net I need to create a shadow copy, trying to use power shell


I have a VB.net backup program. I need to create a shadow copy and it would seem that the easiest way is to just use power shell to create it. If I open a command prompt and go to power shell and type in: (Get-WmiObject -list win32_shadowcopy).Create("C:\","ClientAccessible") it works just fine. But I want to add that to process.start or shell and can't seem to figure out the correct context. I found the following example online, but it gives me errors.

powershell invoke-command -scr {(Get-WmiObject -list win32_shadowcopy).Create("C:\","ClientAccessible")}

The string starting: At line:1 char:70 + invoke-command -scr {(Get-WmiObject -list win32_shadowcopy).Create(C: <<<< ", ClientAccessible)} is missing the terminator: ". At line:1 char:90 + invoke-command -scr {(Get-WmiObject -list win32_shadowcopy).Create(C:",Client Accessible)} <<<< + CategoryInfo : ParserError: (,ClientAccessible)}:String) [], Pa rentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Any idea why I get this error using power shell? I am also open to other solutions for create a shadow with VB.net, as long as it is simple. NOTE: must work in Vista through 8.1


Solution

  • Since there was no response, I looked in to other ways of doing this, found this in vbscript and so I converted it to vb.net. Make sure to set it to compile as any cpu in vs2010.

       Const VOLUME = "C:\"
        Const CONTEXT = "ClientAccessible"
    
        Dim strShadowID
        Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Dim objShadowStorage = objWMIService.Get("Win32_ShadowCopy")
        Dim errResult = objShadowStorage.Create(VOLUME, CONTEXT, strShadowID)
    
        If errResult = 0 Then
            Dim objWMI = GetObject("winmgmts://./root\cimv2")
            Dim objInstances = objWMI.InstancesOf("Win32_ShadowCopy")
    
            For Each objInstance In objInstances
                With objInstance
                    If .ID = strShadowID Then
                        Console.WriteLine(.DeviceObject)
                    End If
                End With
                On Error GoTo 0
            Next
        End If