2014-11-24 168 views
-1

所以我有這樣的PowerShell代碼:需要幫助轉換PowerShell將C#代碼

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 = $_ 
} 

我試圖將其轉換爲C#,不能找到一個方法來做到這一點。我只是不明白如何創建WMI對象。

如果您可以發佈並舉例說明如何操作,那將會非常有幫助。 我已閱讀此Remotely change computer name for a Windows Server 2008 machine using C#?主題。它拋出一個異常,可能是因爲此行的:

Authentication = AuthenticationLevel.PacketPrivacy 

我使用System.Net.Security命名空間,因爲它在評論數據包保密的既定只存在那裏。

由於我不能問那裏,因爲我有低評分我再次問。

如果有人能幫助我,我將不勝感激。

PS:我知道這可以使用NETDOM來完成,但我更喜歡使用WMI對象。

新增:

我試圖用這個:

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); 

而且我得到這個錯誤服務器RPC不可用。 (例外HRESULT:0x800706BA)這裏:

ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters("Rename"); 

ADDED2:

好吧,我想我發現了什麼導致錯誤。

我從

Username = oldName + "\\" + accountWithPermissions.UserName, 

Username = accountWithPermissions.Domain + "\\" + accountWithPermissions.UserName, 

改變原來康恩用戶名和錯誤發生,如果我用舊的代碼我得到ACCESS_IS_DENIED因爲該用戶沒有按」這是正確的沒有權利。

那麼,什麼是錯的,如果我使用域\用戶可能是我應該改變NamespacePathremoteControlObject能夠與域用戶認證工作?

+0

您需要的全部內容將位於[System.Management](http://msdn.microsoft.com/zh-cn/library/System.Management%28v=vs.110%29.aspx)命名空間中。 – arco444 2014-11-24 10:26:26

+0

仍有**服務器RPC不可用。 (異常HRESULT:0x800706BA)**我在這裏得到這個ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters(「Rename」);將添加我試圖在一秒內實現的代碼。 – BolnoYGaD 2014-11-24 10:44:57

+0

我不知道你是如何初始化'ConnectionOptions',似乎沒有遵循這個參考。連接到遠程計算機時,您可能需要使用'ImpersonationLevel.Impersonate'。 – arco444 2014-11-24 11:04:27

回答

0

我無法找到解決此問題的方法,似乎沒有人能夠得到答案。

所以我解決了這個通過使用NETDOM服務器RPC不可用。 (異常HRESULT:0x800706BA)實際上發生任何錯誤我將添加訪問拒絕檢查,以便它不會嘗試NETDOM時發生此情況;

var remoteControlObject = new ManagementPath 
{ 
    ClassName = "Win32_ComputerSystem", 
    Server = oldName, 
    Path = oldName + "\\root\\cimv2:Win32_ComputerSystem.Name='" + oldName + "'", 
    NamespacePath = "\\\\" + oldName + "\\root\\cimv2" 
}; 

string domain = accountWithPermissions.Domain; 
string user = accountWithPermissions.UserName; 

var conn = new ConnectionOptions 
{ 
    Authentication = AuthenticationLevel.PacketPrivacy, 
    Username = domain + "\\" + accountWithPermissions.UserName, 
    Password = accountWithPermissions.Password 
}; 
var remoteScope = new ManagementScope(remoteControlObject, conn); 

var remoteSystem = new ManagementObject(remoteScope, remoteControlObject, null); 
try 
{ 
    ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters("Rename"); 
    newRemoteSystemName.SetPropertyValue("Name", newName); 
    newRemoteSystemName.SetPropertyValue("UserName", accountWithPermissions.UserName); 
    newRemoteSystemName.SetPropertyValue("Password", accountWithPermissions.Password); 
    ManagementBaseObject outParams = remoteSystem.InvokeMethod("Rename", newRemoteSystemName, null); 
} 
catch (Exception e) 
{ 
    this.Res.Inlines.Add(string.Format("Ошибка:\n" + e.Message + "\n")); 
    this.Res.Inlines.Add(string.Format("Пробуем переименовать используя NETDOM\n")); 
    bool restart = false; 
    PowerNETDOM(oldName, newName, accountWithPermissions, restart); 
} 

服務器RPC不可用。(異常HRESULT:0x800706BA)正在發生不是因爲我對腳本的更改。這個錯誤,如果只有少數電腦開啓,因爲我發現它可能發生在很多情況下更多關於這個:Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)。並且改變遠程PC上的設置使我可以直接從該PC上檢查設置。在這種情況下,我可以重命名PC甚至不試圖找出發生了什麼。這就是爲什麼我使用NETDOM,因爲由於某種原因,它沒有問題遠程重命名該PC。

private static void PowerNETDOM(String oldName, String newName, NetworkCredential accountWithPermissions, bool restart) 
    { 
     using (PowerShell PowerShellInstance = PowerShell.Create()) 
     { 
      PowerShellInstance.AddScript 
       (
        "param($Restart,$oldComputerName,$newComputerName,$ADUserPassword,$ADUserName);" + 
        "function ConvertTo-Encoding ([string]$From, [string]$To){" + 
        " Begin{ $encFrom = [System.Text.Encoding]::GetEncoding($from);$encTo = [System.Text.Encoding]::GetEncoding($to); }" + 
        " Process{ $bytes = $encTo.GetBytes($_);$bytes = [System.Text.Encoding]::Convert($encFrom, $encTo, $bytes);$encTo.GetString($bytes)}" + 
        "}" + 
        "$tmp = NETDOM RENAMECOMPUTER $oldComputerName /NewName:$newComputerName /ud:$ADUserName /pd:$ADUserPassword /Force $res_text | ConvertTo-Encoding \"cp866\" \"windows-1251\";" + 
        "$tmp > C:\\Temp\\rename.txt;$tmp;" 
       ); 

      PowerShellInstance.AddParameter("restart", restart); 
      PowerShellInstance.AddParameter("newComputerName", newName); 
      PowerShellInstance.AddParameter("oldComputerName", oldName.ToString()); 
      PowerShellInstance.AddParameter("ADUserPassword", accountWithPermissions.Password); 
      PowerShellInstance.AddParameter("ADUserName", accountWithPermissions.Domain + "\\" + accountWithPermissions.UserName); 

      PowerShellInstance.Invoke(); 
     } 

    }