【问题标题】:DscTemplate isn't executing my SetScriptDscTemplate 没有执行我的 SetScript
【发布时间】:2017-12-13 14:39:25
【问题描述】:
$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
    }
}

【问题讨论】:

    标签: powershell dsc


    【解决方案1】:

    语法错误 (!Test-Path

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

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

    还将-ErrorAction SilentlyContinue 添加到您的Test-Path 命令以抑制错误。

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    有了这些信息就很难分辨了。

    向 TestScript 和 SetScript 添加消息以查看发生了什么。例如:

    Write-Verbose -Message 'Testing if already exists'
    

    TestScript 在 SetScript 之前执行,检查是返回 true 还是 false(通过添加消息)。 TestScript 可能返回 true,因此 SetScript 未运行。输出$TestPath,看看路径是否正确。

    然后查看 C:\Windows\System32\Configuration\ConfigurationStatus 上的日志

    【讨论】:

    • 感谢您给我一种将输出发送到控制台的方法 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多