【问题标题】:Best way to run remote VBScript in ASP.net? WMI or PsExec?在 ASP.net 中运行远程 VBScript 的最佳方式? WMI 还是 PsExec?
【发布时间】:2012-07-16 19:35:49
【问题描述】:

我正在做一些研究,以找出最好和最有效的方法。 我将需要在许多窗口服务器/计算机上执行远程脚本(在我们构建它们时)。

我有一个要自动执行此任务的 Web 应用程序,我目前正在使用我的原型来使用 PsExec 执行远程脚本。这需要在系统上安装 PsExec。一位同事建议我应该为此使用 WMI。

我对 WMI 进行了一些研究,但找不到我想要的东西。 我想要么将脚本上传到服务器并执行它并读取结果,要么已经在服务器上拥有脚本并执行它并读取结果。不过我更喜欢第一个选项!

PsExec 和 WMI 哪个更理想?

作为参考,这是我的原型 PsExec 代码。此脚本仅执行一个小脚本来获取 Windows 操作系统和服务包信息。

Protected Sub windowsScript(ByVal COMPUTERNAME As String)
        ' Create an array to store VBScript results
        Dim winVariables(2) As String
        nameLabel.Text = Name.Text
        ' Use PsExec to execute remote scripts
        Dim Proc As New System.Diagnostics.Process
        ' Run PsExec locally
        Proc.StartInfo = New ProcessStartInfo("C:\Windows\psexec.exe")

        ' Pass in following arguments to PsExec
        Proc.StartInfo.Arguments = COMPUTERNAME & " -s cmd /C c:\systemInfo.vbs"
        Proc.StartInfo.RedirectStandardInput = True
        Proc.StartInfo.RedirectStandardOutput = True
        Proc.StartInfo.UseShellExecute = False
        Proc.Start()
        ' Pause for script to run
        System.Threading.Thread.Sleep(1500)
        Proc.Close()

        System.Threading.Thread.Sleep(2500) 'Allows the system a chance to finish with the process.
        Dim filePath As String = COMPUTERNAME & "\TTO\somefile.txt"
        'Download file created by script on Remote system to local system
        My.Computer.Network.DownloadFile(filePath, "C:\somefile.txt")
        System.Threading.Thread.Sleep(1000) ' Pause so file gets downloaded
        ''Import data from text file into variables
        textRead("C:\somefile.txt", winVariables)
        WindowsOSLbl.Text = winVariables(0).ToString()
        SvcPckLbl.Text = winVariables(1).ToString()
        System.Threading.Thread.Sleep(1000)
        ' ''Delete the file on server - we don't need it anymore
        Dim Proc2 As New System.Diagnostics.Process
        Proc2.StartInfo = New ProcessStartInfo("C:\Windows\psexec.exe")
        Proc2.StartInfo.Arguments = COMPUTERNAME & " -s cmd /C del c:\somefile.txt"
        Proc2.StartInfo.RedirectStandardInput = True
        Proc2.StartInfo.RedirectStandardOutput = True
        Proc2.StartInfo.UseShellExecute = False
        Proc2.Start()
        System.Threading.Thread.Sleep(500)
        Proc2.Close()
        ' Delete file locally
        File.Delete("C:\somefile.txt")
 End Sub

【问题讨论】:

    标签: asp.net vb.net wmi psexec


    【解决方案1】:

    根据其他研究,对于此类项目,PsExec 似乎是最佳途径。

    【讨论】:

      【解决方案2】:

      是的,PsExec 是迄今为止最好的方法。 PsExec 使用 RPC 访问 ADMIN$ 共享。但是,如果 RPC 被禁用,例如在默认 Win7 中,并且 WMI 被启用,并且您的帐户有一个共享文件夹可用于读/写访问,那么您可以使用由 Frank White 创建的解决方法几个月前使用这种“cmd /c echo ...”方法:

      strCommand = "cmd /c echo myTextCommands > c:\temp\testscript.txt"

      要查看完整的 VBScript,请参阅我的解决方案 here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-15
        相关资源
        最近更新 更多