Search code examples
powershelldsc

DscTemplate isn't executing my SetScript


$Launcher_PackageFile = "Launcher.exe"
$Launcher_PackageDestinationDirectory = "$Env:ProgramData\Launcher";
#$Launcher_StartupDirectory = (New-Object -ComObject Shell.Application).NameSpace(0x07)
$Launcher_Source = "$Env:PackageRoot\Launcher\$Launcher_PackageFile" 

Script UpdateConfigurationVersion
{
    GetScript = { 
        # For cmdlet. Must always return a hash table.
        return @{
            "Name"="LauncherPackage"
            "Version"="Latest"
        };
    }          
    TestScript = {
        # Check if the file is in the folder
        $TestPath = Join-Path $Launcher_PackageDestinationDirectory $Launcher_PackageFile

        if (Test-Path $TestPath) {
            return $true;
        }

        return $false;
    }
    SetScript = { 
        # Logic
        #move the exe
        if (!Test-Path $Launcher_PackageDestinationDirectory) {
            New-Item -Type Directory -Path $Launcher_PackageDestinationDirectory
        }

        Copy-Item -Path $Launcher_Source -Destination $Launcher_PackageDestinationDirectory 

        #TODO Create a shortcut to the startup directory 
        #or create a task in the scheduler
    }
}

Solution

  • Incorrect syntax (!Test-Path

    if (!(Test-Path $Launcher_PackageDestinationDirectory)) {....
    

    or

    if (-not(Test-Path $Launcher_PackageDestinationDirectory) {....
    

    Also add -ErrorAction SilentlyContinue to your Test-Path command to suppress error.