Remote WMI connection

前端 未结 9 1824
心在旅途
心在旅途 2020-12-31 14:46

I want to connect to remote PC running Windows 7, from another PC using ManagementScope on a local network. On remote PC I\'ve created a new user account \"Samuel\" withou

9条回答
  •  鱼传尺愫
    2020-12-31 14:59

    Solution using "net view \\servername"

    I know it's not very desirable to use a console command and do some string-gymnastic on the output, but on the other hand it does work and it's not very desirable either, at least for me, to have to mess around with the DCOM default settings to to get the WMI way to work (at least on Win7s).

    Has been tested on Win7 and XP clients and MS- and linux server.

    Function GetShares(ServerName As String) As List(Of String)
        Try
            Dim P As New Process
            Dim Read As Boolean = False
            Dim Str As String
            Dim Shares As New List(Of String)
    
            With P.StartInfo
                .FileName = "net"
                .Arguments = "view " & ServerName
                .RedirectStandardOutput = True
                .RedirectStandardError = True
                .CreateNoWindow = True
                .UseShellExecute = False
            End With
            P.Start()
            P.WaitForExit()
    
            If P.ExitCode <> 0 Then
                MsgBox(P.StandardError.ReadToEnd, MsgBoxStyle.OkOnly, "Error")
            Else
                Do Until P.StandardOutput.EndOfStream = True
                    If Read = True Then
                        Str = P.StandardOutput.ReadLine
                        If Str = "The command completed successfully." Then Exit Do
                        Str = Strings.RTrim(Str) 'Removes any trailing spaces
                        Str = Strings.Mid(Str, 1, Strings.InStrRev(Str, " ")) 'remove Type
                        Str = Strings.RTrim(Str) ''Removes any trailing spaces
                        Shares.Add(Str)
                    Else
                        If Strings.Left(P.StandardOutput.ReadLine, 10) = "----------" Then Read = True
                    End If
                Loop
            End If
            Return Shares
        Catch ex As Exception
            MsgBox("Error in """ & System.Reflection.MethodInfo.GetCurrentMethod.Name & """: " & vbCr & ex.Message, MsgBoxStyle.OkOnly, "Runtime error")
            Debug.Print("--------------------------" & vbCr & "Error: " & ex.Message & vbCr & ex.StackTrace)
            Return Nothing
        End Try
    End Function
    

提交回复
热议问题