Question:
The first sorry for my english, but I need your help with powershell scripting.
I must config IIS server with powershell script and I have problem with “add application”. I need to check whether webapplication exist in site. I want
choose all webapplication in site “testsite2”
1 2 |
$webapplication=Get-WebApplication | Where-Object {$_. applicationpool -eq "testsite2"} | select -expand Name; |
But I have problem that command is ending with error
select : Property “Name” cannot be found.At line:1 char:75 Get-WebApplication | Where-Object {$_. applicationpool -eq “testsite2”} | select … CategoryInfo : InvalidArgument: (Microsoft.IIs.P…gurationElement:PSObject) [Select-Object], PSArgumentException FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
I don´t understand. What have I do when I want choose value from column “Name”?
I get same result
1 2 |
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq "testsite2"} | select -ExpandProperty applications |
result:
select : Property “applications” cannot be found.
At line:1 char:80
Get-ChildItem –Path IIS:\AppPools\ | Where-Object {$_. Name -eq “testsite2”} | s …
CategoryInfo : InvalidArgument: (Microsoft.IIs.P…gurationElement:PSObject) [Select-Object], PSArgumentException
FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand
Answer:
The WebAdministration
module can be a bit deceiving in that it uses custom formatting data to show stuff like Name
with Format-Table
(even though the underlying objects have no Name
property).
To get the equivalent Name
value, take the Path
property and trim /
:
1 2 |
$WebAppNames = Get-WebApplication |Select @{Name='Name';Expression={$_.Path.Trim('/')}} |Select -Expand Name |
The above is exactly what the formatting subsystem in PowerShell does to calculate the Name
value.
You can explore the actual properties an object has with the Get-Member
cmdlet:
1 2 |
Get-WebApplication |Get-Member |
This will also show you the type name, which you can use to explore any additional Type- or Format-Data that might apply:
1 2 3 |
Get-TypeData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application' Get-FormatData -TypeName 'Microsoft.IIS.PowerShell.Framework.ConfigurationElement#site#application' |