简体   繁体   中英

PowerShell to get .NET framework version that a site is running in IIS

Is there a way to use PowerShell to get the .NET framework that a web application is running on in IIS?

I have been able to use the following to get the website name and to see that the app pool is set up as "Clr4IntegratedAppPool," but I am not seeing a way to determine what version of .NET the site itself is running.

To clarify, I am trying to get the .NET version of the site itself, not the app pool. For example if Site1 is running version 4.6.2 under AppPool1 (which is set to 4.0) then I am trying to get to the 4.6.2.

[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")

$sm = New-Object Microsoft.Web.Administration.ServerManager

foreach($site in $sm.Sites)
{
    $root = $site.Applications | where { $_.Path -eq "/" }
    Write-Output ("Site: " + $site.Name + " | Pool: " + $root.ApplicationPoolName )
}

Here is a way to do it

$myServer = "name of server"

$IIS = [Microsoft.Web.Administration.ServerManager]::OpenRemote($myServer)

foreach ($pool in $IIS.ApplicationPools)
{
    $pool.ManagedRuntimeVersion
}

You can go to the bin folder of the site to iterate all dlls and identify the .NET framework version of those dlls.

Function Get-WebsiteDotNetVersion { 
    [CmdletBinding()] 
    Param 
    ( 
        [Parameter(Mandatory=$false)][String]$SiteName 
    ) 

    # get site root directory 
    $site = Get-WebSite -Name $SiteName 
    $binLocation = "$($site.physicalPath)\bin" 

    # get all dlls in bin folder 
    $dllFolder = Get-Item -Path $binLocation 
    $dlls = $dllFolder.GetFiles("*.dll") 

    # analyze dll .net version 
    $set = New-Object System.Collections.Generic.HashSet[String] 
    $dlls | ForEach-Object { 
        $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom("$binLocation\$($_.Name)").ImageRuntimeVersion) | Out-Null 
    } 

    # print all dll .NET version 
    $set 
} 

For details please refer to How to get .NET framework version of site running in IIS by PowerShell

An asp.net application looks at target framework attribute to find out which version of .Net to use

<httpRuntime targetFramework="4.5" />

For more details refer this msdn .If it does not have that attribute set,It will use runtime 4.0

So to correctly figure out the site version, you need

  • Get the Application pool runtime version.
  • Go to the web.config file and check if targetFramework is present.
    • If targetFramework is present ,take that
    • If not,use the Application pool . Runtime

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM