可以转载,转载请著名作者和出处,谢谢,特别鄙视转载后扣上自己名字的哥们
使用wmi的方式实现windows机器的一些简单的远程管理
软件用户的登入使用存储过程验证登入用户
'sql里的存储过程
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description:
-- =============================================
ALTER PROCEDURE [dbo].[uxp_CheckUser]
@User nvarchar(50),
@Pwd nvarchar(50),
@Power int output
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
Declare @Count int
Select @Count = Count(*) from UserTable
Where UserID = @User
If @Count = 1
Begin
Select @Count = Count(*) from UserTable
Where UserID = @User and Pwd = @Pwd
If @Count = 1
Begin
Select @Power = Powers from UserTable
Where UserID = @User and Pwd = @Pwd
End
Else
Set @Power = 101
End
Else
Set @Power = 100
return @Power
END
'VS里使用存储过程
Dim myConn As SqlConnection
Dim myComm As SqlCommand
myConn = New SqlConnection("server=ip;database=tables;uid=sqluser;pwd=sqlpasswd")
myConn.Open()
myComm = New SqlCommand()
myComm.Connection = myConn
myComm.CommandType = CommandType.StoredProcedure
myComm.CommandText = "uxp_CheckUser"
myComm.Parameters.Add("@User", SqlDbType.NVarChar, 100).Value = TextBox1.Text
myComm.Parameters("@User").Direction = ParameterDirection.Input
myComm.Parameters.Add("@Pwd", SqlDbType.NVarChar, 10).Value = TextBox2.Text
myComm.Parameters("@Pwd").Direction = ParameterDirection.Input
myComm.Parameters.Add("@Power", SqlDbType.Int, 4).Value = 0
myComm.Parameters("@Power").Direction = ParameterDirection.Output
myComm.ExecuteNonQuery()
Dim rtn As Integer
rtn = myComm.Parameters(2).Value()
If (rtn = 0 Or rtn = 1) Then
Dim frm As New Form4()
frm._UserName = TextBox2.Text
frm._Power = rtn
frm.Show()
Me.Hide()
ElseIf (rtn = 100) Then
MsgBox("User not found")
Else
MsgBox("Password error")
End If
Catch ex As Exception
MsgBox(ex.Message)
myConn.Close()
'wmi连接子程序
Dim options As New ConnectionOptions
options.Username = strUser
options.Password = strPass
options.Authority = "ntlmdomain:" + strdomain + ""
If strdomain Is Nothing Then
_scope = New ManagementScope("\\" & strIP & "\root\cimv2", options)
_scope.Connect()
Else
options.Authority = "ntlmdomain:" + strdomain + ""
_scope = New ManagementScope("\\" & strIP & "\root\cimv2", options)
_scope.Connect()
End If
'wmi命令管道函数函数
Dim query As New ObjectQuery(strSql)
Dim Searcher As New ManagementObjectSearcher(_scope, query)
Dim ReturnCollection As ManagementObjectCollection = Searcher.Get()
Return ReturnCollection
'远程创建一个程序进程
Dim o As New ObjectGetOptions()
Dim path As ManagementPath = New ManagementPath("Win32_Process")
Dim processClass As ManagementClass = New ManagementClass(_scope, path, o)
Dim inParams As ManagementBaseObject = processClass.GetMethodParameters("Create")
Dim a As ManagementObject
Dim b As String
b = Nothing
inParams("CommandLine") = 路径 & 程序名称
Dim outparams As ManagementBaseObject = processClass.InvokeMethod("Create", inParams, Nothing)
For Each a In wmicommand("select * from win32_process where caption = '" & 程序名称 & "'")
b = a("Description")
Next
MsgBox("远程启动" & b & "成功")
'主机的硬件信息
Dim mb As Long
ListBox2.Items.Clear()
Dim Operation As ManagementObject
For Each Operation In wmicommand("select * from Win32_OperatingSystem")
ListBox2.Items.Add("操作系统:" & Operation("Caption"))
ListBox2.Items.Add("补丁集:" & Operation("CSDversion"))
ListBox2.Items.Add("主机名称:" & Operation("CSName"))
Next
ListBox2.Items.Add("")
ListBox2.Items.Add("")
mb = 1000000000
For Each Operation In wmicommand("select * from Win32_DiskDrive")
ListBox2.Items.Add("磁盘ID:" & Operation("Model").ToString)
ListBox2.Items.Add("接口类型:" & Operation("interfacetype").ToString)
ListBox2.Items.Add("磁盘容量:" & Convert.ToInt64(Operation("Size") / mb) & " GB")
Next
mb = 1073741824
For Each Operation In wmicommand("select * from Win32_LogicalDisk where DriveType=3")
ListBox2.Items.Add("磁盘说明:" & Operation("Description") + " 盘符:" & Operation("Name") + " 剩余空间:" & Convert.ToInt64(Operation("FreeSpace")) \ mb & " GB" + " 总容量:" & Convert.ToInt64(Operation("Size") \ mb) & "GB")
Next
ListBox2.Items.Add("")
ListBox2.Items.Add("")
mb = 1048576
For Each Operation In wmicommand("select * from Win32_PhysicalMemory")
ListBox2.Items.Add("接口位置:" & Operation("devicelocator").ToString & " 单条容量:" & Convert.ToUInt64(Operation("capacity") / mb) & "MB" & " 总线频率:" & Convert.ToInt64(Operation("speed") / mb))
Next
mb = 1024
Dim percent As Double
For Each Operation In wmicommand("Select * from Win32_OperatingSystem")
ListBox2.Items.Add("物理内存:" & Convert.ToUInt64(Operation("TotalVisibleMemorySize") / mb) & "MB" + _
" 可用内存:" & Convert.ToUInt64(Operation("FreePhysicalMemory") / mb) & "MB" + _
" 已用内存:" & Convert.ToInt64(Operation("TotalVisibleMemorySize") - Operation("FreePhysicalMemory")) \ mb & "MB")
percent = Convert.ToInt64(Operation("TotalVisibleMemorySize") - Operation("FreePhysicalMemory")) / Convert.ToUInt64(Operation("TotalVisibleMemorySize")) * 100
ListBox2.Items.Add("内存使用率:" & Math.Round(percent, 2).ToString() & "%")
Next
ListBox2.Items.Add("")
ListBox2.Items.Add("")
For Each Operation In wmicommand("Select * from Win32_NetworkAdapterConfiguration")
If (Operation("IPEnabled") = True) Then
If (Operation("IPAddress") Is Nothing) Then
ListBox2.Items.Add("")
ListBox2.Items.Add("IP:" & "")
ListBox2.Items.Add("MAC:" & Operation("MACAddress"))
Else
ListBox2.Items.Add("")
ListBox2.Items.Add("IP:" & Operation("IPAddress")(0))
ListBox2.Items.Add("MAC:" & Operation("MACAddress"))
End If
End If
Next
ListBox2.Items.Add("")
ListBox2.Items.Add("")
For Each Operation In wmicommand("Select * from Win32_Processor")
ListBox2.Items.Add("")
ListBox2.Items.Add("CPU ID:" & Operation("DeviceID").ToString)
ListBox2.Items.Add("CPU类型:" & Operation("Name").ToString)
ListBox2.Items.Add("CPU频率:" & Operation("MaxClockSpeed").ToString)
ListBox2.Items.Add("CPU使用率:" & Operation("LoadPercentage").ToString)
ListBox2.Items.Add("")
Next
'调用远程主机的dos命令显示在listbox中子程序
Dim p As New Process
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.StandardInput.WriteLine(Doscomm)
p.StandardInput.WriteLine("exit")
Dim strrst As String
Do
strrst = p.StandardOutput.ReadLine()
If Not (strrst Is Nothing) Then
ListBox4.Items.Add(strrst)
End If
Loop While Not (strrst Is Nothing)
'执行远程主机上的组策略本地结果集并显示在本机的listbox上(我也实在想不出什么好办法实现这个功能,如果你有什么好办法请联系我,谢谢)
ListBox4.Items.Clear()
Dim o As New ObjectGetOptions()
Dim path As ManagementPath = New ManagementPath("Win32_Process")
Dim processClass As ManagementClass = New ManagementClass(_scope, path, o)
Dim inParams As ManagementBaseObject = processClass.GetMethodParameters("Create")
Try
inParams("CommandLine") = "cmd.exe /c Gpresult /v > c:/Gpresult.txt"
Dim outparams As ManagementBaseObject = processClass.InvokeMethod("Create", inParams, Nothing)
System.Threading.Thread.Sleep(40000)
If WmiDomain = Nothing Then
Shell("net use \\" & TextBox10.Text & "\ipc$ """ & WmiPwd & """ /user:""" & WmiUser & """", AppWinStyle.MinimizedFocus, True)
Else
Shell("net use \\" & TextBox10.Text & "\ipc$ """ & WmiPwd & """ /user:""" & WmiDomain & "\" & WmiUser & """", AppWinStyle.MinimizedFocus, True)
End If
Dim sr As New StreamReader("\\" & TextBox10.Text & "\c$\Gpresult.txt", System.Text.Encoding.GetEncoding("GB2312"))
Dim s As String
s = ""
Dim arrText As New ArrayList()
Do
s = sr.ReadLine()
If Not s Is Nothing Then
arrText.Add(s)
End If
Loop Until s Is Nothing
sr.Close()
For Each s In arrText
ListBox4.Items.Add(s)
Next
inParams("CommandLine") = "del c:\Gpresult.txt /f"
outparams = processClass.InvokeMethod("Create", inParams, Nothing)
Shell("net use \\" & TextBox10.Text & "\ipc$ /delete")
Catch ex As Exception
MsgBox(ex.Message)
End Try