代码之家  ›  专栏  ›  技术社区  ›  Peter

使用备用凭据管理远程服务

  •  1
  • Peter  · 技术社区  · 14 年前

    -- 原岗位

    我正在尝试使用管理(启动/停止)远程计算机上的windows服务 候补

    Dim sc As New ServiceController(ServiceName, ComputerName)
    

    工作代码(基于公认答案构建) --

    我不得不承认,我怀疑它会起作用。。。但下面是代码。我不得不对你建议的代码做一点小改动。每当我尝试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
    
    1 回复  |  直到 12 年前
        1
  •  2
  •   Oleg    14 年前

    要进行远程登录,您应该使用 WNetAddConnection2 (见 http://msdn.microsoft.com/en-us/library/aa385413.aspx )或者 NetUseAdd (见 http://msdn.microsoft.com/en-us/library/aa370645.aspx \\RemoteComputer\IPC$ 作为目标资源。

    更新 基于评论中的问题:关于IPC$sessions的解释可能很长。只是主要信息。

    如果你想在远程计算机上做些什么,首先要做的就是建立一个到远程计算机的经过身份验证的“连接”。这个 网络登录 远程登录 )将在远程计算机上完成,这与本地登录完全不同。网络登录会话保持不变,例如,如果您与 \\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 在Explorer和access文件系统下用户的 Domain\User RemoteComputer\LocalRemoteUser 凭据。断开连接使用

    net use \\RemoteComputer\IPC /d
    

    如果您尝试在远程计算机上启动/停止服务,将尝试建立相同的IPC会话。如果您已经有这样的会话与用户的凭据之一,它将被使用。功能 NetUseAdd公司 可用作“净用途”的替代品。如果您要永久性地使用其他用户的凭据访问远程计算机,则可以使用 CredWrite CredWriteDomainCredentials CredUIPromptForCredentials / CredUIPromptForWindowsCredentials