Test-UiaControlState Output Issue

Sep 21, 2015 at 7:33 PM
Edited Sep 21, 2015 at 7:34 PM
I'm trying to do something based on the output of Test-UiaControlState, but the output of Test-UiaControlState seems to be formatted strangely or something.

I have something like:
$exists = Get-UiaWindow -Seconds 30 -Name 'Microsoft OneDrive for Business' | Test-UiaControlState -SearchCriteria @{automationId="1606";class="static";name="*";}

if($exists -eq 'False')
{
     echo "Tested False";
}
else 
{
    echo "Tested True";
}
I tested the value of $exists and it outputs 'False' as expected; however, the if/else test outputs 'Tested True'.

Any ideas what is going on or what I'm doing wrong?

Thanks!
Sep 21, 2015 at 7:38 PM
Note:

Just tried changing
if($exists -eq 'False')
to
if($exists -like 'False')
and now the if/else outputs 'Tested False' like it should, so it seems there is indeed something about the Test-UiaControlState output that isn't 'Pure' - though I have no idea what. I'll just use -like for now, but would be interested to know if there is something else I should be doing or why it acts like that.
Coordinator
Sep 23, 2015 at 12:26 PM
Edited Sep 23, 2015 at 12:27 PM
Hi xevilrobotx, you invented a very rare way of misuse. Why do you use strings for comparison instead of native boolean? 'True' and 'False' are string representation of values of boolean data, no matter PowerShell or C#.
We have embedded variables in PowerShell, $true and $false, and we also can use expressions that return boolean results for conditions. Here is an example:
$button1State = Start-Process calc -PassThru | Get-UiaWindow | Test-UiaControlState -SearchCriteria @{name="1"}
$button1state.GetType()

# as can be seen, the result is Boolean, so that we can use the result directly
if ($button1state) {
    Write-Host "Button 1 is here"
} else {
    Write-Host "Button 1 is out now"
}

# or we can compare the result with some boolean value
if ($button1state -eq $true) {
    Write-Host "Button 1 is here"
} else {
    Write-Host "Button 1 is out now"
}

# we even can put the whole one-liner into our 'if', just after decorating it with parentheses:
if ((Start-Process calc -PassThru | Get-UiaWindow | Test-UiaControlState -SearchCriteria @{name="1"})) {
    Write-Host "Button 1 is here"
} else {
    Write-Host "Button 1 is out now"
}

# sure, we can write something like if ($true -eq (expression)) {} and sometimes this is also useful
# it's sooner for style and readabilty as the result is the same

# we can also use search by a wider rule:
$allButtonsState = Start-Process calc -PassThru | Get-UiaWindow | Test-UiaControlState -SearchCriteria @{class="button"}
$allButtonsState.GetType()
if ($allButtonsState) {
    Write-Host "there are some buttons shown on the window"
}
Sep 23, 2015 at 2:10 PM
Thank you!!

Now it makes sense to me :)

I just started with PowerShell/C#/Scripting a couple of weeks ago and am very new to all of this. My boss said I needed to come up with a way to deploy OneDrive for Business to our staff and make it sync without them having to do anything... so here I am.

I very much appreciate the existence of UIAutomation and the help you have given me to work through this.