Assign Get-WebAppPoolState return value to variable in Powershell

This code:

import-module WebAdministration

Get-WebAppPoolState AppPoolName

It produces the following output:

Value - -
Stopped

But this code:

import-module WebAdministration

$ state = Get-WebAppPoolState AppPoolName

WRITE-HOST $ state

Produces this conclusion:

Microsoft.IIs.PowerShell.Framework.CodeProperty

When I get the status of an application pool using Get-WebAppPoolState, I need some type of boolean to assign a variable so that I can use it in a conditional expression.

I cannot use the string Microsoft.IIs.PowerShell.Framework.CodeProperty.

How to fix it?

+4
source share
3 answers

Get-WebAppPoolState does not return a string, but an object of type CodeProperty. You will need the Value property from this object, that is:

$state = (Get-WebAppPoolState AppPoolName).Value; 

I assume that some kind of converter displays the first case when it is written to the output, therefore Stopped is displayed, but not for writing to the hosting, so instead you get the default representation of the object (which is the type name).

+12
source

Not tested, but does it work better?

 $state = $(Get-WebAppPoolState AppPoolName) 
0
source

Another approach is to use the Select-Object cmdlet with ExpandProperty to get the value of 1 or more object properties.

 $pool = "app-pool-name" $state = Get-WebAppPoolState $pool | Select -ExpandProperty Value Write-Host $state 
0
source

All Articles