Reputation: 375
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
Upvotes: 1
Views: 458
Reputation: 1
This is a working VB example using Visual Studio 2013 Pro .NET Framework 4.5 and AlphaVSS. To add AlphaVSS to your project, in Visual Studio click View
, Other Windows
, Package Manager Console
. Go to the Package Manager Console and run this command:
NuGet\Install-Package AlphaVSS -Version 2.0.3
Add this to the beginning of your modules or classes that will need to talk to VSS.
Imports Alphaleonis.Win32
This sub creates a shadow copy of an entire volume and mounts it as a file share. Files can then be copied from the temporary file share to a backup medium. The shadow copy is then deleted.
Imports Alphaleonis.Win32
Public Shared Sub CopyFromShadow()
Dim SnapshotGUID As New Guid
Dim SnapSetGUID As New Guid
Dim ExposedShare As String = "VBSHADOW" ' The temporary file share to which the snapshot will be mounted.
Dim vssFactory = Vss.VssFactoryProvider.Default.GetVssFactory() ' Create vssFactory interface.
Dim vssBackupComponents = vssFactory.CreateVssBackupComponents ' Create backup components.
vssBackupComponents.InitializeForBackup(Nothing) ' Must be called before SetBackupState.
vssBackupComponents.SetBackupState(False, True, Vss.VssBackupType.Full, False) ' ComponentMode, Bootable, BackupType, PartialSupport
vssBackupComponents.GatherWriterMetadata() ' Initiates communication with VSS.
vssBackupComponents.GatherWriterStatus() ' Must be called before StartSnapshotSet.
SnapSetGUID = vssBackupComponents.StartSnapshotSet ' Creates a new SnapshotSet, returns the GUID of the new set.
SnapshotGUID = vssBackupComponents.AddToSnapshotSet("C:\") ' Add the volume containing your files to be backed up.
vssBackupComponents.PrepareForBackup() ' PrepareForBackup needs to be called before DoSnapshotSet.
vssBackupComponents.DoSnapshotSet() ' Tells VSS to create the snapshot (set).
vssBackupComponents.GatherWriterStatus() ' Must be called before ExposeSnapshot.
' Exposes a snapshot of the entire volume as a file share.
Dim ExposeResult As String = vssBackupComponents.ExposeSnapshot(SnapshotGUID, "\", Vss.VssVolumeSnapshotAttributes.ExposedRemotely, ExposedShare)
' At this point, all data on the volume should be accessible via the exposed share. [\\localhost\VBSHADOW]
' Copy all of your files to your backup medium here.
' ---
'
'IO.File.Copy("\\localhost\VBSHADOW\Users\xyz\Documents\Outlook.pst", "D:\backup\Outlook.pst")
'
' ---
' When finished with the shadow, it must be deleted.
vssBackupComponents.GatherWriterStatus() ' Must be called before BackupComplete.
vssBackupComponents.BackupComplete() ' Signals to VSS that we're about to delete the snapshot.
vssBackupComponents.DeleteSnapshotSet(SnapSetGUID, True) ' Delete the snapshot (set). [SetGUID, ForceDelete]
End Sub
It took me 3 days of reading the VSS documentation and googling and experimenting to figure this out. VSS is much, much more complex and capable than what we're doing here. This is one quick and simple way to backup a file that is in use.
Upvotes: 0
Reputation: 375
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
Upvotes: 2