2014-11-24 125 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(); 
     } 

    }