Jenkins execute PowerShell scripts - windows

I'm trying to run PowerShell scripts from Jenkins, but it seems to completely ignore the execution policy! This happens either by executing powershell.exe directly, or using the PowerShell plugin
Additional information:
Jenkins is running as a Windows Service (using the Local System account, non-interactive). Connecting to that server, and checking execution policy is indeed RemoteSigned:
PS C:\> whoami
nt authority\system
PS C:\> Get-ExecutionPolicy
RemoteSigned
PS C:\>
However, when running a Jenkins build, this is not the case. Here's the output of executing Get-ExecutionPolicy -List inside a build step:
d:\workspace\test-job>powershell Get-ExecutionPolicy -list
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine Undefined
I also tried setting it explicitly from the build step, nothing.
What am I missing?

After more than two hours of pulling my hair, the problem was x86/x64!!!
Turns out, Set-ExecutionPolicy of x64 (default) PowerShell has no effect on the x86 settings!
Jenkins is a 32-bit process, running from a Windows service - it executes the x86 PowerShell!
In addition, the Modules directory for x86 PowerShell is located under %systemroot%\SysWow64\WindowsPowerShell\1.0\Modules, another important fact to remember!

Related

Problem to change the Windows policy of execution in PowerShell

I try to run the command ng in a PowerShell, but systems require that the script must be signed.
I try to change my policy of execution.
This is my current configuration Get-ExecutionPolicy -List:
Scope ExecutionPolicy
----- ---------------
MachinePolicy AllSigned
UserPolicy Undefined
Process Undefined
CurrentUser RemoteSigned
LocalMachine Unrestricted
Then, I run the next command Set-ExecutionPolicy -Scope CurrentUser Unrestricted
The change on the policy it's OK, but appear an error telling another policy defined in a most especific ambit deny the value.
And the command ng still not running with the error, \AppData\Roaming\npm\ng.ps1 it's not signed digitally.
Can you help me?
Ty everyone

GitLab Runner not starting process

I have configured a GitLab Runner to run on one of my computers (the computer is Windows and uses Powershell to run yml script commands).
For my repo I have configured the yml file to copy the contents of the build into another directory, then have the runner run: Start-Process -FilePath "$PROJECTNAME.exe" -ArgumentList "$PROG_ARG_1" after changing into that directory.
However, the pipeline succeeds but the process that has been started (using "Start-Process" above) cannot be found on the computer anywhere (has not actually been started).
Am I doing something wrong?
EDIT: it is running just as a "Background Process" (needed to open control panel to verify it)

Jenkins on Windows: cannot run PowerShell scripts due to Execution Policy

I am running Jenkins on Windows Server 2019 and I'm facing problems asking it to run some PowerShell script due to a bad Execution Policy:
powershell.exe -NonInteractive -ExecutionPolicy Bypass -File C:\Windows\TEMP\jenkins3203707218318955273.ps1
File C:\Windows\TEMP\jenkins3203707218318955273.ps1 cannot be loaded. The file
C:\Windows\TEMP\jenkins3203707218318955273.ps1 is not digitally signed. You cannot run this script on the current
system. For more information about running scripts and setting execution policy, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
I checked the Execution Policy as my Administrator user and they seems to be good:
PS C:\Windows\system32> Get-ExecutionPolicy -list
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser Undefined
LocalMachine RemoteSigned
Jenkins is running with the Local System Account:
I'm setting them using both an Active Directory GPO and a Local Group Policy:
Could you help me to find out where I'm wrong, please?

Package Manager Console Not Working

I have checked extensively for a solution to the below error to no avail.
When opening Package Manager Console in Visual Studio, I receive an error stating that "There was an error in loading the format data file.. Path --> to file" cannot be loaded because its execution is blocked by software restriction policies. For more information contact your administrator.
These are all of the things i've done to correct but failed.
Running Powershell as an Administrator.. I Set-ExecutionPolicy to RemoteSigned and Unrestricted in powershell(x86), powershell ISE (x86) and also both 64bit versions..
Running Powershell as an Administrator.. I Set ExecutionPolicy -scope for process, currentuser and localmachine to RemoteSigned and Unrestricted.
Reinstalled Visual Studio.
Set the ExecutionPolicy in Registry Editor to RemoteSigned and Unrestricted.
But I am still getting the error.. Can someone please help..
Thanks
Let me know if you tried the following:
PowerShell says "execution of scripts is disabled on this system."
http://sqlish.com/file-ps1-cannot-be-loaded-because-the-execution-of-scripts-is-disabled-on-this-system-please-see-get-help-about_signing-for-more-details/
Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies
if all fails do the below:
It's possible that you changed the execution policy for 64-bit
powershell and the package manager is running 32-bit (or vice versa).
I'd try opening 32-bit console (PowerShell (x86)) and setting the
execution policy there, as the error is definitely pointing to that
kind of resolution.
Make sure you restart visual studio after changing execution policy so
that changes can take effect. Also make sure you changed execution
policy globally with administrator username and password.
Source:file cannot be loaded because the execution of scripts is disabled on this system
As a workaround, I found this solution (more details at stackoverflow.com/a/53433786/8358565)
Execute the following commands in the Package Manager Console
Set-ExecutionPolicy -Scope Process Bypass
Import-Module "your-solution-directory/packages/EntityFramework<your EF version>/EntityFramework.psd1"

Bamboo remote build agent cannot find powershell.exe after installing nodejs

I just installed nodejs on one of my build servers (Win Server 2008 R2) which hosts a Bamboo remote agent. After completing the installation and doing a reboot I got stuck in the following situation:
The remote Bamboo build agent is running as a windows service with user MyDomain\MyUser. When a build with an inline powershell task is executing it fails with the error (from the build agent log):
com.atlassian.utils.process.ProcessNotStartedException: powershell could not be started
...
java.io.IOException: Cannot run program "powershell"
...
java.io.IOException: CreateProcess error=2, The system cannot find the file specified
Loggin on to the server as MyDomain\MyUser, I have checked that powershell is in the path:
where powershell
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
I have tried to restart the service and reboot the machine multiple times. No luck. The only thing that works is if I execute my scripts as a bat file with an absolute path to powershell - but I do not want that.
I have searched for solutions on this, but even though this one seems related: Hudson cannot find powershell after update to powershell 3 - the proposed solutions do not work.
What am I missing here?
If you do a default installation of nodejs you will see that it adds nodejs and npm to the path. Sometimes I have seen that the installer adds a user variable named PATH - it might be that the Bamboo agent decides to read the user path without "merging" it with the system path. I think it would be worth a try to give that a look.
As per Atlassian support page, this is related to a bug in Java Service Wrapper. I tried Workaround-2 since there was no user PATH variable in my system. I had to uninstall bamboo agent service and Java 64 versions from the agent machine to apply the workaround-2.

Resources