所以我有这样的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