git 如何使用powershell安装和配置IIS、SSL证书、urlrewrite、git和克隆仓库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25288161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use powershell to install and configure IIS, SSL certificate, urlrewrite, git and clone repository
提问by Liam Wheldon
I'm currently setting up auto scaling IIS webservers and need to automatically install and configure the following through a powershell script:
我目前正在设置自动缩放 IIS 网络服务器,需要通过 powershell 脚本自动安装和配置以下内容:
- IIS
- URLRewrite
- Import SSL certificate
- Configure a new website
- Add new SSL bindings
- Download my source code from a GIT repository
- 信息系统
- 网址重写
- 导入 SSL 证书
- 配置新网站
- 添加新的 SSL 绑定
- 从 GIT 存储库下载我的源代码
Regards
问候
Liam
利亚姆
回答by Liam Wheldon
I just thought I'd share a powershell script that I put together with you all as I came across a situation with AWS ELB where I needed to install IIS, URL rewrite, git and clone the repository.
我只是想与大家分享一个 powershell 脚本,当我遇到 AWS ELB 的情况时,我需要安装 IIS、URL 重写、git 和克隆存储库。
echo "Installing web-webserver"
powershell.exe add-windowsfeature web-webserver -includeallsubfeature -logpath $env:temp\webserver_addrole.log
echo "Installing web-mgmt-tools"
powershell.exe add-windowsfeature web-mgmt-tools -includeallsubfeature -logpath $env:temp\mgmttools_addrole.log
echo "Creating C:\inetpub\wwwroot\example.com\"
$TestApplicationroot = Test-Path C:\inetpub\wwwroot\example.com
if (! $TestApplicationroot) {
mkdir C:\inetpub\wwwroot\example.com
}
echo "GIT: Installing Chocolatey"
(new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1') | iex
echo "GIT: Installing Git"
cinst git
echo "GIT: Setting enviroment path"
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"
echo "GIT: Installing poshgit"
cinst poshgit
echo "GIT: Installing UrlRewrite"
cinst UrlRewrite
echo "GIT: Installing git-credential-winstore"
cinst git-credential-winstore
.\CredMan.ps1 -AddCred -Target 'git:https://gitrespos.org' -User 'TestApplication' -Pass 'TestApplicationPassword'
echo "GIT: Cloning TestApplication1 code"
cd C:\inetpub\wwwroot\example.com\
git clone "https://gitrespos.org/Username/TestApplication1.git"
import-module webadministration
echo "Creating new website"
new-website -name "example.com" -port 80 -physicalpath c:\inetpub\wwwroot\example.com -ApplicationPool ".NET v4.5" -force
Echo "Importing SSL certificate"
$mypwd = ConvertTo-SecureString -String "SSLCertificate password" -Force –AsPlainText
Import-PfxCertificate –FilePath .\certificate.pfx cert:\localMachine\my -Password $mypwd
New-WebBinding -Name "example.com" -IP "*" -Port 443 -Protocol https
echo "Assigning SSL certificate"
cd IIS:\SslBindings
$cert = Get-Item cert:\LocalMachine\My\THUMB-OF-SSL-CERTIFICATE
$cert |New-Item 0.0.0.0!443
echo "Adding application pools TestApplication1"
New-Item 'IIS:\Sites\example.com\TestApplication1' -physicalPath "C:\inetpub\wwwroot\example.com\TestApplication1" -type Application
echo "Removing Default Web Site"
remove-website -name "Default Web Site"
Start-Sleep -s 10
echo "Starting example.com website"
start-website -name "example.com"
You can download CredMan.ps1 from the following link http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde
您可以从以下链接下载 CredMan.ps1 http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Credentials-d44c3cde
You'll need to first find the Thumb of your certificate on a server by running the following in powershell and note down the Thumbprint as it'll be the same on every server you import the certificate to.:
您需要首先通过在 powershell 中运行以下命令在服务器上找到您的证书的 Thumb 并记下 Thumbprint,因为它在您将证书导入到的每台服务器上都是相同的。:
get-ChildItem cert:\LocalMachine\My
I hope that this is of help to some of you as it's taken me days to come up with having hit different issues along the way.
我希望这对你们中的一些人有所帮助,因为我花了几天时间才想出一路上遇到的不同问题。
Regards
问候
Liam
利亚姆
回答by Aurel Havetta
Here is the complete code, import pfx, add iis website, add ssl binding:
完整代码如下,导入pfx,添加iis网站,添加ssl绑定:
$certPath = 'c:\cert.pfx'
$CertificatePassword = '1234'
$SiteName = "MySite"
$HostName = "localhost"
$SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName
Write-Host 'Import pfx certificate' $certPath
$certRootStore = “LocalMachine”
$certStore = "My"
$pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import($certPath,$CertificatePassword,"Exportable,PersistKeySet")
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore)
$store.Open('ReadWrite')
$store.Add($pfx)
$store.Close()
$certThumbprint = $pfx.Thumbprint
Write-Host 'Add website' $SiteName
New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force
$IISSite = "IIS:\Sites$SiteName"
Set-ItemProperty $IISSite -name Bindings -value @{protocol="https";bindingInformation="*:443:$HostName"}
if($applicationPool) { Set-ItemProperty $IISSite -name ApplicationPool -value $applicationPool}
Write-Host 'Bind certificate with Thumbprint' $certThumbprint
$obj = get-webconfiguration "//sites/site[@name='$SiteName']"
$binding = $obj.bindings.Collection[0]
$method = $binding.Methods["AddSslCertificate"]
$methodInstance = $method.CreateInstance()
$methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
$methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
$methodInstance.Execute()
回答by Aaron Jensen
Check out the Carbonmodule (disclaimer: I am the owner/maintainer of Carbon). It has functions for doing the IIS and SSL configuration and installing MSIs. Sadly, you'll have to figure out Git yourself. (I recommend switching to Mercurial. :-)
)
查看Carbon模块(免责声明:我是 Carbon 的所有者/维护者)。它具有进行 IIS 和 SSL 配置以及安装 MSI 的功能。遗憾的是,您必须自己弄清楚 Git。(我建议切换到 Mercurial。:-)
)
Good luck!
祝你好运!