PSD icon indicating copy to clipboard operation
PSD copied to clipboard

Minor issue running powershell scripts

Open ASlowTurtle opened this issue 3 years ago • 6 comments

The PowerShell step in the task sequence (which utilises PSDPowershell.ps1) opens a blank powershell window instead of executing the script. This happens only if a full path is specified instead of a simple file name. The format %SCRIPTROOT%\Upload-PSDLogs.ps1 won't work (but Upload-PSDLogs.ps1 should work). This is a minor issue (it works if I provide the file name instead of the full path). However, most if not all examples on using scripts in MDT will use %SCRIPTROOT\ in front of the script name. It also can be confusing because executing scripts using cmd will require the full path.

<![LOG[PSDPowerShell.ps1: Starting: PSDPowerShell.ps1 - Version 0.0.1]LOG]!><time="19:23:30.96960" date="12-21-2022" component="PSDPowerShell.ps1:46" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: The task sequencer log is located at C:\MININT\SMSTSLog\SMSTS.LOG. For task sequence failures, please consult this log.]LOG]!><time="19:23:30.96960" date="12-21-2022" component="PSDPowerShell.ps1:47" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: tsenv:ScriptName C:\MININT\Cache\Scripts\Upload-PSDLogs.ps1]LOG]!><time="19:23:31.21960" date="12-21-2022" component="PSDPowerShell.ps1:50" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: tsenv:Parameters ]LOG]!><time="19:23:31.21960" date="12-21-2022" component="PSDPowerShell.ps1:51" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: About to run: ]LOG]!><time="19:23:31.68860" date="12-21-2022" component="PSDPowerShell.ps1:55" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: ArgumentList:  ]LOG]!><time="19:23:31.68860" date="12-21-2022" component="PSDPowerShell.ps1:56" context="CND-DPL-XN0CPE\Administrator" type="1" thread="7408" file="PSDPowerShell.ps1">

The about About to run: line expands an empty variable. I did a minor change to PSDPowershell.ps1 to line 53:

$ScriptToRun = Find-PSDFile -FileName $($tsenv:ScriptName)

Adding another split-path -Leaf -Path ...

$ScriptFileName = Split-Path -Leaf -Path $($tsenv:ScriptName)
$ScriptToRun = Find-PSDFile -FileName $ScriptFileName

This way it doesn't matter in which format the path is provided and Find-PSDFile can provide the path.

ASlowTurtle avatar Dec 23 '22 09:12 ASlowTurtle

Hello,

Thank you for the spotting of this issue and the details/fix provided, will communicate them to the team for further check and fix. One question for you, did you used a TS step to invoke the Upload-PSDLogs.ps1 script?

GeoSimos avatar Jan 18 '23 16:01 GeoSimos

Hi, I did use a TS step ("Run Powershell Script") to invoke the script. Snippet from ts.xml:

<step type="BDD_RunPowerShellAction" name="Upload-PSDLogs" description="" disable="false" continueOnError="true" successCodeList="0 3010">
  <defaultVarList>
	<variable name="ScriptName" property="ScriptName">%SCRIPTROOT%\Upload-PSDLogs.ps1</variable>
	<variable name="Parameters" property="Parameters"></variable>
	<variable name="PackageID" property="PackageID"></variable>
  </defaultVarList>
  <action>cscript.exe "%SCRIPTROOT%\ZTIPowerShell.wsf</action>
</step>

ASlowTurtle avatar Jan 18 '23 17:01 ASlowTurtle

Hi @ASlowTurtle,

From what I understand, you need to add the following for each "Run PowerShell Script" step in the PowerShell script command box: powershell.exe -file %SCRIPTROOT%\Your-Script.ps1 So in your example it should be:

<step type="BDD_RunPowerShellAction" name="Upload-PSDLogs" description="" disable="false" continueOnError="true" successCodeList="0 3010">
  <defaultVarList>
	<variable name="ScriptName" property="ScriptName">powershell.exe -file %SCRIPTROOT%\Upload-PSDLogs.ps1</variable>
	<variable name="Parameters" property="Parameters"></variable>
	<variable name="PackageID" property="PackageID"></variable>
  </defaultVarList>
  <action>cscript.exe "%SCRIPTROOT%\ZTIPowerShell.wsf</action>
</step>

As I said earlier, I will bring this to attention to the team in our next meeting. Please revert your changes in PSDPowershell.ps1 and test the script's command as mentioned above, please report back your experience/findings.

GeoSimos avatar Jan 18 '23 19:01 GeoSimos

Hey @GeoSimos I just did another test. But it produced the same result. (I reverted the changes to the PSDPowershell.ps1) Your suggestion will only work with the normal command line action but it wouldn't work with the "run powershell script" action (at least not in PSD). Executing e. g. powershell.exe -file %SCRIPTROOT%\Testscript.ps1 as "Run Powershell Script" leads to the same result (an empty powershell prompt). Note the last 4 lines.

