Private Declare Auto Function LogonUser Lib "advapi32.dll" (ByVal un As String, ByVal domain As String, ByVal pw As String, ByVal LogonType As Integer, ByVal LogonProvider As Integer, ByRef Token As IntPtr) As Boolean
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Boolean
Dim tokenHandle As New IntPtr(0)
Dim newId As WindowsIdentity
Sub RunImpersonation()
Console.WriteLine("> RUNNING...")
Try
If LogonUser("user", "domain", "pass", 2, 0, tokenHandle) Then
newId = New WindowsIdentity(tokenHandle)
' Using impersonatedUser As WindowsImpersonationContext = newId.Impersonate()
newId.Impersonate()
'
'
' < RUN YOUR IMPERSONATION STUFF HERE
'
'
' End Using
' CloseHandle(tokenHandle)
Else
Console.WriteLine("!! LOG ON FAILED")
End If
Catch ex As UnauthorizedAccessException
Console.WriteLine("!! UNAUTHORIZED: " + ex.Message())
Catch ex As Exception
Console.WriteLine("!! EXCEPTION: " + ex.Message())
End Try
Console.WriteLine()
End Sub
CloseHandle(tokenHandle)
'http://www.vbforums.com/showthread.php?606500-RESOLVED-VB-NET-2008-Remote-Process-Starting
Private Sub RunRemoteProcess(HostName As String)
Dim sCmd As String = "C:\CrouseScripts\DelProf2.exe /q"
' add a reference to System.Management in Solution Explorer
Dim wmi, objWMIService As ManagementClass
Dim wmi_in, wmi_out As ManagementBaseObject
Dim retValue As Integer
Dim pid As Integer
Try
Console.WriteLine(WindowsIdentity.GetCurrent.User.ToString)
wmi = New ManagementClass("\\" & HostName & "\root\cimv2:Win32_Process")
' get the parameters to the Create method
wmi_in = wmi.GetMethodParameters("Create")
' fill in the command line plus any command-line arguments
' NOTE: the command can NOT be on a network resource!
wmi_in("CommandLine") = sCmd
' do it!
wmi_out = wmi.InvokeMethod("Create", wmi_in, Nothing)
' get the return code. This not the return code of the
' application... it's a return code for the WMI method
retValue = Convert.ToInt32(wmi_out("returnValue"))
Select Case retValue
Case 0
' success!
Case 2
Throw New ApplicationException("Access denied")
Case 3
Throw New ApplicationException("Insufficient privilege")
Case 8
Throw New ApplicationException("Unknown failure")
Case 9
Throw New ApplicationException("Path not found")
Case 21
Throw New ApplicationException("Invalid parameter")
Case Else
Throw New ApplicationException("Unknown return code " & retValue)
End Select
pid = Convert.ToInt32(wmi_out("processid"))
Console.WriteLine("PID: " & pid.ToString)
Catch ex As Exception
Console.WriteLine(HostName & ": Can't create the process. " & ex.Message)
End Try
'Int pid = (Int())managementBaseObject["processId"]
Dim remPrc As Process
Try
While Process.GetProcessById(pid, HostName).Id
Console.Write(".")
Threading.Thread.Sleep(500)
End While
Catch
Console.Write("!")
End Try
Console.WriteLine("*")
'remPrc.WaitForExit()
End Sub
Dim pProcess As New Process
pProcess.StartInfo.CreateNoWindow = True
'pProcess.StartInfo.UserName = ""
'pProcess.StartInfo.Password = ToSecureString("")
pProcess.StartInfo.UseShellExecute = False
'pProcess.StartInfo.Domain = ""
pProcess.StartInfo.FileName = "query "
pProcess.StartInfo.Arguments = " user /server:" & servername
pProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
pProcess.StartInfo.RedirectStandardOutput = True
pProcess.StartInfo.RedirectStandardError = True
Try
TextBox1.AppendText("Query: " & servername & " " & Now & vbCrLf)
pProcess.Start()
If Not pProcess.WaitForExit(1000 * 60) Then
TextBox1.AppendText("60 second timeout on : " & servername & " " & Now & " Aborting." & vbCrLf)
pProcess.Kill()
'Return
Else
Dim oCreditCard As New CreditCardForm
oCreditCard.TopLevel = False