简体   繁体   English

使用Powershell从VB.NET代码调用PhantomJS脚本

[英]PhantomJS Script Call From VB.NET Code Using Powershell

I am currently trying to use phantomJS in order to take a screenshot of a webpage for work. 我目前正在尝试使用phantomJS来获取工作页面的屏幕截图。 I have the script working correctly and can run it from powershell. 我的脚本正常工作,可以从powershell运行它。

Dim PhantomString As String = $"'{System.Environment.CurrentDirectory}\phantomjs' PhantomJSScript.js {parameter1} {parameter2} {parameter3} {parameter4}"

This is the string that represents the call I use in powershell to execute my PhantomJSScript which is called PhantomJSScript.js. 这是代表我在Powershell中用于执行PhantomJSScript.js的调用的字符串。 When I try to run this command from my vb.net code, I get the error that the parameters are not recognized tokens. 当我尝试从vb.net代码运行此命令时,出现以下错误:参数无法识别。 I am using System.Management.Automation.Runspaces.Runspace and other affiliated libraries to execute this. 我正在使用System.Management.Automation.Runspaces.Runspace和其他附属库来执行此操作。

Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace
    MyRunSpace.Open()
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
    MyPipeline.Commands.AddScript(PhantomString, True)

    Dim pipeLineResult As Collection(Of PSObject) = MyPipeline.Invoke()
    If MyPipeline.Error.Count > 0 Then
        ImageList = Nothing
    Else
        ImageList = (From pipeOut In pipeLineResult Select (pipeOut.ToString)).ToList
    End If
End Using
Return ImageList

This is the code that I am using to run the command. 这是我用来运行命令的代码。 To compensate for the error that pops up where {parameter1} is not a recognized token, I tried to use the parameters property on the command. 为了弥补在{parameter1}不是可识别标记的情况下弹出的错误,我尝试在命令上使用parameters属性。 I added each of the parameters in PhantomString to the command property instead and removed them from PhantomString. 我将PhantomString中的每个参数添加到了command属性中,并将它们从PhantomString中删除了。 This allowed the script to run (or so it seemed) however, it just returned the exact value of PhantomString that I passed in. I was expecting bit representation of the image I requested. 这允许脚本运行(或似乎可以运行),但是,它只是返回了我传入的PhantomString的确切值。我期望我所请求图像的位表示形式。

Here is what my code looks like when I try to pass parameters the other way: 当我尝试以其他方式传递参数时,代码如下所示:

    Dim PhantomString As String = $"'{System.Environment.CurrentDirectory}\phantomjs'"

    Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace
        MyRunSpace.Open()
        Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
        MyPipeline.Commands.AddScript(PhantomString, True)
        MyPipeline.Commands.FirstOrDefault.Parameters().Add("arg1", "PhantomJSScript.js")
        MyPipeline.Commands.FirstOrDefault.Parameters().Add("arg2", $"{parameter1}")
        MyPipeline.Commands.FirstOrDefault.Parameters().Add("arg3", $"{parameter2}")
        MyPipeline.Commands.FirstOrDefault.Parameters().Add("arg4", $"{parameter3}")
        MyPipeline.Commands.FirstOrDefault.Parameters().Add("arg5", $"{parameter4}")

        Dim pipeLineResult As Collection(Of PSObject) = MyPipeline.Invoke()
        If MyPipeline.Error.Count > 0 Then
            ImageList = Nothing
        Else
            ImageList = (From pipeOut In pipeLineResult Select (pipeOut.ToString)).ToList
        End If
    End Using

    Return ImageList

Could anyone please give me some insights on how to get this working properly? 谁能给我一些有关如何使其正常工作的见解?

Thanks! 谢谢!

So I have since resolved the problem. 因此,我已经解决了这个问题。 Basically, it had to do with the fact that there was a space in the localhost path to the .exe. 基本上,它与.exe的本地主机路径中存在空格有关。 The way I handled it was by simply moving phantomjs.exe to a different path without spaces in directory names. 我的处理方式是将phantomjs.exe移至目录名称中没有空格的其他路径。 When this gets moved to dev/staging/live, we don't have to worry about the space because the file path is completely different. 当将其移至dev / staging / live时,我们不必担心空间,因为文件路径完全不同。

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

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