0
votes

Question) How do I get a DSC script resource to wait until the code has completed before moving on? (The code is invoke-expression "path\file.exe")

Details) I am using powershell version 5 and am trying to get DSC setup to handle our sql server installations. My manager has asked me to use the out of the box DSC components. i.e. no downloading of custom modules which may help. I have built up the config file that handles the base server build - everything is good. The script resource that installs sql server is good. It executes, and waits until it has installed completely, before moving on. When I get up to the script resource that installs the sql server cumulative update, I have issues. The executable gets called and it starts installing (it should take 10-15 minutes), but the dsc configuration doesn't wait until it has installed, and moves on after a second. This means that the DependsOn for future steps, gets called, before the installation is complete. How can I make the script resource wait until it has finished?

2

2 Answers

2
votes

Have you tried the keyword "DependsOn" like that ?

Script MyNewSvc
{
    GetScript = {
        $SvcName = 'MyNewSvc'

        $Results = @{}
        $Results['svc'] = Get-Service $SvcName

        $Results
    }
    SetScript = {
        $SvcName = 'MyNewSvc'

        setup.exe /param
        while((Get-Service $SvcName).Status -ne "Running"){ Start-Sleep 10 }
    }
    TestScript = {
        $SvcName = 'MyNewSvc'
        $SvcLog = 'c:\svc.log'

        If (condition) { #like a a running svc or a log file
            $True
        }
        Else {
            $False
        }

    }
}


WindowsFeature Feature
{
    Name = "Web-Server"
    Ensure = "Present"
    DependsOn = "[Script]MyNewSvc"
}
0
votes

Invoke-Expression doesn't seem to wait until the process has finished - try this in a generic PowerShell console and you'll see the command returns before you close notepad:

Invoke-Expression -Command "notepad.exe";

You can use Start-Process instead:

Start-Process -FilePath "notepad.exe" -Wait -NoNewWindow;

And if you want to check the exit code you can do this:

$process  = Start-Process -FilePath "notepad.exe" -Wait -NoNewWindow -PassThru;
$exitcode = $process.ExitCode;
if( $exitcode -ne 0 )
{
    # handle errors here
}

Finally, to use command line arguments:

$process  = Start-Process -FilePath "setup.exe" -ArgumentList @("/param1", "/param2") -Wait -PassThru;
$exitcode = $process.ExitCode;