Powershell scripts will not work with Jenkins plugin on Windows - powershell

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.

Related

Import pfx file with sn.exe using powershell

I am trying to install pfx file automatically every time the Azure Pipeline runs. Below is the powershell script i am trying to implement. I need it to provide the password automatically:
Set-Location "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools"
Start-Process -FilePath "sn.exe" -ArgumentList "-d",$container
Start-Sleep 2
Start-Process -FilePath ".\sn.exe" -ArgumentList "-i","c:\cert_2013.pfx",$container -NoNewWindow
Start-Sleep 2
[void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("$($CerPass){ENTER}")
Start-Sleep 2
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
When the pipeline runs the ps1 file it is erroring out at SendWait("$($CerPass){ENTER}") Step. Below is the exact error message:
Enter the password for the PKCS#12 key file:
Could not read password for PKCS#12 blob in c:\cert_2013.pfx --
Console input may not be redirected for password entry.
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At line:1 char:1
+ & 'C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe' -NoLogo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Exception calli...cess is denied":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
At C:\PowerShellScript\CertReInstall.ps1:18 char:46
+ [System.Windows.Forms.SendKeys]::SendWait("$($CerPass){ENTER}")
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : Win32Exception
[error]Non-Zero exit code: '1' for ComputerName: 'PQAFCTC1'
[error]Atleast one remote job failed. Consult logs for more details. ErrorCodes(s): 'RemoteDeployer_NonZeroExitCode'
[section]Finishing: Run PowerShell on Target Machines
I need it to be automatically send the password to the file when installing. When i run the ps1 file directly on the server it works fine, but I need this script to be part of pipeline task (azure devops pipeline).
Any help would be greatly appreciated.
Thank you!

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!

Error creating a powershell COM object in Jenkins

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?

Remote Powershell Access denied for certain dll's execution for Sharepoint 2013

I am attempting to automate a sharepoint 2013 deployment via remote powershell from the build server. Everything executes as expected except when having anything to do with some class in sharepoint dll's such as (Microsoft.SharePoint.Publishing, Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings)
If I run the same script locally under the same credentials it runs fine.
I have considered the below:
The user has full admin right on both machines
Disabled UAC on the remote server
Followed the required Remote Powershell steps in thig post (http://social.technet.microsoft.com/Forums/sharepoint/en-US/09b60466-5432-48c9-aedd-1af343e957de/user-cannot-be-found-when-using-invokecommand-newspsite-on-sharepoint)
I set powershell to run as admin by defualt via the registry (New-Item -Path "Registry::HKEY_CLASSES_ROOT\Microsoft.PowershellScript.1\Shell\runas\command" -Force -Name '' -Value '"c:\windows\system32\windowspowershell\v1.0\powershell.exe" -noexit "%1"')
Script Code:
#Set the radio buttons value
$settings = New-Object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings (,$rootWeb)
$settings.GlobalNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::PortalProvider
#Set the radio buttons value
$settings.CurrentNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::PortalProvider
write-host "I am here.........................."
$settings.Update()
#Set the Publishing Web
$SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($rootWeb)
#Global Navigation Settings
$SPPubWeb.Navigation.InheritGlobal = $false
$SPPubWeb.Navigation.GlobalIncludePages = $false
The Remote Powershell output is as below:
I am here..........................
Exception calling "Update" with "0" argument(s): "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException
+ PSComputerName : Contoso-DEVSP
Exception setting "GlobalIncludePages": "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
+ PSComputerName : Contoso-DEVSP
Many thanks in advance
You need to check CredSSP authentication. Remote PowerShell execution with SharePoint fails as the second hop translates the credentials to system credentials. If the task involves querying or updating DB server, it will fail as SYSTEM account will not have access the remote PowerShell on SQL Server. You need to enable CredSSP.
Check this blog post I wrote a while ago. This is not specific to SharePoint but it should apply to your scenario as well.
http://www.ravichaganti.com/blog/?p=1230