Error creating a powershell COM object in Jenkins - powershell

I want to create a COM object, internet explorer application.
The following code works fine in PowerShell ISE (32 bit):
$ie = New-Object -com InternetExplorer.Application
But, when I try running it on Jenkins Windows server (using either the powershell plugin or calling the PS1 with a batch file), I get the following error.
Building on master in workspace C:\Program Files\Jenkins\workspace\Jenkins Test
[Jenkins Test] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\WINDOWS\TEMP\hudson3148932824880446098.ps1'"
New-Object : Creating an instance of the COM component with CLSID
{0002DF01-0000-0000-C000-000000000046} from the IClassFactory failed due to the
following error: 80004005.
At C:\WINDOWS\TEMP\hudson3148932824880446098.ps1:1 char:17
+ $ie = New-Object <<<< -com InternetExplorer.Application
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
I'm using a VM at work (Server 2003). I'm not sure if this is an issue with my server permissions or if these are interactive commands being used with a non-interactive device. Can anyone help?

Related

error when trying to install chocolatey via powershell : cannot connect to remote server

I am trying to install chocolatey by following official documentation (https://chocolatey.org/install), this is what I am doing :
1-I am opening a cmd.exe running as administrator
2-I am running the following command : powershell and then switch into powershell mode
3-I am running the following command : $PSVersionTable.PSVersion and then get the following result : major:5 minor:1 build:16299 revision:785
4-I am running the following command : Get-ExecutionPolicy and then get the following result : RemoteSigned
5-I am running the following command : Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('http
s://chocolatey.org/install.ps1')) and get the following error :
Exception calling "DownloadString" with "1" argument(s): "Unable to connect to the remote server"
At line:1 char:51
+ ... ess -Force; iex ((New-Object System.Net.WebClient).DownloadString('ht ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I am working in a company (our os is windows 10 enterprise), and when trying or any colleague machine it's working perfectly and there is no (from what we have seen) any configuration difference between them and me.
Thanks in advance
There is nothing wrong with the code you tried. Basically, it is telling you that you are unable to connect, which might be a restriction or policy. Try the following:
$req = [System.Net.HttpWebRequest]::Create("https://www.google.com")
$req.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$req.Proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Then run the comment again. It has something to do with the credentials used in windows 10 for the request.

Task Scheduler Powershell DCOM error

I am running a task to execute a powershell script.
The script runs fine if I run it manually through powershell, or through the task scheduler with "Run only when user is logged on" is selected.
If I change it to "Run whether user is logged on or not" (even with highest privledges checked) I get the following error when I catch the exception from my powershell script that tries to open up internet explorer:
New-Object : Retrieving the COM class factory for component with CLSID {0002DF0
1-0000-0000-C000-000000000046} failed due to the following error: 80080005.
At D:\PSA\EYReserve\EY_reserve.ps1:14 char:18
+ $ie = New-Object <<<< -com internetexplorer.application;
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
I've tried steps provided in solutions for similar problems but they did not work. Namely modifying DCOM settings, adding "NETWORK SERVICE" with full permissions etc.
Anyone think of something I could have missed?
Thanks!

Powershell scripts will not work with Jenkins plugin on Windows

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.
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.

Powershell script not running correctly as a scheduled task on Windows 2008

I have this script which connects to a bunch of servers and gets service status, errors from the Application and System logs and disk space. It runs fine as a scheduled task on Windows XP but has some weird problem on Windows 2008. I am using a function to import the credentials from a separate file where they are stored in encrypted form:
function Import-credential($path) {
$cred = Import-Clixml $path
$cred.password = $cred.Password | ConvertTo-SecureString
New-Object system.Management.Automation.PSCredential(
$cred.username, $cred.password)
}
This works fine when I run it manually and when it runs as a scheduled task on Windows XP but fails on Windows 2008 with the following error:
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process ar
gument because the value of argument "password" is null. Change the value of ar
gument "password" to a non-null value."
At F:\Tools\MCHS Server Report\monitor.ps1:9 char:15
+ New-Object <<<< system.Management.Automation.PSCredential(
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
This is line 9:
New-Object system.Management.Automation.PSCredential(
In the task scheduler I have the full path to powershell.exe in Program/Script and -command & "F:\Tools\MCHS Server Report\monitor.ps1" in Add Arguments.
Any ideas?
EDIT: I tried changing the account it runs under to my own (I was using a local account specifically created for the scheduled tasks) and it worked alright. I have 0 ideas as to why it won't work with the other user considering we're both Administrators and seem to have the same privs...
ConvertTo-SecureString/ConvertFrom-SecureString by default use an encryption key that is bound to the user and computer that the string was originally encrypted on. If you log in as the service account and save the credential it should be able to decrypt it when it is running as a service.

Powershell: Running a .msc applet as another user

I'm currently writing a powershell script that asks for a single set of admin credentials, and uses those to run relevant applications, pulled from a network-hosted CSV. When I try to run
Start-Process $tools[$userInput-1].path.toString() -credential $credential
(where $tools is returning "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc") I get the error below
Start-Process : This command cannot be executed because the input "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" is an Invalid Application. Give a valid application and Run your command again.
At line:1 char:14
+ Start-Process <<<< "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" -credential
Get-Credential
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
If I need to, I'll just write a .bat file and run that, but I'd rather avoid that whenever possible.
And the reason I'm not using Invoke-Item is because it can't take -Credential, even if the man file says otherwise.
.msc is a saved console file, the host of which is mmc, so to start this from powershell you could use syntax similar to the following:
$mmcPath = "C:\Windows\System32\mmc.exe"
$mscPath = "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc"
Start-Process -FilePath $mmcPath -ArgumentList $mscPath