直接看如下代码就好。
Import-Module WebAdministration
Write-Host "Checking IIS status..."
$iis = Get-Service W3SVC -ErrorAction Ignore
if($iis){
if($iis.Status -eq "Running") {
Write-Host "IIS Service is running"
}
else {
Write-Host "IIS Service is not running"
}
}
else {
Write-Host "Checking IIS failed, please make sure IIS is ready."
}
$aspNetCoreModule = Get-WebGlobalModule -Name AspNetCoreModule -ErrorAction Ignore
if($aspNetCoreModule)
{
Write-Host "IIS ASPNetCoreModule is ready:"
Write-Host $aspNetCoreModule.Name $aspNetCoreModule.Image
}
else
{
#download aspnetcoremodule
Invoke-WebRequest -Uri "https://download.visualstudio.microsoft.com/download/pr/34f4b2a6-c3b8-495c-a11f-6db955f27757/8c340c1a8c25966e39e0c0a4b308dff4/dotnet-hosting-2.2.5-win.exe" -OutFile "dotnet-hosting-2.2.5-win.exe"
Start-Process "dotnet-hosting-2.2.5-win.exe" -Wait -ArgumentList '/S', '/v', '/qn' -passthru
Write-Host "IIS ASPNetCoreModule is installed successfully"
}
$site =get-website|Where-Object {$_.Name -like '*mmcapi*'}
$siteName = $site.Name # xxx.com
$poolName =$site.Name
$physicalPath = $site.PhysicalPath #D:\Apps\xxx.com
Write-Host 'site name:' $sitename ', pool name:' $poolName ', physical path:' $physicalPath
# 把应用程序池的.Net版本设置为无托管代码
Set-ItemProperty -Path IIS:\AppPools\$poolName -Name managedRuntimeVersion -Value ''
# Set identity user
Set-ItemProperty IIS:\AppPools\$poolName -name processModel.identityType -Value SpecificUser
Set-ItemProperty IIS:\AppPools\$poolName -name processModel.userName -Value $username
Set-ItemProperty IIS:\AppPools\$poolName -name processModel.password -Value $password
get-ItemProperty -Path IIS:\AppPools\$poolName -Name processmodel
# restart IIS
Invoke-Expression "net stop was /y"
Invoke-Expression "net start w3svc"
Start-WebAppPool -Name $poolName
也可参考我的另一篇文章: