2010-07-19 120 views
1

- 原貼 -管理遠程服務

我想管理(啓動/停止),一個窗口服務使用備用憑據的遠程機器上。我知道我可以使用ServiceController的類使用我目前的憑據管理服務:

Dim sc As New ServiceController(ServiceName, ComputerName) 

,但我想用不同的憑據。我正在使用的其他類(DirectoryEntry和System.Management)都支持使用備用憑據...幫助將不勝感激。

- 工作守則(建基於關公認的答案) -

我不得不承認,我很懷疑這是可行的......但低於是代碼。我不得不對你所建議的代碼做一些小改動。每當我嘗試IPC $時,它都會返回一個53結果代碼,即使我確定共享存在。所以在另一個網站的建議我刪除了共享,只是計算機名稱,這工作。

Imports System.Runtime.InteropServices 
Imports System.Net 
Imports System.IO 
Imports System.ServiceProcess 

Module Module1 

    Sub Main() 
     Dim Computername As String = "SomeComputer" 
     'Create connection to remote computer' 
     Using nc As New NetworkConnection("\\" + Computername, New NetworkCredential("Domain\User", "Password")) 
      Dim sc As New ServiceController("Windows Firewall/Internet Connection Sharing (ICS)", Computername) 
      'now we can start/stop/whatever we want here' 
     End Using 
     Console.ReadLine() 
    End Sub 

    Public Class NetworkConnection 
     Implements IDisposable 


     Private _networkName As String 

     Public Sub New(ByVal networkName As String, ByVal credentials As NetworkCredential) 
      _networkName = networkName 

      Dim netResource = New NetResource() With { _ 
      .Scope = ResourceScope.GlobalNetwork, _ 
      .ResourceType = ResourceType.Disk, _ 
      .DisplayType = ResourceDisplaytype.Share, _ 
      .RemoteName = networkName _ 
      } 

      Dim result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0) 

      If result <> 0 Then 
       Throw New IOException("Error connecting to remote share", result) 
      End If 
     End Sub 

     Protected Overrides Sub Finalize() 
      Try 
       Dispose(False) 
      Finally 
       MyBase.Finalize() 
      End Try 
     End Sub 

     Public Sub Dispose() Implements System.IDisposable.Dispose 
      Dispose(True) 
      GC.SuppressFinalize(Me) 
     End Sub 

     Protected Sub Dispose(ByVal disposing As Boolean) 
      WNetCancelConnection2(_networkName, 0, True) 
     End Sub 

     <DllImport("mpr.dll")> _ 
     Private Shared Function WNetAddConnection2(ByVal netResource As NetResource, ByVal password As String, ByVal username As String, ByVal flags As Integer) As Integer 
     End Function 

     <DllImport("mpr.dll")> _ 
     Private Shared Function WNetCancelConnection2(ByVal name As String, ByVal flags As Integer, ByVal force As Boolean) As Integer 
     End Function 
    End Class 

    <StructLayout(LayoutKind.Sequential)> _ 
    Public Class NetResource 
     Public Scope As ResourceScope 
     Public ResourceType As ResourceType 
     Public DisplayType As ResourceDisplaytype 
     Public Usage As Integer 
     Public LocalName As String 
     Public RemoteName As String 
     Public Comment As String 
     Public Provider As String 
    End Class 

    Public Enum ResourceScope As Integer 
     Connected = 1 
     GlobalNetwork 
     Remembered 
     Recent 
     Context 
    End Enum 

    Public Enum ResourceType As Integer 
     Any = 0 
     Disk = 1 
     Print = 2 
     Reserved = 8 
    End Enum 

    Public Enum ResourceDisplaytype As Integer 
     Generic = &H0 
     Domain = &H1 
     Server = &H2 
     Share = &H3 
     File = &H4 
     Group = &H5 
     Network = &H6 
     Root = &H7 
     Shareadmin = &H8 
     Directory = &H9 
     Tree = &HA 
     Ndscontainer = &HB 
    End Enum 
End Module 

回答

2

進行遠程登錄,你應該使用WNetAddConnection2(見http://msdn.microsoft.com/en-us/library/aa385413.aspx)或NetUseAdd(見http://msdn.microsoft.com/en-us/library/aa370645.aspx)API。您可以使用\\RemoteComputer\IPC$作爲目標資源。

更新根據評論的問題:關於IPC $會話的解釋可能很長。只是主要的信息。

如果您想在遠程計算機上執行某些操作,首先要做的就是建立一個到遠程計算機的認證「連接」。遠程計算機上的網絡登錄遠程登錄)將完成,這與其他本地登錄非常相似。網絡登錄會話保持不變,如果您有連接到例如\\RemoteComputer\share1和您計算機上的其他程序嘗試訪問例如\\RemoteComputer\share2,則將使用相同的會話。

您可以使用net.exe來模擬情況。剛開始cmd.exe並鍵入

net use \\RemoteComputer\IPC$ /u:Domain\User password 

net use \\RemoteComputer\IPC$ /u:RemoteComputer\LocalRemoteUser password 

那麼你將不得不到目標計算機的連接。然後,您可以在資源管理器中鍵入\\RemoteComputer\AnyShare,並在用戶的Domain\UserRemoteComputer\LocalRemoteUser憑據下訪問文件系統。斷開連接使用

net use \\RemoteComputer\IPC /d 

如果您嘗試在遠程計算機上啓動/停止服務,將嘗試建立相同的IPC會話。如果您已經與用戶的憑證之一進行了這樣的會話,它將被使用。功能WNetAddConnection2,NetUseAdd可以用作「淨使用」的替換。如果您永久想要使用其他用戶的憑據訪問遠程計算機,則可以使用CredWrite,CredWriteDomainCredentialsCredUIPromptForCredentials/CredUIPromptForWindowsCredentials。 Cred函數對我來說似乎不是最好的辦法。

+0

您能否提供更好的示例/更多信息?像:我將如何使用這些API之一來停止並在名爲「xyz」的遠程計算機上啓動服務? – Peter 2010-07-28 17:10:36

+0

感謝您的幫助。我已經將我的代碼發佈在我的問題帖子中,因爲您的代碼沒有包含任何代碼。 – Peter 2010-07-29 14:17:22