2013-04-15 41 views
12

Tôi cố gắng để chạy mã PowerShell từ máy tính của tôi để vm trên máy tính của tôi, nhưng tôi cứ bị lỗi này:Đang kết nối đến máy chủ từ xa không sử dụng WinRM từ PowerShell

Connecting to remote server failed with the following error message : The WinRM client cannot process the request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain, then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.

mã của tôi:

string runasUsername = @"\aaa"; 
    string runasPassword = "aaa"; 
    SecureString ssRunasPassword = new SecureString(); 
    foreach (char x in runasPassword) 
     ssRunasPassword.AppendChar(x); 
    PSCredential credentials = new PSCredential(runasUsername, ssRunasPassword); 

    var connInfo = new WSManConnectionInfo(new Uri("http://10.0.5.35/PowerShell"), 
     "http://schemas.microsoft.com/powershell/Microsoft.Exchange",credentials); 
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; 

    var runspace = RunspaceFactory.CreateRunspace(connInfo); 


    var domainName = "domainName.COM"; 
    var password = "ActiveDirectoryPassword1234"; 
    var ssPassword = new SecureString(); 
    foreach (char c in password) 
     ssPassword.AppendChar(c); 


    var command = new Command("New-Mailbox"); 

    command.Parameters.Add("FirstName", firstName); 
    command.Parameters.Add("LastName", lastName); 
    command.Parameters.Add("Password", ssPassword); 
    command.Parameters.Add("ResetPasswordOnNextLogon", false); 
    command.Parameters.Add("OrganizationalUnit", "NeumontStudents"); 

    runspace.Open(); <--//error here 
    var pipeline = runspace.CreatePipeline(); 
    pipeline.Commands.Add(command); 


    var results = pipeline.Invoke(); 

    runspace.Dispose(); 

Tôi đang thiếu gì?

+0

Bạn đã thử kiểm tra những thứ được đề cập trong thông báo lỗi chưa? –

Trả lời

20

Nếu khách hàng và các máy tính từ xa không trên cùng một miền, bạn có một trong hai lựa chọn:

  • sử dụng HTTPS là giao thức vận chuyển
  • thêm máy từ xa vào danh sách các host đáng tin cậy trên máy khách

để configure WinRM sử dụng HTTPS, mở ra một giao diện điều khiển PowerShell as administrator trên cả hai máy và chạy:

winrm quickconfig -transport:https 

và mở cổng 5986 trên tường lửa:

netsh firewall add portopening TCP 5986 "WinRM over HTTPS" 

Ngoài ra, bạn có thể thêm các máy từ xa là đáng tin cậy máy chủ trên máy khách bằng cách chạy:

winrm set winrm/config/client '@{TrustedHosts="10.0.5.35"}' 
+0

yup nó là, tôi nhận được điều này: "WinRM đã được thiết lập để nhận được yêu cầu trên máy này. WinRM đã được thiết lập để quản lý từ xa trên máy này." – woolford

+0

@woolford là khách hàng và máy chủ trên cùng một tên miền? –

+0

Enrico Campidoglio no sir – woolford

1

bạn đã bật winrm trên cả hai máy? thử chạy winrm quickconfig trên mỗi máy để đảm bảo kết nối từ xa được bật.

+0

yup nó là, tôi nhận được điều này: "WinRM đã được thiết lập để nhận được yêu cầu trên máy này. WinRM đã được thiết lập để quản lý từ xa trên máy này." – woolford

+0

Thử sử dụng xác thực CredSSP. Xem các bước trong câu trả lời của tôi tại đây: http://stackoverflow.com/questions/15336336/running-batch-file-on-remote-computers-using-powershell-2-0/15336790#15336790 –

+0

CredSSP được sử dụng để cho phép máy từ xa truyền thông tin xác thực của khách hàng khi kết nối với các dịch vụ khác (ví dụ: [second-hop] (http://blogs.technet.com/b/heyscriptingguy/archive/2012/11/14/enable-powershell- quot-second-hop-quot-chức năng-với-credssp.aspx)). Vấn đề trong trường hợp này là máy khách và máy chủ không nằm trên cùng một miền. –

Các vấn đề liên quan