(BUG) VssBadStateException thrown, but why? (solved)

Dec 12, 2009 at 11:13 AM

Hi all,

I am using AlphaVss with Powershell 2 on Windows 7.  I am trying to write a cmdlet (a script) to create a non-persistent, read-only snapshot of volumes for backup purposes. 

I can load the assemblies OK, and so (for example) $oVSSImpl.IsVolumeSnapshotted("C:\") returns True, but I cannot call $oVSS.SetBackupState(...) without receiving a VssBadStateException (and I have no idea why).  The code is below, and I would appreciate any advice as to where I am missing the plot...

 

# ======================================================================================================================
# ======================================================================================================================
	Write-Debug ("The following number should be 0: {0} (i.e. no AlphaVSS assemblies loaded)." `
		-f @([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {($_.Location -like "*VSS*")}).Count)
	
# DB: Load the platform-independant DLL
	Add-Type -path "AlphaVSS.Common.dll" 

	Write-Debug ("The following number should be 1: {0} (i.e. one AlphaVSS assemblies loaded)." `
		-f @([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {($_.Location -like "*VSS*")}).Count) 

# DB: Some things we can do now...
	$foo = [Alphaleonis.Win32.Vss.VssUtils]::GetPlatformSpecificAssemblyShortName()    # The ()s are REQUIRED

	Write-Debug "Assembly ShortName: '$foo'" 

# DB: More things we can do now...
	$foo = [Alphaleonis.Win32.Vss.OperatingSystemInfo]::OSVersionName 
	$bar = [Alphaleonis.Win32.Vss.OperatingSystemInfo]::ProcessorArchitecture

	Write-Debug "OS Version: '$foo', Architecture: '$bar'" 


# DB: Now, load the platform-dependant DLL
	$oVSSImpl = [Alphaleonis.Win32.Vss.VssUtils]::LoadImplementation()          # The () are REQUIRED
#	$oVSSImpl | Get-Member -static

	Write-Debug ("The following number should be 2: {0} (i.e. both AlphaVSS assemblies loaded)." `
		-f @([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {($_.Location -like "*VSS*")}).Count)


# DB: This will test that everything is OK so far...
	trap [System.UnauthorizedAccessException] {    # Must be running as an Admin for this to work
		"Error found: $_"
	} 

	$code = '$oVSSImpl.IsVolumeSnapshotted("C:\")'
	Write-Debug ("$code = " + (Invoke-Expression $code)) -Debug




# ======================================================================================================================
# ======================================================================================================================
# 1.0 Overview of Backup Initialization                     http://msdn.microsoft.com/en-us/library/aa384577(VS.85).aspx
# ======================================================================================================================

# ======================================================================================================================
# 1.1 Creates an IVssBackupComponents interface and initializes it to manage a backup and optionally enable or disable 
# writers on the system (see CreateVssBackupComponents, IVssBackupComponents::InitializeForBackup).


	$oVSS = $oVSSImpl.CreateVssBackupComponents()				# Creates an IVssBackupComponents interface
#	$oVSS | Get-Member | Where-Object {($_.Name -eq "InitializeForBackup")}

	$oVSS::InitializeForBackup									# Initialize the IVssBackupComponents interface
	

# ======================================================================================================================
# 1.2 Optionally set the context for shadow copy operations and optionally query the system about the providers and 
# shadow copies it supports (see IVssBackupComponents::SetContext, IVssBackupComponents::Query).

#	If ([Alphaleonis.Win32.Vss.OperatingSystemInfo]::OSVersionName -eq "WindowsXP") {
		$oVSS::SetContext   # default context is Backup 
#	} else {
#		$oVSS::SetContext([Alphaleonis.Win32.Vss.VssSnapshotContext]::FileShareBackup)
#	}
	
# http://msdn.microsoft.com/en-us/library/aa384613(VS.85).aspx
# Nonpersistent shadow copies (sometimes called auto-release shadow copies) are created with contexts of VSS_CTX_BACKUP 
# or VSS_CTX_FILE_SHARE_BACKUP. Nonpersistent shadow copies can be made for FAT, RAW, and NTFS file systems. Those that 
# do not involve writers in their creation include: VSS_CTX_CLIENT_ACCESSIBLE, VSS_CTX_FILE_SHARE_BACKUP, VSS_CTX_NAS_ROLLBACK


# ======================================================================================================================
# 1.3 The requester can provide additional information on handling backup and restore operations (see 
# IVssBackupComponents::SetBackupState).

	trap [Alphaleonis.Win32.Vss.VssBadStateException] {    # "The VSS object was in an incorrect state for the requested operation."
		"Error found: $_"
	} 	

	$oVSS.SetBackupState($True, $False, [Alphaleonis.Win32.Vss.VssBackupType]::Copy, $False)

The exception occurs on the last line and is captured by the preceeding trap (see below).

 

DEBUG: $oVSSImpl.IsVolumeSnapshotted("C:\") = True

Error found: The VSS object was in an incorrect state for the requested operation.

Exception calling "SetBackupState" with "4" argument(s): "The VSS object was in an incorrect state for the requested operation."
At :line:93 char:21
+ 	$oVSS.SetBackupState <<<< ($True, $False, [Alphaleonis.Win32.Vss.VssBackupType]::Copy, $False)

 

In addition, I also have a problem with $oVSS::SetContext([Alphaleonis.Win32.Vss.VssSnapshotContext]::FileShareBackup, Which throws another exception:

 

Unable to cast object of type 'Alphaleonis.Win32.Vss.VssBackupComponents' to type 'System.Type'.
At :line:162 char:114
+ }; $bE4078E3092DF4dd9A469F3DC0CBB505C = Set-PSBreakpoint -Variable vE4078E3092DF4dd9A469F3DC0CBB505C -Mode Read;.$ <<<< vE4078E3092DF4dd9A469F3DC0CBB505C 

Guys, any help would be appreciated. 

-Dave

 

 

 

Dec 12, 2009 at 1:47 PM

Actually, when I wrote this down, it set me thinking...

I needed to run this statement, but it threw an exception:

$oVSS::InitializeForBackup($Null)

Exception calling "InitializeForBackup" with "1" argument(s): "Invalid XML document. Check the event log for details."

So I used this instead, which seemed to run OK, but it caused, the problem described above.

$oVSS::InitializeForBackup

Exception calling

"The VSS object was in an incorrect state for the requested operation."

Actually it's a known bug with Powershell, and the following code is a work around:

 

$params = @($Null); $oVSS.GetType().GetMethod("InitializeForBackup").Invoke($oVSS, $params)

$params = @($Null); $oVSS.GetType().GetMethod("InitializeForBackup").Invoke($oVSS, $params)

I haven't tested it fully yet, but the following $oVSS.SetBackupState worked OK.

 

-DB

 

 

 

 

Dec 12, 2009 at 1:48 PM

See: https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=307821&SiteID=99&wa=wsignin1.0 

for more info...