Powershell codes throwing errors on Jenkins - powershell

I've added the following line of Powershell codes in Jenkins and it's throwing some errors, although it's working fine when I'm trying to execute it via Powershell ISE on the same desktop/machine.
I have configured the Jenkins service logon and the user ID got correct permission.
Kindly advise.
servers = Get-Content "C:\tmp\script\updated\SessionHost.txt"
foreach ($server in $servers)
{
$FolderWindows = Start-Process -FilePath "\\$server\C$\Temp" -WindowStyle Maximized
}
Error Message on Jenkins:
Start-Process : This command cannot be run due to the error: Server execution
failed.At C:\tmp\script\updated\MainTaiwanUAT.ps1:385 char:22
+ $FolderWindows = Start-Process -FilePath "\\$server\C$\Temp"
-WindowS ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOp
erationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
ommands.StartProcessCommand

Related

Automate response to User prompt in PowerShell

I'm new to PowerShell
I want to send 3 different responses to 3 different user prompts that I get when running a PowerShell script using another PowerShell script in Jenkins
I tried
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("dummytext")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
$command = "D:\testing\source\sample.ps1" + " -param1 'dummyParam'"
Invoke-Expression $command
but I keep getting this error
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At D:\testing\powershell\testing.ps1:6 char:1
+ [System.Windows.Forms.SendKeys]::SendWait("y")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : Win32Exception
Build step 'PowerShell' marked build as failure
Finished: FAILURE
Does anyone how to fix this issue

Powershell - This command cannot be run

I have a C++ application that launches a ps1 script using the following command
ShellExecuteA(0, "open", "powershell.exe", -executionpolicy RemoteSigned -File script.ps1", NULL, SW_NORMAL)
The script contains the following
$version = [System.Environment]::OSVersion.Version.Major
if ($version -gt 9)
{
$app = 'C:\Windows\System32\uwfmgr.exe'
$appargs = 'servicing enable'
Start-Process $app $appargs
Start-Sleep -s 5
$appargs= 'filter restart'
Start-Process $app $appargs
}
The end result should be that the Windows 10 machine (protected by UWF) should request service mode on its next reboot and the computer should restart
What happens instead is that the following error is raised
Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At script.ps1:6 char: 2
+ Start-Process $app $args
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
Edit:
Added
$Error[0] | FL * -Force
and renamed $args to $appargs

When running a powershell script via Jenkins getting this error : "NativeCommandError" not sure how to use invoke-command

Ignoring an errorlevel != 0 in Windows PowerShell (ISE)
I have read this link but not sure how to run "Invoke-Command" my $ErrorActionPreference is "continue".
This is the error I am getting :
06:06:55 + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command '& ''E: ...
06:06:55 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
06:06:55 + CategoryInfo : NotSpecified: (root : 2020-10-...something:String) [], RemoteException
06:06:55 + FullyQualifiedErrorId : NativeCommandError
The $errorActionPreference should be defined inside the scriptblock like:
Invoke-Command {$ErrorActionPreference = 'SilentlyContinue'; REST OF THE CODE}

Access denied to powershell.exe Start-Process from Jenkins descriptive pipeline

I am very new to Jenkins. I have installed it locally on my Windows 10, with no slaves, only the master agent.
Then I created the following declarative pipeline:
pipeline {
agent any
stages {
stage('Stage 1: Running TSextract') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
powershell 'Start-Process -FilePath E:\\my-config\\bin2\\RemoteRunner.exe -ArgumentList "dummy.azurecr.io/my-extract:1.10.13 E:\\my-config test-79 -t td TSextract"'
}
}
}
}
}
When I run this command in PowerShell from the command line everything happens as it should: Start-Process -FilePath E:\my-config\bin2\RemoteRunner.exe -ArgumentList "dummy.azurecr.io/my-extract:1.10.13 E:\my-config test-79 -t td TSextract"
However, through Jenkins, via the above descriptive pipeline, this results in the following error:
powershell.exe : Start-Process : This command cannot be run due to the error: Access is denied.
At C:\windows\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\workspace\LocalPipeline#tmp\durable-eb81f70f\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Start-Process :...cess is denied. :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
At C:\windows\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\work
space\LocalPipeline#tmp\durable-eb81f70f\powershellScript.ps1:1 char:1
+ Start-Process -FilePath E:\my-config\bin2\RemoteRunner.exe -Argument ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOp
erationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
ommands.StartProcessCommand
So the question is what/ how exactly do I grant permissions to Start-Process from Jenkins?

'Provider load failure' during installation process

I execute two Powershell scripts during a installation process from a desktop application under Windows 10 IoT Enterprise.
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Bypass -File ".\KeyboardFilter.ps1"
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Bypass -File ".\ShellLauncher.ps1"
But the execution of the Powershell scripts is not successful. I get the following errors:
Get-WMIObject : Provider load failure
At C:\Program Files\Application\KeyboardFilter.ps1:31 char:19
+ ... $predefined = Get-WMIObject -class WEKF_PredefinedKey #CommonParams |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Write-Error : A positional parameter cannot be found that accepts argument 'is'.
At C:\Program Files\Application\KeyboardFilter.ps1:41 char:9
+ Write-Error $Id is not a valid predefined key
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-Error], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteErrorCommand
enable-windowsoptionalfeature : An attempt was made to load a program with an incorrect format.
At C:\Program Files\Application\ShellLauncher.ps1:4 char:1
+ enable-windowsoptionalfeature -online -featureName Client-EmbeddedShe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Enable-WindowsOptionalFeature], COMException
+ FullyQualifiedErrorId : Microsoft.Dism.Commands.EnableWindowsOptionalFeatureCommand
The installation process starts with administration permissions.
The first script adds key combinations to the Keyboard Filter (Windows 10 IoT feature).
The second scripts enable and configure the Shell Launcher (also Windows 10 IoT feature).
KeyboardFilter.ps1:
param (
[String] $ComputerName
)
$CommonParams = #{"namespace"="root\standardcimv2\embedded"}
$CommonParams += $PSBoundParameters
function Enable-Predefined-Key($Id) {
$predefined = Get-WMIObject -class WEKF_PredefinedKey #CommonParams |
where {
$_.Id -eq "$Id"
};
if ($predefined) {
$predefined.Enabled = 1;
$predefined.Put() | Out-Null;
Write-Host Enabled $Id
} else {
Write-Error $Id is not a valid predefined key
}
}
If I execute the Powershell scripts in a batchfile or on Powershell console, everything works fine. I also tried to execute the Powershell scripts during the installation process with Powershell x86 and x64, same errors in both cases.
Any hints, tips or solution for this problem?