RemotePC_Rename.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID aafce280-6ff5-4388-bf13-4a779a764577
 
.AUTHOR nikon@2nikon.net
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Citrix RemotePC
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Script to allow rename of Citrix RemotePC published name
 
#>
 

Param()


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms

asnp Citrix*

$DDC                   = 'DDC_Server'
$RemotePC_Catalog      = 'Remotepc'

#####################################################################################
## Functions
Function CurrentName($RemotePC_MachineName){
    $RemotePC = Get-BrokerMachine -MachineName $RemotePC_MachineName -AdminAddress $DDC
    Return $RemotePC.PublishedName
}

Function RenameRemotePC([string]$New_Name){
    $RemotePC = Get-BrokerMachine -MachineName $ComboBox1.SelectedItem.ToString( ) -AdminAddress $DDC
    $CurrentMachine = $RemotePC.MachineName
    $Current_Name = $RemotePC.PublishedName
    $MyConfirmationString = @"
You are about to change the assigned published name for a RemotePC machine:
                Machine Name : $CurrentMachine
                Current Published Name : $Current_Name
                New Published Name : $New_Name
 
 
                                                          Are you sure?
"@

    $msgBoxInput =  [System.Windows.MessageBox]::Show($MyConfirmationString,'Confirmation',4)
    switch  ($msgBoxInput) {
      'Yes' {
              Set-BrokerMachine -MachineName $CurrentMachine -PublishedName $New_Name -AdminAddress $DDC
              $RemotePC = Get-BrokerMachine -MachineName $CurrentMachine -AdminAddress $DDC
              $NewPublishedName = $RemotePC.PublishedName
              [System.Windows.MessageBox]::Show("The new publshed name for $CurrentMachine is $NewPublishedName",'Result',1)
              $TextBox2.Text = ""
              $TextBox1.Text = CurrentName($CurrentMachine)
              Return $NewPublishedName
           }
      'No' {  [System.Windows.MessageBox]::Show("Publshed name for $CurrentMachine was not changed",'Result',0)
              Return $Current_Name
           }
    }

}

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,140'
$Form.text                       = "RemotePC Rename Tool"
$Form.TopMost                    = $false
$form.FormBorderStyle            = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Form.StartPosition              = [System.Windows.Forms.FormStartPosition]::CenterScreen
$Form.MaximizeBox                = $false

$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Select a machine:"
$Label2.AutoSize                 = $true
$Label2.width                    = 25
$Label2.height                   = 10
$Label2.location                 = New-Object System.Drawing.Point(11,13)
$Label2.Font                     = 'Microsoft Sans Serif,10'

$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "Rename"
$Button1.width                   = 74
$Button1.height                  = 30
$Button1.location                = New-Object System.Drawing.Point(220,94)
$Button1.Font                    = 'Microsoft Sans Serif,10'

$Button2                         = New-Object system.Windows.Forms.Button
$Button2.text                    = "Close"
$Button2.width                   = 67
$Button2.height                  = 30
$Button2.location                = New-Object System.Drawing.Point(315,94)
$Button2.Font                    = 'Microsoft Sans Serif,10'

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Current Name:"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(203,13)
$Label1.Font                     = 'Microsoft Sans Serif,10'

$Label3                          = New-Object system.Windows.Forms.Label
$Label3.text                     = "New Name:"
$Label3.AutoSize                 = $true
$Label3.width                    = 25
$Label3.height                   = 10
$Label3.location                 = New-Object System.Drawing.Point(11,75)
$Label3.Font                     = 'Microsoft Sans Serif,10'

$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 180
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(203,35)
$TextBox1.Font                   = 'Microsoft Sans Serif,10'
$TextBox1.ReadOnly               = $true

$TextBox2                        = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline              = $false
$TextBox2.width                  = 180
$TextBox2.height                 = 20
$TextBox2.location               = New-Object System.Drawing.Point(13,97)
$TextBox2.Font                   = 'Microsoft Sans Serif,10'

$ComboBox1                       = New-Object system.Windows.Forms.ComboBox
$ComboBox1.width                 = 179
$ComboBox1.height                = 20
$ComboBox1.location              = New-Object System.Drawing.Point(11,35)
$ComboBox1.Font                  = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Label2,$Button1,$Button2,$Label1,$Label3,$TextBox1,$TextBox2,$ComboBox1))

#####################################################################################
## Logic

$Button1.Add_Click({ if ([string]::IsNullOrEmpty($textbox2.text)) { [System.Windows.MessageBox]::Show("Please enter a valid name",'Warning',0) } `
                        else { RenameRemotePC($TextBox2.Text) } })
$Button2.Add_Click({ $form.Close() })
$ComboBox1.Add_SelectedIndexChanged({ $TextBox1.Text = CurrentName($ComboBox1.SelectedItem.ToString( )) })

$machines = Get-BrokerMachine -CatalogName $RemotePC_Catalog -AdminAddress $DDC
Foreach ($machine in $machines){ [void]$Combobox1.Items.Add($machine.MachineName ) } 
$ComboBox1.SelectedIndex = 0

$TextBox2.MaxLength = 50
$TextBox2.Add_TextChanged({
    if ($this.Text -match '[^a-z 0-9_\-]') {
        $cursorPos = $this.SelectionStart
        $this.Text = $this.Text -replace '[^a-z 0-9_\-]',''
        # move the cursor to the end of the text:
        # $this.SelectionStart = $this.Text.Length

        # or leave the cursor where it was before the replace
        $this.SelectionStart = $cursorPos - 1
        $this.SelectionLength = 0
    }
})

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()