OSDeploy / OSDBuilder

PowerShell Module
http://osdbuilder.com
MIT License
193 stars 55 forks source link

AllFunctions.ps1 : curl.exe fails with NativeCommandError in Jenkins #73

Open andreacuozzo opened 2 years ago

andreacuozzo commented 2 years ago

OSDBuilder version; 21.12.15.1

Command:

Update-OSMedia -execute -Name Windows Server 2019 Datacenter Desktop Experience x64 1809 17763.379 -Download 
-Include SSU,LCU,DotNetCU,AdobeSU

Scenario: I'm using Jenkins to run a powershell script on a remote host. The Update-OSMedia fails with the error below, because the Jenkins environment doesn't handle well the output generated by the curl.exe call in AllFunctions.ps1 starting on line 3054:

if (Get-Command 'curl.exe' -ErrorAction SilentlyContinue) {
    Write-Verbose "cURL: $SourceUrl"
    if ($host.name -match 'ConsoleHost') {
        Invoke-Expression "& curl.exe --insecure --location --output `"$DownloadFullPath`" --url `"$SourceUrl`""
           }
    else {
         #PowerShell ISE will display a NativeCommandError, so progress will not be displayed
         $Quiet = Invoke-Expression "& curl.exe --insecure --location --output `"$DownloadFullPath`" --url `"$SourceUrl`" 2>&1"
            }

Error:

WSUSXML (Microsoft Updates) Download
07/14/2020 17:00:00 - DotNetCU - 2020-07 Cumulative Update for .NET Framework 3.5 4.7.2 and 4.8 for Windows Server 2019 for x64 (KB4566516)
C:\Build\OSDBuilder\Windows2019\Updates\windows10.0-kb4565632-x64-ndp48_8b481763cf07efc55f729bb1e08dc9fe32d3237a.cab
http://download.windowsupdate.com/c/msdownload/update/software/secu/2020/06/windows10.0-kb4565632-x64-ndp48_8b481763cf07efc55f729bb1e08dc9fe32d3237a.cab
C:\Windows\system32\windowspowershell\v1.0\powershell.exe : curl.exe :   % Total    % Received % Xferd  Average Speed  
 Time    Time     Time  Current
At C:\Users\test\AppData\Local\Temp\jenkins6057044616296770173.ps1:2 char:1
+ C:\Windows\system32\windowspowershell\v1.0\powershell.exe -File C:\bu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (curl.exe :   % ...  Time  Current:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Workaround (there might be other ones better than this):

the code runs through the ($host.name -match 'ConsoleHost') path, and fails here:

Invoke-Expression "& curl.exe --insecure --location --output"$DownloadFullPath" --url"$SourceUrl""

the workaround is to force it to run through the other "ISE quiet" path that handles the curl.exe output:

$Quiet = Invoke-Expression "& curl.exe --insecure --location --output"$DownloadFullPath" --url"$SourceUrl" 2>&1"

It's been months since I've last used this script of mine, it was working fine on previous OSDBuilder versions: my scenario (Jenkins agent running a script remotely) might be a corner case unworhty of being investigated and fixed, just my 2 cents should anyone else have the same problem.

Andrea

FLeven commented 2 years ago

You could always try to da a Invoke-Webrequest, if you are working in powershell.

@andreacuozzo could you share how you manage the import of base images in your Jenkins pipeline ?

andreacuozzo commented 2 years ago

Thanks FLeven: the Invoke-Expression code snippets is taken from the allfunctions.ps1, called by Update-OSMedia, I'll play with the Invoke-Webrequest, but it seems to me it would still require AllFunctions.ps1 to be changed, and it's probably not worthy for my corner case; as of now I'm just copying my modified version of AllFunctions.ps1 right before calling Update-OSMedia.

I'll soon post on github my attempt at using Jenkins to orchestrate a bunch of powershell scripts in order to generate a vsphere template of Windows 2019 with the latest updates, run some unit tests and a call to the qualys api to make sure the finished product is vulns free (4 and 5 severity, at least). I'll let you know as soon as it's done.