<![LOG[Start-PSDLogging: Logging CMtrace logs to C:\MININT\SMSOSD\OSDLOGS\PSDPowerShell.log]LOG]!><time="18:28:23.35760" date="01-24-2023" component="PSDUtility.psm1:180" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDUtility.psm1">
<![LOG[PSDDeploymentShare.psm1: Reconnecting to the deployment share at [PathToDeploymentShare].]LOG]!><time="18:28:23.60760" date="01-24-2023" component="PSDDeploymentShare.psm1:424" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDConnection: PowerShell variable global:psddsDeployRoot is now =  [PathToDeploymentShare]]LOG]!><time="18:28:24.10760" date="01-24-2023" component="PSDDeploymentShare.psm1:71" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDConnection: PowerShell variable global:psddsDeployUser is now = [USERNAME]]LOG]!><time="18:28:24.12260" date="01-24-2023" component="PSDDeploymentShare.psm1:72" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[<Message containing password has been suppressed>]LOG]!><time="18:28:24.13860" date="01-24-2023" component="PSDDeploymentShare.psm1:73" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDContent: Download from Control to C:\MININT\Cache\Control took 00:00:00]LOG]!><time="18:28:24.23260" date="01-24-2023" component="PSDDeploymentShare.psm1:213" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDContent: Download from Templates to C:\MININT\Cache\Templates took 00:00:00]LOG]!><time="18:28:24.24760" date="01-24-2023" component="PSDDeploymentShare.psm1:213" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDProvider: PowerShell variable deployRoot is now = C:\MININT\Cache]LOG]!><time="18:28:24.26360" date="01-24-2023" component="PSDDeploymentShare.psm1:119" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDProvider: Set MDT Install_Dir to C:\MININT\Cache\ for MDT Provider.]LOG]!><time="18:28:24.32560" date="01-24-2023" component="PSDDeploymentShare.psm1:131" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDContent: Download from Tools\Modules to C:\MININT\Cache\Tools\Modules took 00:00:00]LOG]!><time="18:28:24.34160" date="01-24-2023" component="PSDDeploymentShare.psm1:213" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[Get-PSDProvider: Creating MDT provider drive DeploymentShare: at C:\MININT\Cache]LOG]!><time="18:28:24.60760" date="01-24-2023" component="PSDDeploymentShare.psm1:142" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDDeploymentShare.psm1">
<![LOG[PSDPowerShell.ps1: Starting: PSDPowerShell.ps1 - Version 0.0.1]LOG]!><time="18:28:24.63860" date="01-24-2023" component="PSDPowerShell.ps1:46" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: The task sequencer log is located at C:\MININT\SMSTSLog\SMSTS.LOG. For task sequence failures, please consult this log.]LOG]!><time="18:28:24.65360" date="01-24-2023" component="PSDPowerShell.ps1:47" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: tsenv:ScriptName powershell.exe -file C:\MININT\Cache\Scripts\Testscript.ps1]LOG]!><time="18:28:24.68560" date="01-24-2023" component="PSDPowerShell.ps1:50" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: tsenv:Parameters ]LOG]!><time="18:28:24.70060" date="01-24-2023" component="PSDPowerShell.ps1:51" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: About to run: ]LOG]!><time="18:28:25.21660" date="01-24-2023" component="PSDPowerShell.ps1:55" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">
<![LOG[PSDPowerShell.ps1: ArgumentList:  ]LOG]!><time="18:28:25.23260" date="01-24-2023" component="PSDPowerShell.ps1:56" context="CND-DPL-012\Administrator" type="1" thread="6112" file="PSDPowerShell.ps1">

However, your suggestion would have worked if I did use the "Run Commad Line" instead of "Run PowerShell Script" step.

What will work in the current version is this (using Upload-PSDLogs.ps1 instead of %SCRIPTROOT%\Upload-PSDLogs.ps1):

<step type="BDD_RunPowerShellAction" name="Upload-PSDLogs" description="" disable="false" continueOnError="true" successCodeList="0 3010">
  <defaultVarList>
	<variable name="ScriptName" property="ScriptName">Upload-PSDLogs.ps1</variable>
	<variable name="Parameters" property="Parameters"></variable>
	<variable name="PackageID" property="PackageID"></variable>
  </defaultVarList>
  <action>cscript.exe "%SCRIPTROOT%\ZTIPowerShell.wsf</action>
</step>

The old way (how it was done in MDT) however doesn't work in the current version:

<step type="BDD_RunPowerShellAction" name="Upload-PSDLogs" description="" disable="false" continueOnError="true" successCodeList="0 3010">
  <defaultVarList>
	<variable name="ScriptName" property="ScriptName">%SCRIPTROOT%\Upload-PSDLogs.ps1</variable>
	<variable name="Parameters" property="Parameters"></variable>
	<variable name="PackageID" property="PackageID"></variable>
  </defaultVarList>
  <action>cscript.exe "%SCRIPTROOT%\ZTIPowerShell.wsf</action>
</step>

The only difference between those two is %SCRIPTROOT%\ in the path. This might confuse users because in MDT you would provide the whole path to the script instead of the file name only.

ASlowTurtle avatar Jan 24 '23 17:01 ASlowTurtle

Hi @ASlowTurtle,

Thank you for your detailed report, it helps a lot to fix issues. I have to discuss this with the team and come back with answers and fixes, you'll get a notification from GitHub when this issue thread is updated.

Kind regards

GeoSimos avatar Jan 31 '23 23:01 GeoSimos

I just came across this issue myself and as there seems to be no real solution for it, I made my own one by extending the PSDPowershell.ps1 script to my needs.

I have added handling of full paths and relative script paths defined in the task sequence:

  • Full script paths are used as they are defined
  • Relative path are built to full paths based on the current %SCRIPTROOT%
  • Everything else is handled the same way as before

The script will throw a terminating error if the name of the script to call is empty or the defined script cannot be found. I also made the script pass the exit code of it´s child script to the calling process, so one can define success codes as usual.

Last thing I changed: I do not want the window of the new Powershell process to be visible in WinPE, unless $PSDDebug is $true, so the script also handles this.

Maybe someone else finds this additions useful. Please feel free to use the modified script in any way you like, as long as it satisfies the terms of the original license.

Regards

PSDPowerShell.zip

daooze avatar Jan 27 '25 13:01 daooze