1
votes

I'm writing some pretty simple scripts in Powershell 32-bit and they are working fine.

For instance, I want to open internet explorer to a webpage and start typing in keys. The following code works fine when I run it in 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}")

I have downloaded a Windows PowerShell plugin and have executed the same code through it and received the following errors:

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

Is there a reason why my code will not work through Jenkins like it does in Powershell? I tried some simple echo scripts which worked fine, but any more complex and it throws an error.

1
There might be dependencies which are missing on your build server causing these errors. - Anthony Forloney
What user are you using to run the jenkins agent, lack of permission can cause issues. COM objects have other permissions associated with them as well. - JamesD
Could you elaborate on this Anthony? I'm a bit new to Jenkins and how to start remedying the situation. - LeChuck
Hi James, I'm using the admin, so I should have no issues with permissions should I? - LeChuck
@LeChuck So the jenkins service is running as admin in the services TAB? The application error seems like the COM object is not registered to this specific account. - JamesD

1 Answers

0
votes

You may need to set execution policy once: https://technet.microsoft.com/library/hh849812.aspx

Set-ExecutionPolicy Unrestricted

Run PowerShell as admin and run that command. There may be security concerns using Unrestricted. But if it works, read the link above and find the policy that works best in your context. Options are (copy/pasted from the link):

  • Restricted. Does not load configuration files or run scripts. Restricted is the default execution policy.
  • AllSigned. Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on the local computer.
  • RemoteSigned. Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
  • Unrestricted. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.
  • Bypass. Nothing is blocked and there are no warnings or prompts.
  • Undefined. Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy that is set in a Group Policy scope.