PSD icon indicating copy to clipboard operation
PSD copied to clipboard

%ScriptRoot% launches a very simple script that doesn't work

Open Valceen opened this issue 9 months ago • 21 comments

Hi all

I'm trying to run a simple script from the script directory.

Copy-Item -Path ".\Scripts" -Destination "C:\Windows\Setup" -Recurse -Force

It copies locally to the C:\Windows\Setup\ folder the script directory which contains other scripts.

Image

Image

It works under MDT, no problem, but under PSD, it doesn't!

What am I doing wrong?

Valceen avatar Apr 09 '25 10:04 Valceen

xcopy works, use the "run command line" task sequence step instead.

xcopy [source] [destination] /option

Example:

xcopy "%ScriptRoot%\CustomStartMenu\Win11\start2.bin" "C\Users\Default\AppData\Local\Packages\Microsoft.Windows.StartMenuExperienceHost_cw5n1h2txyewy\LocalState" /y

MDA400 avatar Apr 09 '25 20:04 MDA400

No, that doesn't work either!

The task sequence stops and I get an error in the PSD: "Task sequence failed, Return Code is -2147467259"

Image

In the smsts.log, I get this error:

Image

I don't know what to do anymore?

Valceen avatar Apr 10 '25 13:04 Valceen

%ScriptRoot% is not present in the Variables.dat. %DeployRoot% does not work but is initialized in the file

but it crashes every time the task sequence starts...

Task sequence failed, Return Code is -2147467259

I'm tired of searching... I'm reinstalling the entire server!

Valceen avatar Apr 10 '25 15:04 Valceen

it works, but looks like you are using a SMB share path ( \ \Servername\path ) instead of %ScriptRoot% variable in your command line field, which isn't supported with PSD.

Also, try without the \ * or at least without the asterisk * after the source path

MDA400 avatar Apr 10 '25 15:04 MDA400

I'm reinstalling the server. I'll keep you posted! Thank you for your help!

Valceen avatar Apr 10 '25 16:04 Valceen

It doesn't work...

Image

result on the workstation

Image

Valceen avatar Apr 11 '25 14:04 Valceen

smsts.log

Image

Valceen avatar Apr 11 '25 14:04 Valceen

I see you are trying to move a whole folder's contents (recursive) from your deployment share to the computer being imaged.

Unfortunately, xcopy cannot do this in PSD. You will need to use something like Invoke-WebRequest to download a folder's contents from the web server the PS deployment share is being hosted on, to a local directory on the computer.

You will also need to pass windows credentials along with the invoke-webrequest, but you could also enable Anonymous Authentication along side Windows Authentication (Windows authentication is needed otherwise it breaks the PE environment authentication). If you choose to enable Anonymous Authentication for your deployment share, make sure you only allow it for directories that don't have any sensitive data in them.

Image

I use this powershell script to achieve this - Start-DirDownload.ps1

If you want items to stream faster, hide the progress bar by entering $ProgressPreference = 'SilentlyContinue' after the first Invoke-WebRequest entry you see in the script.

Image Image

MDA400 avatar Apr 11 '25 15:04 MDA400

Thank you very much for your help!

The scripts are finally copied to the correct location: C:\Windows\Setup\Scripts

Image

But these scripts then run from the local directory, and it doesn't work...

Under PSD : Image

Under PSD : Image

No scripts run (they run but do nothing). These scripts work under MDT with the same parameters , and they work when launched manually.

Under MDT : Image

This is going to drive me crazy....

Valceen avatar Apr 11 '25 17:04 Valceen

Good to hear the directory download script at least worked!

What are the reason(s) you are trying to run the script from a directory local in Windows, but using the deployment process to execute it?

Are they possibly designed in a way where you could just run the script from the deployment share (just entering DefaultUser.ps1 in the "Powershell Script" field in the task step), which then should process locally on the computer being imaged anyway?

MDA400 avatar Apr 11 '25 19:04 MDA400

Because it's easier to do it locally (no network issues, no rights issues, etc.)...

And because we can reapply it whenever we want!

And because I prefer it to be local for the support teams, it saves me from giving them access to the network share!

Valceen avatar Apr 11 '25 19:04 Valceen

I have a different way of doing it. Install it as an application with the needed contents in your case the script folder followed with a bat script that has the copy command to copy your scripts folder to the needed location. Then when you scope the application out in the Applications just specify the bat file as the install command. Add to your task sequence and d4d. No need to mess with importing or using smb shares or none of that mess.

