title: Need help converting PowerShell to C# code
body: <p>So i have this powershell code:</p>
  
  <pre><code>try{
      $ComputerWMIObject = Get-WmiObject Win32_ComputerSystem -ComputerName "$oldComputerName" -Authentication 6
      if ( $ComputerWMIObject ){
          $result = $ComputerWMIObject.Rename("$newComputerName", $ADUserPassword , $ADUserName )
          switch($result.ReturnValue)
          {
              0 {
                  if ( $Restart.IsChecked ) {
                      Get-WmiObject Win32_OperatingSystem -ComputerName "$oldComputerName" | ForEach-Object {$restart = $_.Win32Shutdown(6)}
                      $ResultText.Text = "Computer $oldComputerName was renamed to $newComputerName and restarted"
                  } else {
                      $ResultText.Text = "Computer $oldComputerName was renamed to $newComputerName restart computer to finish"
                  }
  
              }
              5 { $ResultText.Text = "Computer was not renamed. Please check if you have admin permissions (ReturnCode 5)" }
              default { $ResultText.Text = "ReturnCode $($result.ReturnValue)"}
          }
      }else{
          $ResultText.Text = "Couldn't create WMI Object on $oldComputerName"
      }
  }catch{
      $ResultText.Text = $_
  }
  </code></pre>
  
  <p>I'm trying to convert this to C# and can't find a way to do this. I just don't understand how to create WMI object.</p>
  
  <p>It would be very helpful if you can post and example on how to do this. 
  I've read this <a href="https://stackoverflow.com/questions/6207411/remotely-change-computer-name-for-a-windows-server-2008-machine-using-c">Remotely change computer name for a Windows Server 2008 machine using C#?</a> topic. And it throws an exception may be its because of this line:</p>
  
  <pre><code>Authentication = AuthenticationLevel.PacketPrivacy
  </code></pre>
  
  <p>I'm using System.Net.Security namespace and as it's stated in comment PacketPrivacy exists only there.</p>
  
  <p>Since I can't ask there because I have low rating I've asked again.</p>
  
  <p>Would be grateful if any one can help me.</p>
  
  <p>PS: I know this can be done using NETDOM but I would prefer using WMI object.</p>
  
  <p><strong>ADDED:</strong></p>
  
  <p>I'm trying to use this:</p>
  
  <pre><code>var remoteControlObject = new ManagementPath
  {
      ClassName = "Win32_ComputerSystem",
      Server = oldName,
      Path = oldName + "\\root\\cimv2:Win32_ComputerSystem.Name='" + oldName + "'",
      NamespacePath = "\\\\" + oldName + "\\root\\cimv2"
  };
  
  var conn = new ConnectionOptions
  {
      Authentication = AuthenticationLevel.PacketPrivacy,
      Username = accountWithPermissions.Domain + "\\" + accountWithPermissions.UserName,
      Password = accountWithPermissions.Password
  };
  
  var remoteScope = new ManagementScope(remoteControlObject, conn);
  
  var remoteSystem = new ManagementObject(remoteScope, remoteControlObject, null);
  
  ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters("Rename");
  var methodOptions = new InvokeMethodOptions();
  
  newRemoteSystemName.SetPropertyValue("Name", newName);
  newRemoteSystemName.SetPropertyValue("UserName", accountWithPermissions.UserName);
  newRemoteSystemName.SetPropertyValue("Password", accountWithPermissions.Password);
  
  ManagementBaseObject outParams = remoteSystem.InvokeMethod("Rename", newRemoteSystemName, null);
  </code></pre>
  
  <p>And I get this error <strong>Server RPC is unavailable. (Exception HRESULT: 0x800706BA)</strong> here:</p>
  
  <pre><code>ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters("Rename");
  </code></pre>
  
  <p><strong>ADDED2:</strong> </p>
  
  <p>Ok, I guess I found out what causes an error.</p>
  
  <p>i've changed original <strong>conn</strong> User name from</p>
  
  <pre><code>Username = oldName + "\\" + accountWithPermissions.UserName,
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Username = accountWithPermissions.Domain + "\\" + accountWithPermissions.UserName,
  </code></pre>
  
  <p>and error happens, if I use old code I get ACCESS_IS_DENIED and that's correct because that user doesn't have rights.</p>
  
  <p>So what's wrong if I use Domain\User may be I should change <strong>NamespacePath</strong> in <strong>remoteControlObject</strong> to be able to work with domain user authentication? </p>