# Flow control when invoking PowerShell with complex expressions using ScriptBlock

**URL:** <https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172>\
**Category:** PowerShell Help\
**Created:** [April 15, 2020, 5:13pm UTC](https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172 "2020-04-15T17:13:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 15, 2020, 5:13pm UTC](https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172/1 "2020-04-15T17:13:04Z")

</div>

Hi,

I am able to use PowerShell as described in the blog post:

[https://devblogs.microsoft.com/powershell/invoking-powershell-with-complex-expressions-using-scriptblocks/](https://devblogs.microsoft.com/powershell/invoking-powershell-with-complex-expressions-using-scriptblocks/)

It seems flow control statements (if-then-else, etc.) are ignored as there is no evidence that the are executed. The script blocks do do execute (all) the single line powershell expressions, but not flow control.

We are using PowerShell from an external program via the .Net Process class where we push a script block (see below) via standard input. The script creates a “inner script Block”, converts to a encoded base 64 and launches a new elevated PowerShell process. In both the inner and outer script blocks the flow control statements are ignored.

Can anybody suggest why?

&nbsp;

Thanks.

Here is a simplified script:

```
### PowerShell Script Service Install
$innerScriptBlock = {
$scriptOutput = @{}
$outputFileName = "tempFileName.xml"
$serviceName = "SomeWindowsService"
$serviceDisplay = "Some Windows Service"
$serviceDescription = "Not some other service"
$servicePath = "c:\bin\someservice.exe"
$serviceAccount = "MachineName\DomainServiceAccount"
$serviceStartupType = "Manual"
$machineName = "MachineName"

$sspw = ConvertTo-SecureString "ignoreThisPassword" -asPlainText -Force

if ($serviceAccount -eq "LocalSystem")
{
     $serviceCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "$machineName\LocalSystem",$sspw
}
else
{
     $serviceCredentials = Get-Credential -UserName $serviceAccount -Message "Enter Service Credentials"
     if ($? -eq $false )
     {
          $outLog += "Get Creds failed`n"
     }
}

$catchOutput = New-Service -Name $serviceName -BinaryPathName $servicePath -StartupType $serviceStartupType -Credential $serviceCredentials -DisplayName $serviceDisplay -Description $serviceDescription
if ($? -eq $false )
{
     $outLog += "Service failed`n"
}
else
{
     $outLog += "Service created:`n "
}
$scriptOutput["OutputLog"] = $outLog
$scriptOutput | ConvertTo-Xml -as string | Set-Content -Path $outputFileName
#### end inner script block
}

$bytes = [System.Text.Encoding]::Unicode.GetBytes($innerScriptBlock)
$innerBlockEncoded = [Convert]::ToBase64String($bytes)
$p = New-Object -TypeName System.Diagnostics.Process
$p.StartInfo.FileName = "PowerShell.exe"
$p.StartInfo.WorkingDirectory = "c:\MyWorkingDir"
$p.StartInfo.Verb = "runas"
$p.StartInfo.UseShellExecute = $true
##$p.StartInfo.WindowStyle = 1
$p.StartInfo.Arguments = "-EncodedCommand $innerBlockEncoded"
$ret_value = $p.Start()
$ret_value = $p.WaitForExit(120)

$t = 0
while (( $t -le 10000) -and (-not (Test-Path "tempFileName.xml")) )
{
    Start-Sleep -Milliseconds 100
    $t += 100
}

$script_out = Get-Content -Path "tempFileName.xml" -force
$script_out | Write-Output
Remove-Item -Path "tempFileName.xml" -force
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [April 15, 2020, 5:17pm UTC](https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172/2 "2020-04-15T17:17:43Z")

</div>

second attempt to format code - edited the main post instead -

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 15, 2020, 6:01pm UTC](https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172/3 "2020-04-15T18:01:05Z")

</div>

April,

with a quick overlook I see some minor issues …

on line 16 you are actually comparing “ **MachineName\DomainServiceAccount** ” to “ **LocalSystem** ”. What result would you expect from this comaprison? 😉

on the lines 23 and 30 you check if the last command has been completed successfully. As this commands are variable assignments they will likely always be successful. BTW: Because **$?** returns a **boolean** it looks better like this “**if ( -not $?)**”

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/flow-control-when-invoking-powershell-with-complex-expressions-using-scriptblock/14172/4 "2024-05-16T20:29:56Z")

</div>


