开发者

Assign a Get-WebAppPoolState returned value to a variable in Powershell

开发者 https://www.devze.com 2023-03-16 14:54 出处:网络
This code: import-module WebAdministration Get-WebAppPoolState AppPoolName Produces the following output:

This code:

import-module WebAdministration

Get-WebAppPoolState AppPoolName

Produces the following output:

Value - -

Stopped

But this code:

import开发者_运维知识库-module WebAdministration

$state = Get-WebAppPoolState AppPoolName

WRITE-HOST $state

Produces this output:

Microsoft.IIs.PowerShell.Framework.CodeProperty

When I get the state of the App Pool using Get-WebAppPoolState, I need a boolean value of some sort to assign to the variable so I can use it in a conditional statement.

I cant use the Microsoft.IIs.PowerShell.Framework.CodeProperty line.

How do I correct this?


Get-WebAppPoolState is not returning a string but an object of type CodeProperty. You'll want the Value property from that object, i.e.:

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

I presume some display converter is kicking in the first case when it gets written to output which is why Stopped is displayed but not for writing to host so you get the default object representation (which is the type name) instead.


Not tested, but does this work better?

$state = $(Get-WebAppPoolState AppPoolName) 


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号