我在Powershell 32位中写了一些非常简单的脚本,它们工作正常。
例如,我想在网页上打开Internet Explorer并开始输入密钥。当我在powershell ISE中运行它时,以下代码工作正常。
Add-Type –AssemblyName System.Windows.Forms
$url = "http://WebAddress"
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate($url)
sleep 30
[System.Windows.Forms.SendKeys]::SendWait("{2}{tab}{H}{E}{L}{L}{O}")
我已经下载了Windows PowerShell plugin并通过它执行了相同的代码并收到以下错误:
Building on master in workspace C:\Program Files\Jenkins\workspace\Jenkins Test
[Jenkins Test] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\WINDOWS\TEMP\hudson257018662776252417.ps1'"
New-Object : Creating an instance of the COM component with CLSID {0002DF01-000
0-0000-C000-000000000046} from the IClassFactory failed due to the following er
ror: 80004005.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:4 char:17
+ $ie = New-Object <<<< -com "InternetExplorer.Application"
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:5 char:13
+ $ie.Navigate <<<< ($url)
+ CategoryInfo : InvalidOperation: (Navigate:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:9 char:42
+ [System.Windows.Forms.SendKeys]::SendWait <<<< ("{2}{tab}{H}{E}{L}{L}{O}")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我的代码是否有理由不像Jenkins那样在Powershell中工作?我尝试了一些简单的echo脚本,它们运行良好,但是更复杂,它会引发错误。
答案 0 :(得分:0)
您可能需要设置一次执行政策:https://technet.microsoft.com/library/hh849812.aspx
Set-ExecutionPolicy Unrestricted
以管理员身份运行PowerShell并运行该命令。使用Unrestricted可能存在安全问题。但是如果它有效,请阅读上面的链接,找到最适合您的上下文的策略。选项是(从链接复制/粘贴):