V3.x - Copy file from windows to ubuntu subsystem file share if ubuntu is installed

Hi Guys,

need help to copy file from windows to ubuntu wsl share,

i created the below code which works manually, the ubuntu Distribution subsystem gets installed as user and not system,

i need to copy the certificate file with PSADT from windows share to ubuntu share,
the below command works fine but how can i make it work while deploying using SCCM with PSADT? as we know SCCM executes this with system account i am curious if this logic will work or not?

i even need to check if ubuntu subsystem is installed or not then it should copy.

please suggest

$WSLTargetPath = "/usr/local/share/ca-certificates/root.crt"

 wsl -d Ubuntu -u root cp /mnt/c/temp/root.cer $WSLTargetPath
                   
 wsl -d Ubuntu -u root update-ca-certificates

If your script works as user manually, try launching it as the user either with PSADT or ActiveSetup if applicable.

If you run PSADT as system you can launch things as the current user.

I created a Tip about your options here.
Please note that it refers to PSADT v3 functions and that you’ll have find the match V4 function names.
If you need to launch the script only when a user is logged in, consider ActiveSetup. That way you can install regardless if a user is logged-in or not.

The only issue i am facing is resolving the WSL share path inside the IF condition it’s not getting resolved can someone help on this? The Log is not written as the if condition not satisfied, but it is still copying the file to WSL share:

                 $certWSL = "$dirfiles\Dev.cer"
                  $wslTargetPath = "/usr/local/share/ca-certificates/dev.crt"
                  $wslPath = "\\wsl$\Ubuntu\usr\local\share\ca-certificates"
                  
                   if(test-path $wslpath)  {            
   
                    Write-Log "Certificate file found. Copying to WSL path."
                    try {
                        
                        Execute-Process -Path "wsl.exe" -Parameters "-d Ubuntu -u root cp /mnt/c/$(( $certWSL -replace '\\', '/' ).replace(':','$')) $wslTargetPath"
                        Execute-Process -Path "wsl.exe" -Parameters "-d Ubuntu -u root update-ca-certificates"
                        
                        Write-Log "Certificate copied successfully to WSL."
                    } catch {
                        Write-Log "Certificate file not copied to WSL.." 
                    }
                  }

If this script is run as system, it should write to the log file just fine.

But if this script is run as USER, then make sure the user has permission to the log file’s parent folder.

BTW: I would add an Else to the If statement in-case the $wslpath does not exist.

I am running it as system and tried admin as well and it is copying the file however it is not writing the log, I tried troubleshooting it and if condition always returning not found…. But it is executing the try block and copying the file… but no log entry if run using system or admin

What I am getting from you is:
1 - The PSADT log file is being created.
2 - NONE of the 3 Write-log lines listed above show up in the log file.
3 - The 2 Execute-Process lines listed above do work. (IOW: the files are copied)
4 - Other Write-log lines, before and after this block of code, do show up in the log file.

Is this correct?

Yes correct somehow windows not able to recognise wsl path I suspect

If the command within the If statement works then I see no reason why the Write-Log won’t work. You should probably check your PSADT configuration where you are writing log files and check for log files in both paths. In v3 the configuration is in the file AppDeployToolkitConfig.xml. Check both Toolkit_LogPath and Toolkit_LogPathNoAdminRights, it uses one of these depending on the rights of the user executing the script.

   $certWSL = "$dirfiles\CA_GN2.cer"
                  $wslTargetPath = "/usr/local/share/ca-certificates/ca_gn2.crt"
                  $wslPath = "\\wsl$\Ubuntu\usr\local\share\ca-certificates"
                  
                   if(test-path $wslpath)  {                   
                                                                          
                        
                        Execute-Process -Path "wsl.exe" -Parameters "-d Ubuntu -u root cp /mnt/c/$(( $certWSL -replace '\\', '/' ).replace(':','$')) $wslTargetPath"
                        Execute-Process -Path "wsl.exe" -Parameters "-d Ubuntu -u root update-ca-certificates"
                                                
                    echo "found"
                    }

                  else 

                  {
                  echo "not found"
                   echo (test-path $wslpath)
                  }

not found
False

this is the result of psadt while checking manually with ADM rights, it still copies the file but look at the output

Impossible.

Either the files are already copied or something else copies the files for you.

I also think wsl$ should be $wsl on the 3rd line.

Yes impossible but i am facing it… i and my team tried different ways but same result, the wsl takes 15 seconds to copy it as its weird

If you install ubuntu on windows you can access the fileshare with \\wsl.localhost or \\wsl$…… i tried \\wsl.localhost the file copy fails but with \\wsl$ it copies the file

Somehow if condition able to check the path of ubuntu subsystem but when it starts the wsl.exe session it ignores the write log or echo

Asked this to chatgpt and getting below response , so i will first check the session of wsl if its not running then will initiate it and then execute the remaining code

Your script is checking for the existence of $wslPath using Test-Path, but the issue is that Test-Path does not reliably detect WSL paths (\wsl$ shares) immediately after being accessed or created. Here is why this might be happening and how to fix it:

Possible Causes:

  1. WSL Path Timing Issue:

Sometimes, the \wsl$\Ubuntu\usr\local\share\ca-certificates path is not immediately accessible after launching WSL, especially if WSL was not already running.

  1. Permissions Issue:

Even if the path exists, Test-Path might fail if the current user doesnt have permission to access the \wsl$ share.

  1. WSL Not Fully Initialized:

If WSL was not running before the script executed, it may take a moment to initialize, causing Test-Path to fail.