简体   繁体   English

VB.Net-使用带有“以管理员身份运行”的进程来执行vbscript

[英]VB.Net - execute vbscript using process with 'run as administrator'

I have a vbscript file that I want to run in my vb.net application. 我有一个要在我的vb.net应用程序中运行的vbscript文件。 In the application, the script 'must' use a process. 在应用程序中,脚本“必须”使用一个进程。 The reason is that it's actually being called from a windows process. 原因是它实际上是从Windows进程中调用的。 In order for me to execute the vbscript manually, I have to right-click on the shortcut and select 'run as administrator'. 为了让我手动执行vbscript,我必须右键单击快捷方式,然后选择“以管理员身份运行”。

How can I emulate this using vb.net? 我如何使用vb.net进行仿真? Currently the execution works because I tested it with it only creating a text file. 当前执行有效,因为我仅通过创建文本文件对其进行了测试。 Also, I want to assume that the user is in the administrators group and don't want them to have to login each time because it will execute every minute. 另外,我想假设用户在管理员组中,并且不希望他们每次都必须登录,因为它将每分钟执行一次。

My code: 我的代码:

Dim foo As New System.Diagnostics.Process
foo.StartInfo.WorkingDirectory = "c:\"
foo.StartInfo.RedirectStandardOutput = True
foo.StartInfo.FileName = "cmd.exe"
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"
foo.StartInfo.UseShellExecute = False
foo.StartInfo.CreateNoWindow = True
foo.Start()
foo.WaitForExit()
foo.Dispose()

Thanks. 谢谢。

The class ProcessStartInfo has two properties that can be used to define the user name that will run your script ProcessStartInfo类具有两个属性,可用于定义将运行脚本的用户名。

ProcessStartInfo.UserName ProcessStartInfo.UserName
ProcessStartInfo.Password ProcessStartInfo.Password

Please note as from MSDN: The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\\system32. 请注意,从MSDN起: The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\\system32. The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\\system32.

The Password property is of type SecureString. 密码属性的类型为SecureString。 This class need a special initialization code like this: 此类需要一个特殊的初始化代码,如下所示:

  ' Of course doing this will render the secure string totally 'insecure'
  Dim pass As String = "Password"
  Dim passString As SecureString = New SecureString()
  For Each c As Char In pass
     passString.AppendChar(ch)
  Next   

So your code could be changed in this way 因此您的代码可以通过这种方式进行更改

Dim foo As New System.Diagnostics.Process   
foo.StartInfo.WorkingDirectory = "c:\"   
foo.StartInfo.RedirectStandardOutput = True   
foo.StartInfo.FileName = "cmd.exe"   
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"   
foo.StartInfo.UseShellExecute = False   
foo.StartInfo.CreateNoWindow = True   
foo.StartInfo.UserName = "administrator"
foo.StartInfo.Password = passString
foo.Start()   
foo.WaitForExit()   
foo.Dispose()  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM