将字符串从vb.net传递到cmd

时间:2019-07-18 13:48:11

标签: vb.net cmd

我正在尝试将vb.net中的字符串传递到命令提示符窗口中。最后,我想使用runas并使用在登录应用程序时提供的密码作为密码。基本上,我想运行runas命令,然后在窗口中键入密码(或以某种方式将其提供给cmd)

Public Sub runCmd(ByVal pass As String, ByVal command As String, ByVal arguments As String, ByVal permanent As Boolean)
        Dim p As Process = New Process()
        Dim pi As ProcessStartInfo = New ProcessStartInfo()
        pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
        pi.FileName = "cmd.exe"
        pi.Verb = "runas"
        p.StartInfo = pi
        p.Start()

    End Sub

1 个答案:

答案 0 :(得分:0)

我现在知道您想做什么。将其添加到ProcessStartInfo,其中userNamedomainNamerunCmd() Sub的新参数。

pi.UserName = userName
pi.Domain = domainName
pi.Password = GetSecureString(pass)

,其中GetSecureString()的定义如下。

Public Shared Function GetSecureString(ByVal str As String) As SecureString
     Dim secureString As SecureString = New SecureString
     For Each ch As Char In str
         secureString.AppendChar(ch)
     Next
     secureString.MakeReadOnly
     Return secureString
 End Function