Click to See Complete Forum and Search --> : Help with WMI Win32_ShadowCopy in C#


Michael.Net
06-04-2007, 03:35 PM
I have tried the WMI Code Creator to get he correct syntax for the Win32_ShadowCopy Create Method but it generates only partially usable code. I have managed to get it where I can actually get the Method, but when I invoke the Create Method, I get a ManagementException: Invalid Method Parameter(s) error. All I am trying to do is create a shadow copy via code on a remote machine.

Any help would be most appreciated.


private bool createShadow()
{
try {
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "Administrator";
connection.Password = "xxxxxxxxx";
connection.Authority = "ntlmdomain:fftechnologies";
connection.Impersonation = ImpersonationLevel.Impersonate;


ManagementScope scope = new ManagementScope(
"\\\\fftnas01.fftechnologies.int\\root\\CIMV2", connection);
scope.Connect();

ManagementClass mgmtClass = new ManagementClass(scope, new ManagementPath("Win32_ShadowCopy"), null);
ManagementBaseObject inParams = mgmtClass.GetMethodParameters("Create");
int numItems = inParams.Properties.Count;

IEnumerator eProps = inParams.Properties.GetEnumerator();
inParams.SetPropertyValue("Volume", "S:");
inParams.SetPropertyValue("Context", "ClientAccessible");
for(int i=0; i<numItems;i++)
{
eProps.MoveNext();
PropertyData pData = (PropertyData)eProps.Current;
Console.WriteLine("Name: " + pData.Name);
Console.WriteLine("Value: " + pData.Value);
}

ManagementBaseObject outParams =
mgmtClass.InvokeMethod("Create", inParams, null);

// List outParams
txtMsg.Text = "ReturnValue: " + outParams["ReturnValue"] + " ShadowID: " + outParams["ShadowID"];
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
Console.WriteLine("ShadowID: " + outParams["ShadowID"]);

}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
MessageBox.Show(err.StackTrace);
}
catch(System.UnauthorizedAccessException unauthorizedErr)
{
MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}
return true;

}