windowspowershellenterprise

PowerShell - Setting working directory for SetCustomShell ShellLauncher


I'm attempting to set up a kiosk by setting the SetCustomShell via PowerShell.

Fetching the class instance by

$ShellLauncherClass = [wmiclass]"\\$COMPUTER\${NAMESPACE}:WESL_UserSetting"

and setting the custom shell by

$ShellLauncherClass.SetCustomShell($Kiosk_SID, "C:\Path\To\Executable.exe", ($null), ($null), 0)

Are there any ways to set the working directory for the executable within the SetCustomShell function? Would there be any way to set this on the preferences of the executable either? Or setting the user's working directory to the program directory?

With it configured as above the program will not function properly, but if Start-Process -FilePath C:\Path\To\Executable.exe -WorkingDirectory C:\Path\To is used the program launches as expected.

Any advice?


Solution

  • Really isn't the solution I was going for, but in order to achieve this I've set up a PowerShell script that acts as a wrapper for the program. Checking every 20 seconds to see if the software has closed and needs to be opened again.

    for(;;){
        try{
            if (!(Get-Process -Name Executable -ErrorAction SilentlyContinue)){
                Start-Process -FilePath "C:\Path\To\Executable.exe" -WorkingDirectory "C:\Path\To"
                }
            $proc = Get-Process -Name Executable | Sort-Object -Property ProcessName -Unique -ErrorAction SilentlyContinue
            if (!$proc -or ($proc.Responding -eq $false)) {
                $proc.Kill()
                Start-Sleep -s 10
                Start-Process -FilePath "C:\Path\To\Executable.exe" -WorkingDirectory "C:\Path\To"
                }
        }
        catch    {    }
        Start-sleep -s 20
    }
    

    This script is then set with $ShellLauncherClass.SetCustomShell($User_SID, "powershell.exe -WindowStyle hidden -file C:\Path\To\Script.ps1", ($null), ($null), 0) to replace the default shell for that user.