Addiso909 avatar Apr 12 '25 01:04 Addiso909

@Valceen PSD itself has a local caching machanism already builtin. It creates copies of several folders below your deployment share in C:\MININT\Cache. I don´t know exaclty which folders, but at least "Scripts" and "Tools", including their subfolders, are there.

Just name the script to run and it should be fine.

Maybe you also need to update the PSDPowershell.ps1 script with the one I posted here, so that relative paths are handled correctly.

daooze avatar Apr 22 '25 10:04 daooze

@daooze is correct PSD works by copying the contents over HTTP/HTTPS to the local cache folder, then it executes it from there. You could put it in the scripts folder, and it should copy the contents over. Then it sets the environment variables to that location. PSD does not work well with UNC paths anymore...

But I noticed in your screenshots your task sequences look like MDT task sequences. While this is the template to use for MDT version, it is NOT the one to for PSD. The PSD Client Standard one is simpler. Are you using the PSD Task Sequence templates? Based on screenshots, those look like MDT Templates.

Here is what a PSD Client/Server Standard Task Sequence will look like.

Image

YOU MUST USE THESE when using PSD! While this is not ideal, it does mean you must create new TS using that template (DO NOT COPY from other deploymentshares). You can verify by looking here:

Image

The reason for this is, PSD does not use the PHASE property where MDT did and some other things. Behind the scenes, the TS steps are wsf/vbs scripts and PSD replaces them with ps1, but not all; it is not 1:1.

Image

PowerShellCrack avatar Apr 23 '25 21:04 PowerShellCrack

But with all that I am running a PSD task sequence that should create two shortcuts via a ps1 script on the desktop. I am testing both Run PowerShell script and Run command line to see if they both honor %SCRIPTROOT%

Image

Image

I will show the results after its complete...

PowerShellCrack avatar Apr 23 '25 21:04 PowerShellCrack

After testing this. I see two results.

While it did copy them to the script folder; i have something flawed in the quick script i wrote....

Image

  1. Running the command line will translate %SCRIPTROOT% to literal path (eg. C:\MININT\Cache\Scripts). But Mine was stuck runing because I was dumb and running parameters in start in box

Image

  1. Running the PowerShell step will translate it even in the parameters

Image

PowerShellCrack avatar Apr 24 '25 00:04 PowerShellCrack

Just set your your files that you want copied over in the scripts folder. Use a command line sequence in the TS to run your copy files powershell script (e.g. powershell -ExecutionPolicy Bypass -File "%scriptroot%\script.ps1"

Torres7514 avatar Apr 28 '25 23:04 Torres7514

I'd given up! And I came back a few weeks later and I certainly have the solution thanks to you! I'm re-installing the PSD platform and testing again. Thanks everyone. I'll get back to you in a few days!

Valceen avatar May 18 '25 08:05 Valceen

I rebuilt a PSD server ! And it still doesn't work!

I followed @Daooze's recommendations... I replaced the PSDPowershell.ps1 file with @Daooze's.

Then I launched my scripts from %SCRIPTROOT%... But it doesn't work. The script starts and stops there:

Image Image

I'll give you the task sequences in XML. rename it to xml ts.txt

I also tested with the original PSDPowershell.ps1 but I have the same errors!

The same scripts work with MDT!

In short, nothing works with PSD.

Plus this error occurs at any time, during or before the "Install Operating System"...

Image

I'm going crazy!

Valceen avatar May 20 '25 18:05 Valceen

When looking at the Tx in notepad, i think i see what is going on and may be the issue:

A default run action by PSD does this:

Image

Where in yours I see that your just calling the ps1 file.

Image

Try doing:

powershell -file %SCRIPTROOT%\Copy-Utilisateur.ps1

I also noticed the path. Are the scripts you have under the scripts directory or under script folder in the scripts directory?

PowerShellCrack avatar May 20 '25 18:05 PowerShellCrack

"under script folder in the scripts directory" I renamed it to "Perso" to avoid problems.

Image

I run the scripts as you told me and it works (I have to edit one script at this stage and it seems to work if I check the "Continue on error" box). But it's at the end that it goes to waste.

Finally, I have a script that places the machine in the domain (my PSD server is in a workgroup)....

When I reboot, I log into the domain and I get this error because the deployment share is no longer accessible!

Image

Valceen avatar May 21 '25 15:05 Valceen