2015-05-13 05:35:08 +00:00
|
|
|
# Configure a Windows host for remote management with Ansible
|
2014-11-11 07:20:28 +00:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
#
|
|
|
|
# This script checks the current WinRM/PSRemoting configuration and makes the
|
|
|
|
# necessary changes to allow Ansible to connect, authenticate and execute
|
|
|
|
# PowerShell commands.
|
2015-09-14 21:47:44 +00:00
|
|
|
#
|
2014-11-11 07:20:28 +00:00
|
|
|
# Set $VerbosePreference = "Continue" before running the script in order to
|
|
|
|
# see the output messages.
|
2015-09-22 18:57:15 +00:00
|
|
|
# Set $SkipNetworkProfileCheck to skip the network profile check. Without
|
|
|
|
# specifying this the script will only run if the device's interfaces are in
|
|
|
|
# DOMAIN or PRIVATE zones. Provide this switch if you want to enable winrm on
|
|
|
|
# a device with an interface in PUBLIC zone.
|
2014-07-06 17:02:32 +00:00
|
|
|
#
|
|
|
|
# Written by Trond Hindenes <trond@hindenes.com>
|
2014-11-11 07:20:28 +00:00
|
|
|
# Updated by Chris Church <cchurch@ansible.com>
|
2015-05-13 05:35:08 +00:00
|
|
|
# Updated by Michael Crilly <mike@autologic.cm>
|
2014-07-06 17:02:32 +00:00
|
|
|
#
|
|
|
|
# Version 1.0 - July 6th, 2014
|
2014-11-11 07:20:28 +00:00
|
|
|
# Version 1.1 - November 11th, 2014
|
2015-05-13 05:35:08 +00:00
|
|
|
# Version 1.2 - May 15th, 2015
|
2014-07-06 17:02:32 +00:00
|
|
|
|
|
|
|
Param (
|
|
|
|
[string]$SubjectName = $env:COMPUTERNAME,
|
|
|
|
[int]$CertValidityDays = 365,
|
2015-09-14 21:47:44 +00:00
|
|
|
[switch]$SkipNetworkProfileCheck,
|
2014-07-06 17:02:32 +00:00
|
|
|
$CreateSelfSignedCert = $true
|
|
|
|
)
|
|
|
|
|
|
|
|
Function New-LegacySelfSignedCert
|
|
|
|
{
|
|
|
|
Param (
|
|
|
|
[string]$SubjectName,
|
|
|
|
[int]$ValidDays = 365
|
|
|
|
)
|
2015-09-14 21:47:44 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
$name = New-Object -COM "X509Enrollment.CX500DistinguishedName.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$name.Encode("CN=$SubjectName", 0)
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
$key = New-Object -COM "X509Enrollment.CX509PrivateKey.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$key.ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
|
|
|
|
$key.KeySpec = 1
|
|
|
|
$key.Length = 1024
|
|
|
|
$key.SecurityDescriptor = "D:PAI(A;;0xd01f01ff;;;SY)(A;;0xd01f01ff;;;BA)(A;;0x80120089;;;NS)"
|
|
|
|
$key.MachineContext = 1
|
|
|
|
$key.Create()
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
$serverauthoid = New-Object -COM "X509Enrollment.CObjectId.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$serverauthoid.InitializeFromValue("1.3.6.1.5.5.7.3.1")
|
2014-11-11 07:20:28 +00:00
|
|
|
$ekuoids = New-Object -COM "X509Enrollment.CObjectIds.1"
|
|
|
|
$ekuoids.Add($serverauthoid)
|
|
|
|
$ekuext = New-Object -COM "X509Enrollment.CX509ExtensionEnhancedKeyUsage.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$ekuext.InitializeEncode($ekuoids)
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
$cert = New-Object -COM "X509Enrollment.CX509CertificateRequestCertificate.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$cert.InitializeFromPrivateKey(2, $key, "")
|
|
|
|
$cert.Subject = $name
|
|
|
|
$cert.Issuer = $cert.Subject
|
2014-11-11 07:20:28 +00:00
|
|
|
$cert.NotBefore = (Get-Date).AddDays(-1)
|
2014-07-06 17:02:32 +00:00
|
|
|
$cert.NotAfter = $cert.NotBefore.AddDays($ValidDays)
|
|
|
|
$cert.X509Extensions.Add($ekuext)
|
|
|
|
$cert.Encode()
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
$enrollment = New-Object -COM "X509Enrollment.CX509Enrollment.1"
|
2014-07-06 17:02:32 +00:00
|
|
|
$enrollment.InitializeFromRequest($cert)
|
|
|
|
$certdata = $enrollment.CreateRequest(0)
|
|
|
|
$enrollment.InstallResponse(2, $certdata, 0, "")
|
|
|
|
|
2015-05-13 05:35:08 +00:00
|
|
|
# Return the thumbprint of the last installed certificate;
|
|
|
|
# This is needed for the new HTTPS WinRM listerner we're
|
|
|
|
# going to create further down.
|
2014-11-11 07:20:28 +00:00
|
|
|
Get-ChildItem "Cert:\LocalMachine\my"| Sort-Object NotBefore -Descending | Select -First 1 | Select -Expand Thumbprint
|
2014-07-06 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Setup error handling.
|
|
|
|
Trap
|
|
|
|
{
|
|
|
|
$_
|
|
|
|
Exit 1
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Detect PowerShell version.
|
|
|
|
If ($PSVersionTable.PSVersion.Major -lt 3)
|
2014-07-06 17:02:32 +00:00
|
|
|
{
|
2014-11-11 07:20:28 +00:00
|
|
|
Throw "PowerShell version 3 or higher is required."
|
2014-07-06 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Find and start the WinRM service.
|
|
|
|
Write-Verbose "Verifying WinRM service."
|
|
|
|
If (!(Get-Service "WinRM"))
|
|
|
|
{
|
|
|
|
Throw "Unable to find the WinRM service."
|
|
|
|
}
|
|
|
|
ElseIf ((Get-Service "WinRM").Status -ne "Running")
|
|
|
|
{
|
|
|
|
Write-Verbose "Starting WinRM service."
|
|
|
|
Start-Service -Name "WinRM" -ErrorAction Stop
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# WinRM should be running; check that we have a PS session config.
|
|
|
|
If (!(Get-PSSessionConfiguration -Verbose:$false) -or (!(Get-ChildItem WSMan:\localhost\Listener)))
|
|
|
|
{
|
2015-09-14 21:47:44 +00:00
|
|
|
if ($SkipNetworkProfileCheck) {
|
|
|
|
Write-Verbose "Enabling PS Remoting without checking Network profile."
|
|
|
|
Enable-PSRemoting -SkipNetworkProfileCheck -Force -ErrorAction Stop
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Write-Verbose "Enabling PS Remoting"
|
2014-12-16 00:29:13 +00:00
|
|
|
Enable-PSRemoting -Force -ErrorAction Stop
|
2015-09-14 21:47:44 +00:00
|
|
|
}
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "PS Remoting is already enabled."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make sure there is a SSL listener.
|
|
|
|
$listeners = Get-ChildItem WSMan:\localhost\Listener
|
|
|
|
If (!($listeners | Where {$_.Keys -like "TRANSPORT=HTTPS"}))
|
|
|
|
{
|
|
|
|
# HTTPS-based endpoint does not exist.
|
|
|
|
If (Get-Command "New-SelfSignedCertificate" -ErrorAction SilentlyContinue)
|
2014-07-06 17:02:32 +00:00
|
|
|
{
|
2015-05-13 05:35:08 +00:00
|
|
|
$cert = New-SelfSignedCertificate -DnsName $SubjectName -CertStoreLocation "Cert:\LocalMachine\My"
|
2014-11-11 07:20:28 +00:00
|
|
|
$thumbprint = $cert.Thumbprint
|
2015-05-13 05:35:08 +00:00
|
|
|
Write-Host "Self-signed SSL certificate generated; thumbprint: $thumbprint"
|
2014-07-06 17:02:32 +00:00
|
|
|
}
|
2014-11-11 07:20:28 +00:00
|
|
|
Else
|
|
|
|
{
|
2015-05-13 05:35:08 +00:00
|
|
|
$thumbprint = New-LegacySelfSignedCert -SubjectName $SubjectName
|
|
|
|
Write-Host "(Legacy) Self-signed SSL certificate generated; thumbprint: $thumbprint"
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 17:02:32 +00:00
|
|
|
# Create the hashtables of settings to be used.
|
|
|
|
$valueset = @{}
|
2015-05-13 05:35:08 +00:00
|
|
|
$valueset.Add('Hostname', $SubjectName)
|
2014-11-11 07:20:28 +00:00
|
|
|
$valueset.Add('CertificateThumbprint', $thumbprint)
|
2014-07-06 17:02:32 +00:00
|
|
|
|
|
|
|
$selectorset = @{}
|
2014-11-11 07:20:28 +00:00
|
|
|
$selectorset.Add('Transport', 'HTTPS')
|
|
|
|
$selectorset.Add('Address', '*')
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
Write-Verbose "Enabling SSL listener."
|
|
|
|
New-WSManInstance -ResourceURI 'winrm/config/Listener' -SelectorSet $selectorset -ValueSet $valueset
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "SSL listener is already active."
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Check for basic authentication.
|
|
|
|
$basicAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where {$_.Name -eq "Basic"}
|
|
|
|
If (($basicAuthSetting.Value) -eq $false)
|
|
|
|
{
|
|
|
|
Write-Verbose "Enabling basic auth support."
|
2014-07-06 17:02:32 +00:00
|
|
|
Set-Item -Path "WSMan:\localhost\Service\Auth\Basic" -Value $true
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "Basic auth is already enabled."
|
|
|
|
}
|
|
|
|
|
|
|
|
# Configure firewall to allow WinRM HTTPS connections.
|
|
|
|
$fwtest1 = netsh advfirewall firewall show rule name="Allow WinRM HTTPS"
|
|
|
|
$fwtest2 = netsh advfirewall firewall show rule name="Allow WinRM HTTPS" profile=any
|
|
|
|
If ($fwtest1.count -lt 5)
|
|
|
|
{
|
|
|
|
Write-Verbose "Adding firewall rule to allow WinRM HTTPS."
|
|
|
|
netsh advfirewall firewall add rule profile=any name="Allow WinRM HTTPS" dir=in localport=5986 protocol=TCP action=allow
|
|
|
|
}
|
|
|
|
ElseIf (($fwtest1.count -ge 5) -and ($fwtest2.count -lt 5))
|
|
|
|
{
|
|
|
|
Write-Verbose "Updating firewall rule to allow WinRM HTTPS for any profile."
|
|
|
|
netsh advfirewall firewall set rule name="Allow WinRM HTTPS" new profile=any
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "Firewall rule already exists to allow WinRM HTTPS."
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2015-08-31 23:38:23 +00:00
|
|
|
# Test a remoting connection to localhost, which should work.
|
|
|
|
$httpResult = Invoke-Command -ComputerName "localhost" -ScriptBlock {$env:COMPUTERNAME} -ErrorVariable httpError -ErrorAction SilentlyContinue
|
|
|
|
$httpsOptions = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
|
|
|
|
|
|
|
|
$httpsResult = New-PSSession -UseSSL -ComputerName "localhost" -SessionOption $httpsOptions -ErrorVariable httpsError -ErrorAction SilentlyContinue
|
|
|
|
|
|
|
|
If ($httpResult -and $httpsResult)
|
|
|
|
{
|
2015-05-13 05:35:08 +00:00
|
|
|
Write-Verbose "HTTP: Enabled | HTTPS: Enabled"
|
2015-08-31 23:38:23 +00:00
|
|
|
}
|
|
|
|
ElseIf ($httpsResult -and !$httpResult)
|
|
|
|
{
|
2015-05-13 05:35:08 +00:00
|
|
|
Write-Verbose "HTTP: Disabled | HTTPS: Enabled"
|
2015-08-31 23:38:23 +00:00
|
|
|
}
|
|
|
|
ElseIf ($httpResult -and !$httpsResult)
|
|
|
|
{
|
2015-05-13 05:35:08 +00:00
|
|
|
Write-Verbose "HTTP: Enabled | HTTPS: Disabled"
|
2015-08-31 23:38:23 +00:00
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Throw "Unable to establish an HTTP or HTTPS remoting session."
|
|
|
|
}
|
2014-11-11 07:20:28 +00:00
|
|
|
Write-Verbose "PS Remoting has been successfully configured for Ansible."
|