title: Setting Hyper-V snapshot's name programmatically
body: <p>I'm creating an Hyper-V snapshot with a C# program:</p>
  
  <pre><code>    private static bool Snapshot(string vmName, string snapshotName)
      {
          var result = false;
          var scope = new ManagementScope(@"root\virtualization", null);
          var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
  
          var vm = Utility.GetTargetComputer(vmName, scope);
  
          var inParams = virtualSystemService.GetMethodParameters("CreateVirtualSystemSnapshot");
          inParams["SourceSystem"] = vm.Path.Path;
  
          var outParams = virtualSystemService.InvokeMethod("CreateVirtualSystemSnapshot", inParams, null);
  
          if ((UInt32)outParams["ReturnValue"] == ReturnCode.Started)
          {
              if (Utility.JobCompleted(outParams, scope))
              {
                  Console.WriteLine("Snapshot was created successfully.");
                  result = true;
              }
              else
              {
                  Console.WriteLine("Failed to create snapshot VM");
                  result = false;
              }
          }
          else if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
          {
              Console.WriteLine("Snapshot was created successfully.");
              result = true;
          }
          else
          {
              Console.WriteLine("Create virtual system snapshot failed with error {0}", outParams["ReturnValue"]);
              result = false;
          }
  
          inParams.Dispose();
          outParams.Dispose();
          vm.Dispose();
          virtualSystemService.Dispose();
  
          return result;
      }
  </code></pre>
  
  <p>(NOTE: This code was taken from <a href="http://msdn.microsoft.com/en-us/library/cc136785%28v=VS.85%29.aspx" rel="nofollow">MSDN</a>)</p>
  
  <p>Is there a way to set the snapshot name through this WMI call ? Otherwise, does anyone know a working solution to rename a snapshot through WMI call? I already found <a href="http://social.msdn.microsoft.com/Forums/en/servervirtualization/thread/13f4f411-277c-4a8c-989a-55916471b865" rel="nofollow">this thread</a>, but it's some kind of ambiguous and it doesn't provide any solution ...</p>
  
  <p>EDIT: The solution was to rename snapshot after having create it. Here's my function to rename the snapshot using Hans advice:</p>
  
  <p>SOLUTION:</p>
  
  <pre><code>    public static bool RenameSnapshot(string vmName, string snapshotName)
      {
  
          var result = false;
          var scope = new ManagementScope(@"root\virtualization", null);
          var vm = Utility.GetTargetComputer(vmName, scope);
  
          // load snapshot
          var objSnapshot = GetLastVirtualSystemSnapshot(vm);
  
          // rename snapshot
          objSnapshot["ElementName"] = snapshotName;
  
          // save
          var virtualSystemService = Utility.GetServiceObject(scope, "Msvm_VirtualSystemManagementService");
          var inParams = virtualSystemService.GetMethodParameters("ModifyVirtualSystem");
          inParams["ComputerSystem"] = vm.Path.Path;
          inParams["SystemSettingData"] = objSnapshot.GetText(TextFormat.CimDtd20);
          var outParams = virtualSystemService.InvokeMethod("ModifyVirtualSystem", inParams, null);
  
          if ((UInt32)outParams["ReturnValue"] == ReturnCode.Completed)
          {
              result = true;
          }
          else
          {
              result = false;
          }
  
  
  
          inParams.Dispose();
          outParams.Dispose();
          vm.Dispose();
          virtualSystemService.Dispose();
  
  
          return result;
      }
  </code></pre>