PowerShell Scripting Disclaimer:
This script is provided "as is" for the purpose of illustrating how product tasks may be performed in conjunction with PowerShell. Support shall not be liable for any direct, indirect, incidental, consequential, or other damage alleged in connection with the furnishing or use of this script or of the principles it demonstrates. See PowerShell Scripting Support for more information.
Configure the winrm service on the core to allow being accessed from the management workstation and add the management workstation on the trusted hosts list of the remote core. Since this is a complex operation, a powershell script capable of performing this operation and at the same time maintain the winrm capabilities on the Rapid Recovery core has been prepared.
The script allows entering the IP address for the management workstation and preparing the necessary permissions, reverting to the default settings, adding ip addresses to the trusted hosts list and removing the trusted hosts list. Additionally, for reference, the current configuration can be shown, without making any changes to the winrm configuration. After the script is executed, the winrm service is restarted.
The script help feature is fully documented and can be accessed by typing:
#Get-Help wirmconfigure.ps1 -full
The script code is shown below between horizontal lines and attached to the KB.
________________________________________________________
<#
.Synopsis
The script modifies the winrm settings to allow cross domain or cross workgroups connections. Please note that only LOCAL ADMIN credentials will work if you connect from another Domain or Workgroup.
.Description
Only one parameter at a time should be used to perform the various functions of this script
.Parameter ipaddress
The IP address to be added to the trusted hosts list. If a list of ip addresses is to be added, please use double quotes to enclose the list
i.e.: `"10.10.0.1,10.100.100.3,20.12.5.2`"
.Parameter reverse
Changes all values to the default settings without removing the trustedhosts list
.Parameter showonly
Shows current settings
.Parameter removetrustedhosts
Removes the trustedhosts list
.Example
winrmconfigure.ps1 -ipaddress `"10.10.0.1,10.100.100.3,20.12.5.2`"
.Example
winrmconfigure.ps1 -ipaddress 10.10.0.1
.Example
winrmconfigure.ps1 -reverse
.Example
winrmconfigure.ps1 -showonly
.Example
winrmconfigure.ps1 -removetrustedhosts
#>
param (
[Parameter(ParameterSetName='Main',Mandatory=$true)]
[string]$ipaddress,
[Parameter(ParameterSetName='Extra1',Mandatory=$true)]
[switch]$reverse=$false,
[Parameter(ParameterSetName='Extra2',Mandatory=$true)]
[switch]$showonly=$false,
[Parameter(ParameterSetName='Extra3',Mandatory=$true)]
[switch]$removetrustedhosts=$false
)
function get-values {
$trustedhosts = (get-childitem wsman:\localhost\client\trustedhosts).value
$matrix=@()
$line = [pscustomobject]@{Item="Client:TrustedHost";value=$trustedhosts}
$matrix += $line
$x = (get-childitem wsman:\localhost\client\allowunencrypted).value
$line = [pscustomobject]@{Item="Client:AllowUnencrypted";value=$x}
$matrix += $line
$x = (get-childitem wsman:\localhost\client\auth\basic).value
$line = [pscustomobject]@{Item="Client:BasicAuthentication";value=$x}
$matrix += $line
$x = (get-childitem wsman:\localhost\service\allowunencrypted).value
$line = [pscustomobject]@{Item="Service:AllowUnencrypted";value=$x}
$matrix += $line
$x = (get-childitem wsman:\localhost\service\auth\basic).value
$line = [pscustomobject]@{Item="Service:BasicAuthentication";value=$x}
$matrix += $line
$matrix | ft -AutoSize
return
}
cls
Write-Host "`r`nConfigure WinRm for Out-of-Domain Quickaccess`r`n---------------------------------------------`r`n"
winrm quickconfig
if(!($reverse)){
$op= "true"
} else {$op="false"}
if($showonly.IsPresent){
get-values
}
else {
get-values
$trustedhosts = (get-childitem wsman:\localhost\client\trustedhosts).value
#$trustedhosts = (get-childitem wsman:\localhost\client\trustedhosts).value
if($removetrustedhosts.IsPresent -or $reverse.IsPresent){
Write-host "The removetrustedhosts and/or reverse parameter(s) are present" -f Yellow
$trustedhosts = ""}
else
{
if(((($trustedhosts).trim()).length) -gt 0){
$trustedhosts = $trustedhosts.Trim()
if($trustedhosts.Replace($ipaddress,"").length -eq $trustedhosts.Length)
{
$trustedhosts += ",$IPaddress"
}
else
{
Write-host "Trustedhosts: $trustedhosts -- $ipaddress already in the list" -f Yellow;
break
}
}
else {$trustedhosts = $ipaddress}
}
set-item wsman:\localhost\client\trustedhosts -value $trustedhosts -Force
set-item wsman:\LocalHost\client\allowunencrypted -value $op -Force
set-item wsman:\LocalHost\client\auth\basic -value $op -Force
set-item wsman:\LocalHost\service\allowunencrypted -value $op -Force
set-item wsman:\LocalHost\service\auth\basic -value $op -Force
get-values
}
Write-host "`r`nRestarting winrm...`r`n"
restart-service winrm
________________________________________________________