2017-01-27 22:23:18 +00:00
|
|
|
#Requires -Version 3.0
|
|
|
|
|
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
|
|
|
# -----------------------------------------------------------
|
|
|
|
#
|
2017-02-04 07:52:01 +00:00
|
|
|
# This script checks the current WinRM (PS Remoting) configuration and makes
|
2017-02-18 11:43:04 +00:00
|
|
|
# the necessary changes to allow Ansible to connect, authenticate and
|
2017-02-04 07:52:01 +00:00
|
|
|
# execute PowerShell commands.
|
2015-09-14 21:47:44 +00:00
|
|
|
#
|
2017-01-11 07:52:41 +00:00
|
|
|
# All events are logged to the Windows EventLog, useful for unattended runs.
|
2014-07-06 17:02:32 +00:00
|
|
|
#
|
2017-01-11 07:52:41 +00:00
|
|
|
# Use option -Verbose in order to see the verbose output messages.
|
|
|
|
#
|
2017-02-04 07:52:01 +00:00
|
|
|
# Use option -CertValidityDays to specify how long this certificate is valid
|
|
|
|
# starting from today. So you would specify -CertValidityDays 3650 to get
|
|
|
|
# a 10-year valid certificate.
|
2017-01-11 07:52:41 +00:00
|
|
|
#
|
|
|
|
# Use option -ForceNewSSLCert if the system has been SysPreped and a new
|
2017-06-12 06:55:19 +00:00
|
|
|
# SSL Certificate must be forced on the WinRM Listener when re-running this
|
2017-01-11 07:52:41 +00:00
|
|
|
# script. This is necessary when a new SID and CN name is created.
|
2016-04-13 22:02:55 +00:00
|
|
|
#
|
2017-11-02 15:59:13 +00:00
|
|
|
# Use option -EnableCredSSP to enable CredSSP as an authentication option.
|
|
|
|
#
|
2018-01-02 00:13:20 +00:00
|
|
|
# Use option -DisableBasicAuth to disable basic authentication.
|
|
|
|
#
|
2017-02-04 07:52:01 +00:00
|
|
|
# Use option -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.
|
|
|
|
#
|
|
|
|
# Use option -SubjectName to specify the CN name of the certificate. This
|
|
|
|
# defaults to the system's hostname and generally should not be specified.
|
|
|
|
|
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>
|
2016-04-13 22:02:55 +00:00
|
|
|
# Updated by Anton Ouzounov <Anton.Ouzounov@careerbuilder.com>
|
2017-04-19 11:21:25 +00:00
|
|
|
# Updated by Nicolas Simond <contact@nicolas-simond.com>
|
2017-01-11 07:52:41 +00:00
|
|
|
# Updated by Dag Wieërs <dag@wieers.com>
|
2017-02-09 01:00:58 +00:00
|
|
|
# Updated by Jordan Borean <jborean93@gmail.com>
|
2018-01-02 00:13:20 +00:00
|
|
|
# Updated by Erwan Quélin <erwan.quelin@gmail.com>
|
2018-04-19 23:01:48 +00:00
|
|
|
# Updated by David Norman <david@dkn.email>
|
2014-07-06 17:02:32 +00:00
|
|
|
#
|
2017-01-11 07:52:41 +00:00
|
|
|
# Version 1.0 - 2014-07-06
|
|
|
|
# Version 1.1 - 2014-11-11
|
|
|
|
# Version 1.2 - 2015-05-15
|
|
|
|
# Version 1.3 - 2016-04-04
|
|
|
|
# Version 1.4 - 2017-01-05
|
2017-02-09 01:00:58 +00:00
|
|
|
# Version 1.5 - 2017-02-09
|
2017-04-19 11:21:25 +00:00
|
|
|
# Version 1.6 - 2017-04-18
|
2018-01-02 00:13:20 +00:00
|
|
|
# Version 1.7 - 2017-11-23
|
2018-04-19 23:01:48 +00:00
|
|
|
# Version 1.8 - 2018-02-23
|
2017-01-11 07:52:41 +00:00
|
|
|
|
|
|
|
# Support -Verbose option
|
|
|
|
[CmdletBinding()]
|
2014-07-06 17:02:32 +00:00
|
|
|
|
|
|
|
Param (
|
|
|
|
[string]$SubjectName = $env:COMPUTERNAME,
|
2017-04-19 11:21:25 +00:00
|
|
|
[int]$CertValidityDays = 1095,
|
2015-09-14 21:47:44 +00:00
|
|
|
[switch]$SkipNetworkProfileCheck,
|
2016-04-13 22:02:55 +00:00
|
|
|
$CreateSelfSignedCert = $true,
|
2017-02-09 01:00:58 +00:00
|
|
|
[switch]$ForceNewSSLCert,
|
2018-01-02 00:13:20 +00:00
|
|
|
[switch]$GlobalHttpFirewallAccess,
|
|
|
|
[switch]$DisableBasicAuth = $false,
|
|
|
|
[switch]$EnableCredSSP
|
2014-07-06 17:02:32 +00:00
|
|
|
)
|
|
|
|
|
2017-01-11 07:52:41 +00:00
|
|
|
Function Write-Log
|
|
|
|
{
|
|
|
|
$Message = $args[0]
|
|
|
|
Write-EventLog -LogName Application -Source $EventSource -EntryType Information -EventId 1 -Message $Message
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Write-VerboseLog
|
|
|
|
{
|
|
|
|
$Message = $args[0]
|
|
|
|
Write-Verbose $Message
|
|
|
|
Write-Log $Message
|
|
|
|
}
|
|
|
|
|
|
|
|
Function Write-HostLog
|
|
|
|
{
|
|
|
|
$Message = $args[0]
|
2017-12-21 06:40:55 +00:00
|
|
|
Write-Output $Message
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log $Message
|
|
|
|
}
|
|
|
|
|
2014-07-06 17:02:32 +00:00
|
|
|
Function New-LegacySelfSignedCert
|
|
|
|
{
|
|
|
|
Param (
|
|
|
|
[string]$SubjectName,
|
2017-04-19 11:21:25 +00:00
|
|
|
[int]$ValidDays = 1095
|
2014-07-06 17:02:32 +00:00
|
|
|
)
|
2015-09-14 21:47:44 +00:00
|
|
|
|
2018-04-19 23:01:48 +00:00
|
|
|
$hostnonFQDN = $env:computerName
|
|
|
|
$hostFQDN = [System.Net.Dns]::GetHostByName(($env:computerName)).Hostname
|
|
|
|
$SignatureAlgorithm = "SHA256"
|
|
|
|
|
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"
|
2018-04-19 23:01:48 +00:00
|
|
|
$key.ProviderName = "Microsoft Enhanced RSA and AES Cryptographic Provider"
|
2014-07-06 17:02:32 +00:00
|
|
|
$key.KeySpec = 1
|
2017-04-19 11:21:25 +00:00
|
|
|
$key.Length = 4096
|
2014-07-06 17:02:32 +00:00
|
|
|
$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)
|
2018-04-19 23:01:48 +00:00
|
|
|
|
|
|
|
$SigOID = New-Object -ComObject X509Enrollment.CObjectId
|
|
|
|
$SigOID.InitializeFromValue(([Security.Cryptography.Oid]$SignatureAlgorithm).Value)
|
|
|
|
|
|
|
|
[string[]] $AlternativeName += $hostnonFQDN
|
|
|
|
$AlternativeName += $hostFQDN
|
|
|
|
$IAlternativeNames = New-Object -ComObject X509Enrollment.CAlternativeNames
|
|
|
|
|
|
|
|
foreach ($AN in $AlternativeName)
|
|
|
|
{
|
|
|
|
$AltName = New-Object -ComObject X509Enrollment.CAlternativeName
|
|
|
|
$AltName.InitializeFromString(0x3,$AN)
|
|
|
|
$IAlternativeNames.Add($AltName)
|
|
|
|
}
|
|
|
|
|
|
|
|
$SubjectAlternativeName = New-Object -ComObject X509Enrollment.CX509ExtensionAlternativeNames
|
|
|
|
$SubjectAlternativeName.InitializeEncode($IAlternativeNames)
|
|
|
|
|
|
|
|
[String[]]$KeyUsage = ("DigitalSignature", "KeyEncipherment")
|
|
|
|
$KeyUsageObj = New-Object -ComObject X509Enrollment.CX509ExtensionKeyUsage
|
|
|
|
$KeyUsageObj.InitializeEncode([int][Security.Cryptography.X509Certificates.X509KeyUsageFlags]($KeyUsage))
|
|
|
|
$KeyUsageObj.Critical = $true
|
|
|
|
|
|
|
|
$cert.X509Extensions.Add($KeyUsageObj)
|
2014-07-06 17:02:32 +00:00
|
|
|
$cert.X509Extensions.Add($ekuext)
|
2018-04-19 23:01:48 +00:00
|
|
|
$cert.SignatureInformation.HashAlgorithm = $SigOID
|
|
|
|
$CERT.X509Extensions.Add($SubjectAlternativeName)
|
2014-07-06 17:02:32 +00:00
|
|
|
$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, "")
|
|
|
|
|
2017-02-13 09:16:23 +00:00
|
|
|
# extract/return the thumbprint from the generated cert
|
|
|
|
$parsed_cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
|
|
|
|
$parsed_cert.Import([System.Text.Encoding]::UTF8.GetBytes($certdata))
|
|
|
|
|
|
|
|
return $parsed_cert.Thumbprint
|
2014-07-06 17:02:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 04:36:26 +00:00
|
|
|
Function Enable-GlobalHttpFirewallAccess
|
|
|
|
{
|
|
|
|
Write-Verbose "Forcing global HTTP firewall access"
|
|
|
|
# this is a fairly naive implementation; could be more sophisticated about rule matching/collapsing
|
|
|
|
$fw = New-Object -ComObject HNetCfg.FWPolicy2
|
|
|
|
|
|
|
|
# try to find/enable the default rule first
|
|
|
|
$add_rule = $false
|
|
|
|
$matching_rules = $fw.Rules | ? { $_.Name -eq "Windows Remote Management (HTTP-In)" }
|
|
|
|
$rule = $null
|
|
|
|
If ($matching_rules) {
|
|
|
|
If ($matching_rules -isnot [Array]) {
|
|
|
|
Write-Verbose "Editing existing single HTTP firewall rule"
|
|
|
|
$rule = $matching_rules
|
|
|
|
}
|
|
|
|
Else {
|
|
|
|
# try to find one with the All or Public profile first
|
|
|
|
Write-Verbose "Found multiple existing HTTP firewall rules..."
|
|
|
|
$rule = $matching_rules | % { $_.Profiles -band 4 }[0]
|
|
|
|
|
|
|
|
If (-not $rule -or $rule -is [Array]) {
|
|
|
|
Write-Verbose "Editing an arbitrary single HTTP firewall rule (multiple existed)"
|
|
|
|
# oh well, just pick the first one
|
|
|
|
$rule = $matching_rules[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
If (-not $rule) {
|
|
|
|
Write-Verbose "Creating a new HTTP firewall rule"
|
|
|
|
$rule = New-Object -ComObject HNetCfg.FWRule
|
|
|
|
$rule.Name = "Windows Remote Management (HTTP-In)"
|
|
|
|
$rule.Description = "Inbound rule for Windows Remote Management via WS-Management. [TCP 5985]"
|
|
|
|
$add_rule = $true
|
|
|
|
}
|
|
|
|
|
|
|
|
$rule.Profiles = 0x7FFFFFFF
|
|
|
|
$rule.Protocol = 6
|
|
|
|
$rule.LocalPorts = 5985
|
|
|
|
$rule.RemotePorts = "*"
|
|
|
|
$rule.LocalAddresses = "*"
|
|
|
|
$rule.RemoteAddresses = "*"
|
|
|
|
$rule.Enabled = $true
|
|
|
|
$rule.Direction = 1
|
|
|
|
$rule.Action = 1
|
|
|
|
$rule.Grouping = "Windows Remote Management"
|
|
|
|
|
|
|
|
If ($add_rule) {
|
|
|
|
$fw.Rules.Add($rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Verbose "HTTP firewall rule $($rule.Name) updated"
|
|
|
|
}
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Setup error handling.
|
|
|
|
Trap
|
|
|
|
{
|
|
|
|
$_
|
|
|
|
Exit 1
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
$ErrorActionPreference = "Stop"
|
2017-01-27 22:23:18 +00:00
|
|
|
|
|
|
|
# Get the ID and security principal of the current user account
|
|
|
|
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
|
|
|
|
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
|
|
|
|
|
|
|
|
# Get the security principal for the Administrator role
|
|
|
|
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
|
|
|
|
|
|
|
|
# Check to see if we are currently running "as Administrator"
|
|
|
|
if (-Not $myWindowsPrincipal.IsInRole($adminRole))
|
|
|
|
{
|
2017-12-21 06:40:55 +00:00
|
|
|
Write-Output "ERROR: You need elevated Administrator privileges in order to run this script."
|
|
|
|
Write-Output " Start Windows PowerShell by using the Run as Administrator option."
|
2017-01-27 22:23:18 +00:00
|
|
|
Exit 2
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:52:41 +00:00
|
|
|
$EventSource = $MyInvocation.MyCommand.Name
|
2017-01-20 21:52:09 +00:00
|
|
|
If (-Not $EventSource)
|
2017-01-11 07:52:41 +00:00
|
|
|
{
|
|
|
|
$EventSource = "Powershell CLI"
|
|
|
|
}
|
|
|
|
|
|
|
|
If ([System.Diagnostics.EventLog]::Exists('Application') -eq $False -or [System.Diagnostics.EventLog]::SourceExists($EventSource) -eq $False)
|
|
|
|
{
|
|
|
|
New-EventLog -LogName Application -Source $EventSource
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
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
|
|
|
{
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "PowerShell version 3 or higher is required."
|
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"))
|
|
|
|
{
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Unable to find the WinRM service."
|
2014-11-11 07:20:28 +00:00
|
|
|
Throw "Unable to find the WinRM service."
|
|
|
|
}
|
|
|
|
ElseIf ((Get-Service "WinRM").Status -ne "Running")
|
|
|
|
{
|
2016-09-09 16:47:15 +00:00
|
|
|
Write-Verbose "Setting WinRM service to start automatically on boot."
|
2016-09-09 11:00:49 +00:00
|
|
|
Set-Service -Name "WinRM" -StartupType Automatic
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Set WinRM service to start automatically on boot."
|
2017-08-06 22:14:56 +00:00
|
|
|
Write-Verbose "Starting WinRM service."
|
|
|
|
Start-Service -Name "WinRM" -ErrorAction Stop
|
|
|
|
Write-Log "Started WinRM service."
|
2017-01-11 07:52:41 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
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)))
|
|
|
|
{
|
2017-01-11 07:52:41 +00:00
|
|
|
If ($SkipNetworkProfileCheck) {
|
2015-09-14 21:47:44 +00:00
|
|
|
Write-Verbose "Enabling PS Remoting without checking Network profile."
|
|
|
|
Enable-PSRemoting -SkipNetworkProfileCheck -Force -ErrorAction Stop
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Enabled PS Remoting without checking Network profile."
|
2015-09-14 21:47:44 +00:00
|
|
|
}
|
2017-01-11 07:52:41 +00:00
|
|
|
Else {
|
|
|
|
Write-Verbose "Enabling PS Remoting."
|
2014-12-16 00:29:13 +00:00
|
|
|
Enable-PSRemoting -Force -ErrorAction Stop
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Enabled PS Remoting."
|
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"}))
|
|
|
|
{
|
2017-02-06 08:14:42 +00:00
|
|
|
# We cannot use New-SelfSignedCertificate on 2012R2 and earlier
|
|
|
|
$thumbprint = New-LegacySelfSignedCert -SubjectName $SubjectName -ValidDays $CertValidityDays
|
|
|
|
Write-HostLog "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.
|
2017-02-06 08:14:42 +00:00
|
|
|
$valueset = @{
|
|
|
|
Hostname = $SubjectName
|
|
|
|
CertificateThumbprint = $thumbprint
|
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2017-02-06 08:14:42 +00:00
|
|
|
$selectorset = @{
|
|
|
|
Transport = "HTTPS"
|
|
|
|
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
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Enabled SSL listener."
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "SSL listener is already active."
|
2017-01-11 07:52:41 +00:00
|
|
|
|
2016-04-13 22:02:55 +00:00
|
|
|
# Force a new SSL cert on Listener if the $ForceNewSSLCert
|
2017-01-11 07:52:41 +00:00
|
|
|
If ($ForceNewSSLCert)
|
|
|
|
{
|
|
|
|
|
2017-02-06 08:14:42 +00:00
|
|
|
# We cannot use New-SelfSignedCertificate on 2012R2 and earlier
|
|
|
|
$thumbprint = New-LegacySelfSignedCert -SubjectName $SubjectName -ValidDays $CertValidityDays
|
|
|
|
Write-HostLog "Self-signed SSL certificate generated; thumbprint: $thumbprint"
|
2016-04-13 22:02:55 +00:00
|
|
|
|
2017-02-06 08:14:42 +00:00
|
|
|
$valueset = @{
|
|
|
|
CertificateThumbprint = $thumbprint
|
|
|
|
Hostname = $SubjectName
|
|
|
|
}
|
2016-04-13 22:02:55 +00:00
|
|
|
|
|
|
|
# Delete the listener for SSL
|
2017-02-06 08:14:42 +00:00
|
|
|
$selectorset = @{
|
|
|
|
Address = "*"
|
|
|
|
Transport = "HTTPS"
|
|
|
|
}
|
2016-04-13 22:02:55 +00:00
|
|
|
Remove-WSManInstance -ResourceURI 'winrm/config/Listener' -SelectorSet $selectorset
|
|
|
|
|
|
|
|
# Add new Listener with new SSL cert
|
|
|
|
New-WSManInstance -ResourceURI 'winrm/config/Listener' -SelectorSet $selectorset -ValueSet $valueset
|
|
|
|
}
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
2014-07-06 17:02:32 +00:00
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# Check for basic authentication.
|
2018-01-02 00:13:20 +00:00
|
|
|
$basicAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where-Object {$_.Name -eq "Basic"}
|
|
|
|
|
|
|
|
If ($DisableBasicAuth)
|
2014-11-11 07:20:28 +00:00
|
|
|
{
|
2018-01-02 00:13:20 +00:00
|
|
|
If (($basicAuthSetting.Value) -eq $true)
|
|
|
|
{
|
|
|
|
Write-Verbose "Disabling basic auth support."
|
|
|
|
Set-Item -Path "WSMan:\localhost\Service\Auth\Basic" -Value $false
|
|
|
|
Write-Log "Disabled basic auth support."
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "Basic auth is already disabled."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Else
|
2014-11-11 07:20:28 +00:00
|
|
|
{
|
2018-01-02 00:13:20 +00:00
|
|
|
If (($basicAuthSetting.Value) -eq $false)
|
|
|
|
{
|
|
|
|
Write-Verbose "Enabling basic auth support."
|
|
|
|
Set-Item -Path "WSMan:\localhost\Service\Auth\Basic" -Value $true
|
|
|
|
Write-Log "Enabled basic auth support."
|
|
|
|
}
|
|
|
|
Else
|
|
|
|
{
|
|
|
|
Write-Verbose "Basic auth is already enabled."
|
|
|
|
}
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 01:00:58 +00:00
|
|
|
# If EnableCredSSP if set to true
|
|
|
|
If ($EnableCredSSP)
|
|
|
|
{
|
|
|
|
# Check for CredSSP authentication
|
|
|
|
$credsspAuthSetting = Get-ChildItem WSMan:\localhost\Service\Auth | Where {$_.Name -eq "CredSSP"}
|
|
|
|
If (($credsspAuthSetting.Value) -eq $false)
|
|
|
|
{
|
|
|
|
Write-Verbose "Enabling CredSSP auth support."
|
|
|
|
Enable-WSManCredSSP -role server -Force
|
|
|
|
Write-Log "Enabled CredSSP auth support."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:36:26 +00:00
|
|
|
If ($GlobalHttpFirewallAccess) {
|
|
|
|
Enable-GlobalHttpFirewallAccess
|
|
|
|
}
|
|
|
|
|
2014-11-11 07:20:28 +00:00
|
|
|
# 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
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Added firewall rule to allow WinRM HTTPS."
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
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
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Updated firewall rule to allow WinRM HTTPS for any profile."
|
2014-11-11 07:20:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-Log "Unable to establish an HTTP or HTTPS remoting session."
|
2015-08-31 23:38:23 +00:00
|
|
|
Throw "Unable to establish an HTTP or HTTPS remoting session."
|
|
|
|
}
|
2017-01-11 07:52:41 +00:00
|
|
|
Write-VerboseLog "PS Remoting has been successfully configured for Ansible."
|