%WINDIR%\system32\inetsrv\appcmd.exe set site /site.name:"WebRole_IN_0_CB" /[Path='/'].applicationPool:"ASP.NET v4.0" >>CBLog.log
powershell.exe -command Set-ExecutionPolicy Unrestricted
powershell.exe .\CheckIfSuccessful.ps1
I would like to run the script above and if appcmd.exe can't execute it due the WebSite not yet being up and ready I would get the following error message in the CBLog.log file.
ERROR ( message:Cannot find SITE object with identifier "WebRole_IN_0_CB". )
In the CheckIfSuccessful.ps1 I would like to create a Powershell script to loop, which runs the appcmd command and checks the error again. Then sleeps for 5 seconds and then retries, until it succeeds.
How would I do this in Powershell?
highly appreciated,
UPDATE:
Ok, many thanks for the tip with $lastexitcode, while it looks promising. It seems I have problems with converting single quoates in Powershell: (The below is part of my initial appcmd command)
/[Path='/'].applicationPool:"ASP.NET v4.0"
How do I quote this in powershell? I tried to simplify it so that powershell has less trouble with it, but it seems my command is now wrong, as it says now:
ERROR ( message:Cannot find SITE object with identifier "v4.0". )
& $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" >CBLog.log
if($lastexitcode -ne '0')
{
while($lastexitcode -ne '0')
{
Start-Sleep -s 5
& $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" >CBLog.log
}
}
UPDATE II: I have escaped it properly. But still I get the error message. If I run the appcmd alone after creating that site, the log file is ok with it. Any suggestions please?
In PowerShell Do Until loop is used when we want to repeatedly execute statements as long as the condition is false. Like the Do-While loop, Do Until loop always runs at least once before the condition is evaluated in PowerShell.
PowerShell supports several operators and cmdlets that can be used to wait for the command to finish. In this regard, we have experienced the working of cmdlets such as Wait-Process and Start-Sleep. We have also presented the functionality of the Timeout and -Wait parameters.
The special variable $lastexitcode
gives you the exit code of the last native executable (i.e. not cmdlet) that was run. This will have different values depending on the results of the operation. For example, it will be 5 if appcmd encounters access denied. It should be 0 if the last command completed successfuolly.
Make sure you check this variable immediately after you run appcmd and in the same script. If not, you risk getting the exitcode from powershell.exe and not from appcmd.exe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With