我正在尝试从我的 VB 程序中运行 cmd.exe 中的影子命令,但由于某种原因它不会运行该命令,我尝试了一些不同的方法,但都没有奏效。我能够将命令保存到批处理文件中,然后成功执行,但我更愿意将参数/命令直接传递给命令提示符并以这种方式执行。基本上,我有一个用户运行我创建的另一个程序来提取 sessionid 和服务器名称(然后我只需将服务器编号从末尾取出)。他们得到一个 4 位数的密码,基本上前两个是 sessionid,后两个是服务器号(我们所有的服务器都被命名为 smdts-(一个数字)所以我只关心他们在什么服务器号上)然后我拿四位数代码并将其插入我的影子管理程序。这是我当前没有的代码工作:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
Dim Prc As Process
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Pinfo = New System.Diagnostics.ProcessStartInfo
Pinfo.FileName = "cmd.exe"
Pinfo.Arguments = fileargs
Pinfo.ErrorDialog = False
Pinfo.UseShellExecute = False
Pinfo.CreateNoWindow = False
Pinfo.WindowStyle = ProcessWindowStyle.Normal
Pinfo.RedirectStandardOutput = False
Pinfo.RedirectStandardInput = True
Pinfo.RedirectStandardError = False
Prc = New Process
Prc.StartInfo = Pinfo
Prc.Start()
End Sub
什么工作(我不想使用的批处理文件):
Public Class Form1
Public pathvar As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Streamwriter As StreamWriter
Streamwriter = File.CreateText(pathvar + "\ShadowBatch.bat")
Streamwriter.WriteLine(fileargs)
Streamwriter.Close()
Shell(pathvar + "\ShadowBatch.bat")
End Sub
任何关于为什么第一个示例不起作用的帮助将不胜感激!谢谢!
汤姆·K