使用Powershell从VB.NET代码调用PhantomJS脚本
我目前正试图使用phantomJS来拍摄一个工作网页的截图。我的脚本工作正常,可以从powershell运行它使用Powershell从VB.NET代码调用PhantomJS脚本,vb.net,shell,powershell,phantomjs,pipeline,Vb.net,Shell,Powershell,Phantomjs,Pipeline,我目前正试图使用phantomJS来拍摄一个工作网页的截图。我的脚本工作正常,可以从powershell运行它 Dim PhantomString As String = $"'{System.Environment.CurrentDirectory}\phantomjs' PhantomJSScript.js {parameter1} {parameter2} {parameter3} {parameter4}" 这是一个字符串,表示我在powershell中用于执行PhantomJSScr
Dim PhantomString As String = $"'{System.Environment.CurrentDirectory}\phantomjs' PhantomJSScript.js {parameter1} {parameter2} {parameter3} {parameter4}"
这是一个字符串,表示我在powershell中用于执行PhantomJSScript(称为PhantomJSScript.js)的调用。当我试图从我的vb.net代码中运行此命令时,我得到一个错误,即参数不是可识别的标记。我正在使用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
这是我用来运行命令的代码。为了补偿在{parameter1}
不是可识别标记时弹出的错误,我尝试在命令上使用parameters属性。我将PhantomString中的每个参数添加到命令属性中,并将它们从PhantomString中删除。这允许脚本运行(或者看起来是这样),但是,它只是返回了我传入的PhantomString的确切值。我希望得到我要求的图像的位表示
以下是我尝试以另一种方式传递参数时代码的外观:
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
有谁能给我一些关于如何让它正常工作的见解吗
谢谢 所以我解决了这个问题。基本上,这与.exe的localhost路径中有一个空格有关。我处理它的方法是简单地将phantomjs.exe移动到不同的路径,目录名中没有空格。当它被移动到dev/staging/live时,我们不必担心空间问题,因为文件路径完全不同