New-EventLog -LogName 'Windows PowerShell' -Source 'AnsibleWinrmHttpsScript' -ErrorAction SilentlyContinue Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.NotAfter -gt (Get-Date) -and $_.NotBefore -lt (Get-Date) -and $_.Subject -cmatch [System.Net.Dns]::GetHostByName(($env:computerName)).Hostname}| ForEach-Object { $httpsListeners = Get-ChildItem WSMan:\localhost\Listener | Where {$_.Keys -like "TRANSPORT=HTTPS"} if([bool]$httpsListeners) { if((($httpsListeners) | Measure-Object).Count -eq 1) { if(((Get-ChildItem -Path $httpsListeners.PSPath) | Where-Object {$_.Name -match 'CertificateThumbprint'}).Value -Replace ' ','' -match $_.Thumbprint) { Write-EventLog -LogName 'Windows PowerShell' -Source 'AnsibleWinrmHttpsScript' -EventId 3660 -EntryType Information -Message 'valid https endpoint alerady existed' } else { Remove-Item -Recurse -Path $httpsListeners.PSPath -Confirm:$false New-WSManInstance -ResourceURI 'winrm/config/Listener' -ValueSet @{Hostname = [System.Net.Dns]::GetHostByName(($env:computerName)).Hostname} -SelectorSet @{Transport="HTTPS";Address="*"} | Out-Null Write-EventLog -LogName 'Windows PowerShell' -Source 'AnsibleWinrmHttpsScript' -EventId 3662 -EntryType Information -Message 'invalid https endpoint alerady existed - removed and recreated' } } else { Write-EventLog -LogName 'Windows PowerShell' -Source 'AnsibleWinrmHttpsScript' -EventId 3666 -EntryType Information -Message 'multiple https endpoint exist - aborting' } } else { New-WSManInstance -ResourceURI 'winrm/config/Listener' -ValueSet @{Hostname = [System.Net.Dns]::GetHostByName(($env:computerName)).Hostname} -SelectorSet @{Transport="HTTPS";Address="*"} | Out-Null Write-EventLog -LogName 'Windows PowerShell' -Source 'AnsibleWinrmHttpsScript' -EventId 3661 -EntryType Information -Message 'a new endpont has been created' } Remove-Variable httpsListeners }