Use PowerShell to automatically deploy ASP. NetCore to IIS,

Source: Internet
Author: User

Use PowerShell to automatically deploy ASP. NetCore to IIS,

Windows PowerShell is a command line shell program and script environment that allows command line users and script writers to take advantage of the powerful functions of. NET Framework. For more information about PowerShell, refer to Baidu entry

Next, we will use PowerShell to automatically deploy the ASP. NetCore program.

Open PowerShell

Open the Start menu and enter PowerShell to search for it.

WebAdministration

First, we need to introduce the WebAdministration module in PowerShell to perform related operations on IIS.

Import-Module WebAdministration
DotNetCore. WindowsHosting

To run the ASP. NetCore program in IIS, you must first install DotNetCore. WindowsHosting. If you have already installed the program, skip this step.

Download DotNetCore. WindowsHosting and install it

Invoke-WebRequest -Uri "https://aka.ms/dotnetcore.2.0.0-windowshosting" -OutFile "DotNetCore.WindowsHosting.exe"Start-Process "DotNetCore.WindowsHosting.exe" -Wait

After installing DotNetCore. WindowsHosting, restart the IIS service.

Invoke-Expression "net stop was /y"Invoke-Expression "net start w3svc"

Run the following command to check if ASPNetCoreModule is installed:

Get-WebGlobalModule -Name AspNetCoreModule -ErrorAction Ignore
Program deployment and IIS settings

Deploy a program in IIS to perform two tasks: 1. Create an application pool, 2. Create a website and use this application pool.

Next, create an application pool named TestApp.

New-Item -path IIS:\AppPools\TestApp

Set the. Net version of the application pool to unmanaged code

Set-ItemProperty -Path IIS:\AppPools\TestApp -Name managedRuntimeVersion -Value ''

After creating an application pool, you need to create a website and use the newly created application pool TestApp to set the website name to TestSite and direct it to your website path, such as C: \ TestSite

New-Website -name TestSite -PhysicalPath "C:\TestSite" -ApplicationPool TestApp -Port 8080

Finally, you can open your browser to access it.

Invoke-Expression "cmd.exe /C start http://localhost:8080"
Complete script

The above are the key commands used to deploy IIS using PowerShell. Once you know this, you can write a complete automatic deployment script. Take ZKEACMS as an example. Create a file named ZKEACMS. ps1 and paste the following code into the file to save it.

This script contains IIS detection, DotNetCore. WindowsHosting detection and installation, online program download and decompression, and so on:

Add-Type -AssemblyName System.IO.Compression.FileSystemImport-Module WebAdministration$WebPath=[environment]::CurrentDirectory + "\ZKEACMS";Write-Host "Welcome to use ZKEACMS, visit our website(http://www.zkea.net) for more information"Write-Host "ZKEACMS will install to $WebPath"Write-Host "After installation, you can visit the site with http://localhost:8080"Write-Host "Installation started. Press Ctrl+C to stop."Write-Host "Checking IIS status..."$iis = Get-Service W3SVC -ErrorAction Ignoreif($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 Ignoreif($aspNetCoreModule){Write-Host "IIS ASPNetCoreModule is ready:"Write-Host $aspNetCoreModule.Name $aspNetCoreModule.Image }else{Write-Host "Downloading DotNetCore.WindowsHosting."if(Test-Path -Path "DotNetCore.WindowsHosting.exe"){Remove-Item -Path "DotNetCore.WindowsHosting.exe" -Force}Invoke-WebRequest -Uri "https://aka.ms/dotnetcore.2.0.0-windowshosting" -OutFile "DotNetCore.WindowsHosting.exe"Write-Host "Installing DotNetCore.WindowsHosting."Start-Process "DotNetCore.WindowsHosting.exe" -Waitif(Test-Path -Path "DotNetCore.WindowsHosting.exe"){Remove-Item -Path "DotNetCore.WindowsHosting.exe" -Force}}Write-Host "Downloading ZKEACMS application package."if(Test-Path -Path "ZKEACMS.zip"){Remove-Item -Path "ZKEACMS.zip" -Force}Invoke-WebRequest -Uri "http://cdn.zkeasoft.com/core/cms.zip" -OutFile "ZKEACMS.zip"Write-Host "Unzip ZKEACMS application package."if(Test-Path "ZKEACMS"){Remove-Item -Path "ZKEACMS" -Force -Recurse}[System.IO.Compression.ZipFile]::ExtractToDirectory("ZKEACMS.zip" ,"ZKEACMS")Write-Host "Setting up IIS."if(!(Test-Path IIS:\AppPools\ZKEACMS)){New-Item -path IIS:\AppPools\ZKEACMS}Set-ItemProperty -Path IIS:\AppPools\ZKEACMS -Name managedRuntimeVersion -Value ''if(Test-Path IIS:\Sites\ZKEACMS){Remove-Website ZKEACMS}New-Website -name ZKEACMS -PhysicalPath $WebPath -ApplicationPool ZKEACMS -Port 8080Invoke-Expression "net stop was /y"Invoke-Expression "net start w3svc"Invoke-Expression "cmd.exe /C start http://localhost:8080"if(Test-Path -Path "ZKEACMS.zip"){Remove-Item -Path "ZKEACMS.zip" -Force}Write-Host "ZKEACMS installed successfully."
Execute scripts

The script execution method is simple. You can directly use the Script Name on the PowerShell Console (note the path)

./ZKEACMS.ps1

Alternatively, you can right-click the script file and choose "Run with PowerShell" from the menu.

If your script is on the network, you can run it directly on the console using the following script instead of downloading it. For example, you can use the following script to install ZKEACMS online:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "&([scriptblock]::Create((Invoke-WebRequest -useb 'http://cdn.zkeasoft.com/core/win-zkeacms.ps1')))"

Address: http://www.zkea.net/codesnippet/detail/post-87

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.