IIS question

This topic contains 4 replies, has 3 voices, and was last updated by  Robert Westerlund 3 years, 3 months ago.

  • Author
    Posts
  • #15229

    Nick Chard
    Participant

    Hi,

    I have a question about some of the PowerShell Commands in the WebAdministration module.

    When I run the command get-website I get a list of all my websites, and the first column is headed "Name", so to get the website name and physical path I run this command:
    Get-Website | select-object -Property Name,PhysicalPath

    When I run the command Get-WebApplication I get a list of all my applications, and again the first column is headed Name and contains the application name. When I run this command:
    Get-WebApplication | select-object -Property Name,PhysicalPath
    The Name column is empty

    It appears that a Name property does not exist for Get-WebApplication, as confirmed when I run:
    Get-WebApplication | get-member

    So how can I get the Name property of an application? It must be possible, because it is returned by running the Get-WebApplication command on its own.

    Thanks
    Nick

  • #15234

    Anonymous

    Hi Nick,

    This stumped me too when I first started messing with the IIS commandlets. Please refer to this blog post for more guidance.

    http://nisanthkv.blog.com/2012/07/06/name-of-web-applications-using-powershell/

    basically you have to foreach through the object returned since the Name is a deeper attribute of the object being returned.


    $foo = get-webapplication
    foreach($a in $foo) { echo $a.Attributes[0].Value}

    Eric

  • #15235

    Robert Westerlund
    Participant

    The Name column which is displayed for the items returned by the Get-WebApplication cmdlet is not an immediate property on the returned object. The Name displayed there is defined in the "iisprovider.format.ps1xml" file which should be located in your "$pshome\modules\WebAdministration\iisprovider.format.ps1xml" folder.

    In that file, for the entry with the name "applicationElementView" and the typename "Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application" I can see that the header "Name" is the first header and the first column is defined as a ScriptBlock (the other columns are defined by immediate PropertyName accessors).

    The ScriptBlock for the name column contains the following script:

    $name = $_.Path.Trim('/')
    $name

    So you need to either do that trimming yourself or add a ScriptProperty which does it for you. The following script seems to work for me:

    $apps = Get-WebApplication | Add-Member -MemberType ScriptProperty -Name ApplicationName -Value { $this.Path.Trim('/') } -PassThru
    $apps | select ApplicationName

    Or, of course, you could just skip adding the ScriptProperty and just do the trimming when you want the application name.

  • #15236

    Robert Westerlund
    Participant

    By the way, you can read up more about how the default display of types are handled in PowerShell if you run the following command:

    Get-Help about_Format.ps1xml
  • #15237

    Robert Westerlund
    Participant

    If you have PowerShell 3 and above I believe you should be able to do something like the following, if you want to add it to all items of the type "Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application".

    Update-TypeData -MemberName "ApplicationName" -MemberType ScriptProperty -Value {$this.Path.Trim("/")} -TypeName "Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application"

    This adds the scriptproperty to all objects of that type, instead of the above mentioned version which only adds it to the objects you pass to Add-Member.

    After you have run the above command, you should be able to do:

    Get-WebApplication | Select ApplicationName

    I have not been able to test that above exact code, since I don't have access to a computer with the WebAdministration module and PowerShell version above 2. I do believe it should work just fine, given that I don't have any spelling errors or similar (similar code worked on the System.Diagnostics.Process type on my computer, but I don't currently have access to a computer with PowerShell version above 2 which also has the WebAdministration module, so I can't test it at the moment).

    Be aware that the property is only added for objects of that type in that session. If you start a new PowerShell window, you'll have to add the type again, so if you want to always have that property on your objects, you need to add that script to your profile.

You must be logged in to reply to this topic.