所以我有這樣的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因爲該用戶沒有按」這是正確的沒有權利。
那麼,什麼是錯的,如果我使用域\用戶可能是我應該改變NamespacePath在remoteControlObject能夠與域用戶認證工作?
您需要的全部內容將位於[System.Management](http://msdn.microsoft.com/zh-cn/library/System.Management%28v=vs.110%29.aspx)命名空間中。 – arco444 2014-11-24 10:26:26
仍有**服務器RPC不可用。 (異常HRESULT:0x800706BA)**我在這裏得到這個ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters(「Rename」);將添加我試圖在一秒內實現的代碼。 – BolnoYGaD 2014-11-24 10:44:57
我不知道你是如何初始化'ConnectionOptions',似乎沒有遵循這個參考。連接到遠程計算機時,您可能需要使用'ImpersonationLevel.Impersonate'。 – arco444 2014-11-24 11:04:27