Trusted Hosts Helper (Ver 0.1)

After writing my post about “Server Manager & WinRM” I decided to write a script to take care of adding and modifying the TrustedHosts list.

Here are two scripts, first one is menu-based script which would run entirely in the PowerShell/ISE window and second one is GUI; Graphical interface has been created using “SAPIEN PrimalForm (Community Edition)” and then the working part added to it.

[crayon title=”Trusted Hosts Helper – CLI (Ver 0.1)”]# Created by: Sohrab Kasraeian Fard

CLS
Write-Host “Trusted Hosts Helper – CLI (Ver 0.1)n"
$Action = Read-Host "(A)dd
n(C)lean listn(G)et listn(R)eplacen(E)xitnPlease select from above action list (Default: Add)”
switch ($Action) {
“c” {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value “” -Force
}
“g” {
Get-Item WSMan:\localhost\Client\TrustedHosts | select name,value | fl
}
“r” {
$TRS = Read-Host “Enter target "Server(s)” name or IP address (comma separated value)”
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $TRS -Force
}
“e” {
Exit
}
default {
$TRS = Read-Host “Enter target "Server(s)” name or IP address (comma separated value)”
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $TRS.ToString() -Concatenate -Force
}
}[/crayon]

[crayon title=”Trusted Hosts Helper – GUI (Ver 0.1)”]#Generated Form Function
function GenerateForm {
########################################################################

Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0

Generated On: 2/20/2014 11:55 PM

Generated By: Sohrab Kasraeian Fard

Script: Trusted Hosts Helper – GUI (Ver 0.1)

########################################################################

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname(“System.Windows.Forms”) | Out-Null
[reflection.assembly]::loadwithpartialname(“System.Drawing”) | Out-Null
#endregion

#region Generated Form Objects
$frmMain = New-Object System.Windows.Forms.Form
$btnGet = New-Object System.Windows.Forms.Button
$btnClear = New-Object System.Windows.Forms.Button
$btnExit = New-Object System.Windows.Forms.Button
$btnReplace = New-Object System.Windows.Forms.Button
$btnAdd = New-Object System.Windows.Forms.Button
$txtTRS = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$msg = New-Object -ComObject Wscript.Shell
#endregion Generated Form Objects

#———————————————-
#Generated Event Script Blocks
#———————————————-
#Provide Custom Code for events specified in PrimalForms.
$btnExit_OnClick=
{
#TODO: Place custom script here
$frmMain.Close()
CLS
}

$btnReplace_OnClick=
{
#TODO: Place custom script here
if ($txtTRS.Text -ne “”) {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $txtTRS.Text -Force
$txtTRS.Text = “”
$val = Get-Item WSMan:\localhost\Client\TrustedHosts
$msg.popup(“Current TrustedHosts are n“$($val.value)`””,10,”TrustedHosts”)
$txtTRS.Focus()
}
}

$btnClear_OnClick=
{
#TODO: Place custom script here
Set-Item WSMan:\localhost\Client\TrustedHosts -Value “” -Force
}

$btnGet_OnClick=
{
#TODO: Place custom script here
$txtTRS.Text = “”
$val = Get-Item WSMan:\localhost\Client\TrustedHosts
$msg.popup(“Current TrustedHosts are n“$($val.value)`””,10,”TrustedHosts”)
}

$btnAdd_OnClick=
{
#TODO: Place custom script here
if ($txtTRS.Text -ne “”) {
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $txtTRS.Text -Concatenate -Force
$txtTRS.Text = “”
$val = Get-Item WSMan:\localhost\Client\TrustedHosts
$msg.popup(“Current TrustedHosts are n“$($val.value)`””,10,”TrustedHosts”)
$txtTRS.Focus()
}
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$frmMain.WindowState = $InitialFormWindowState
}

#———————————————-
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 85
$System_Drawing_Size.Width = 420
$frmMain.ClientSize = $System_Drawing_Size
$frmMain.DataBindings.DefaultDataSourceUpdateMode = 0
$frmMain.FormBorderStyle = 1
$frmMain.MaximizeBox = $False
$frmMain.Name = “frmMain”
$frmMain.StartPosition = 1
$frmMain.Text = “Trusted Hosts Helper – GUI (Ver 0.1)”

$btnGet.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 174
$System_Drawing_Point.Y = 51
$btnGet.Location = $System_Drawing_Point
$btnGet.Name = “btnGet”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnGet.Size = $System_Drawing_Size
$btnGet.TabIndex = 4
$btnGet.Text = “Get”
$btnGet.UseVisualStyleBackColor = $True
$btnGet.add_Click($btnGet_OnClick)

$frmMain.Controls.Add($btnGet)

$btnClear.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 93
$System_Drawing_Point.Y = 50
$btnClear.Location = $System_Drawing_Point
$btnClear.Name = “btnClear”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnClear.Size = $System_Drawing_Size
$btnClear.TabIndex = 3
$btnClear.Text = “&Clear”
$btnClear.UseVisualStyleBackColor = $True
$btnClear.add_Click($btnClear_OnClick)

$frmMain.Controls.Add($btnClear)

$btnExit.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 336
$System_Drawing_Point.Y = 50
$btnExit.Location = $System_Drawing_Point
$btnExit.Name = “btnExit”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnExit.Size = $System_Drawing_Size
$btnExit.TabIndex = 6
$btnExit.Text = “&Exit”
$btnExit.UseVisualStyleBackColor = $True
$btnExit.add_Click($btnExit_OnClick)

$frmMain.Controls.Add($btnExit)

$btnReplace.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 255
$System_Drawing_Point.Y = 50
$btnReplace.Location = $System_Drawing_Point
$btnReplace.Name = “btnReplace”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnReplace.Size = $System_Drawing_Size
$btnReplace.TabIndex = 5
$btnReplace.Text = “&Replace”
$btnReplace.UseVisualStyleBackColor = $True
$btnReplace.add_Click($btnReplace_OnClick)

$frmMain.Controls.Add($btnReplace)

$btnAdd.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 51
$btnAdd.Location = $System_Drawing_Point
$btnAdd.Name = “btnAdd”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$btnAdd.Size = $System_Drawing_Size
$btnAdd.TabIndex = 2
$btnAdd.Text = “&Add”
$btnAdd.UseVisualStyleBackColor = $True
$btnAdd.add_Click($btnAdd_OnClick)

$frmMain.Controls.Add($btnAdd)

$txtTRS.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 25
$txtTRS.Location = $System_Drawing_Point
$txtTRS.Name = “txtTRS”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 399
$txtTRS.Size = $System_Drawing_Size
$txtTRS.TabIndex = 1

$frmMain.Controls.Add($txtTRS)

$label1.AutoSize = $True
$label1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 9
$label1.Location = $System_Drawing_Point
$label1.Name = “label1”
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 13
$System_Drawing_Size.Width = 105
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 0
$label1.Text = “Target remote server”

$frmMain.Controls.Add($label1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $frmMain.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmMain.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmMain.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm[/crayon]

Download

Trusted Hosts Helper – CLI (Ver 0.1)
Trusted Hosts Helper – GUI (Ver 0.1)

Screenshot

Trusted Hosts Helper (Ver 0.1)

 

It's your kindness to leave a reply/feedback