Skip to main content

Runas Program




 I wrote up a little program that is similar to my touch screen runas program just missing the buttons. Here is the source code You will need to create a couple buttons and three text boxes. txtpassword txtusername txtpath and two buttons one for running the program and one for exit.

Requires VB.net Express 2010



Imports System.Security
Imports System.ComponentModel

Public Class frmMain

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
    Function ConvertToSecureString(ByVal str As String)
        Dim password As New SecureString
        For Each c As Char In str.ToCharArray
            password.AppendChar(c)
        Next
        Return password
    End Function

    Private Sub Step1_UsernamesPasswords()

        Dim strUsername As String
        Dim strpassword As SecureString = ConvertToSecureString(txtpassword.Text)
        Dim strCommand As String = txtpath.Text

        ' MsgBox(strCommand)
       
        strUsername = txtusername.Text


        'If username = nothing display error then exit sub
        If strUsername = Nothing Then
            MsgBox("Error: Please enter a username", , "Error!")
            Exit Sub
        End If

        'the username is then set to a lowercase
        strUsername = LCase(strUsername)

        'Removes all spaces from username
        strUsername = strUsername.Replace(" ", "")





        Step2_Runas(strCommand, strUsername, strpassword)
    End Sub



    Private Sub Step2_Runas(strpathSecure As String, strusername As String, strpassword As SecureString)

        ' Process.Start(strpath, strusername, strpassword, ".")
        Dim pinfo = New System.Diagnostics.ProcessStartInfo
        Dim workingFolder = "c:"
        Dim strArguments As String = ""
        Dim intstrpathsecure As Integer = strpathSecure.Count
        Dim checkforspace
        Dim strlast3characters As String = ""
        Dim strchecklast3char As String
        Dim strErrorMsg As String

        'Checks for an argument and set strArguments if found
        For i = 0 To intstrpathsecure - 1
            strlast3characters = strlast3characters & strpathSecure.Chars(i) 'all characters
            checkforspace = strpathSecure.Chars(i) 'char that currently on

            strchecklast3char = Microsoft.VisualBasic.Right(strlast3characters, 4) ' last 4 chars.
            'This detects when "exe " and there is a space then set arguements values and strpathsecure
            If checkforspace = " " And strchecklast3char = "exe " Or checkforspace = " " And strchecklast3char = "bat " Then
                strArguments = Microsoft.VisualBasic.Mid(strpathSecure, i + 2)
                strpathSecure = Microsoft.VisualBasic.Left(strpathSecure, i)
                Exit For
            End If
        Next i

        pinfo.FileName = strpathSecure
        pinfo.WorkingDirectory = workingFolder
        pinfo.LoadUserProfile = True
        pinfo.UseShellExecute = False
        pinfo.UserName = strusername
        pinfo.Password = strpassword
        pinfo.Arguments = strArguments
        pinfo.Domain = "."

        Try


            System.Diagnostics.Process.Start(pinfo)

        Catch ex As Win32Exception
            'Error Message


            strErrorMsg = "Error: " & ex.Message.ToString & vbNewLine & _
                            "___________________________________________________________ " & vbNewLine & vbNewLine & _
                            "File Path: " & strpathSecure & vbNewLine & _
                            "Arguments: " & strArguments & vbNewLine & _
                            "Username: " & pinfo.UserName & vbNewLine & _
                            "WorkingDirectory: " & pinfo.WorkingDirectory & vbNewLine & _
                            "Time Stamp: " & TimeOfDay & " on " & Date.Today
                            






            'Debug.Assert(False, ex.Message)



            MessageBox.Show(strErrorMsg, "Error logging in as administrator", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private Sub cmdRun_Click(sender As System.Object, e As System.EventArgs) Handles cmdRun.Click
        Step1_UsernamesPasswords()

      
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        End
    End Sub
End Class

Comments

Popular posts from this blog

Excel document for pinging list of computers VBA

Here is an excel document I created that will ping a list of nodes in column A and give results in column B. There are much better tools that can be used such as angry ip scanner  http://www.angryip.org/w/Home . I just wrote this as an example Requirements: Tested with Office 2010 Download: Download

Powershell - Com+ Application Recycle

Needed a script to recycle a com+ application nightly and this is what i came up with. This script will write each recycle it does to the event log under application. Run locally or via a scheduled task. #Recycle COM+ Application and write to the event log the status # 1.0 Release # Run script locally # Write to the event log ######################################## #Configurable ######################################## #Com+ ApplicationName $ComPlusLikeAppName = "Put the name of Com+ Application here a like statement is used to eval so you can get away with putting part of it" #EventLog to write to. $eventlog = "Application" #Source for eventlog. $source = "RecycleComObject" #Successful Event ID $SEventID = 0 #Error Event ID $EEventID = 666 #Process that COM+ runs under $process = "dllhost.exe". ######################################## #Clear $CurrentMemory = $null $PRocessID = $null $Commandline = $null $GUID = $null $AppID = $null $Messag

Powershell : Certutil Find Expired Certs on CA server

Wrote this to get certificate expiration information for certificates that expired 5 days ago to ones that expire in 90 days. Wrap an invoke-command around this for remote query. $Before = (get-date).adddays(90).ToString("MM/dd/yyyy") $After = (get-date).AddDays(-5).ToString("MM/dd/yyyy") <# https://blogs.technet.microsoft.com/poshchap/2016/01/01/powershell-and-certutil-exe/ We create a date range with $Before, i.e. certificates expiring before this date, and $After, i.e. certificates expiring after this date. These values are converted into something that certutil can understand - $Restrict. This is then used with the certutil -restrict parameter. #> $Restrict = "NotAfter<=$Before,NotAfter>=$After" $Report = @() $cmd = & certutil.exe -view -restrict $Restrict -out "RequesterName,CommonName,Certificate Expiration Date","Certificate Template" $SplitLines = $cmd.Split("`n`r") $Index = 0 foreach ($line in $Sp