Wednesday, September 18, 2013

Deleting Roaming Profiles in Citrix/Terminal Server WITH GUI

Here is a nifty little script that can be used by a helpdesk in a Windows 2008/R2 Citrix environment, that uses roaming profiles. In the case of profile corruption, it will go through each server and delete the local profile, then delete the profile in the central store.
It is based on PowerShell.

Specify your servers on line 11, your domain(s) on line 13 and your roaming profiles store on line 15.
<#

Title: DeleteCitrixUserProfile.ps1
Description: Deletes Citrix User Profiles from Citrix Servers
Comments: None
Author: Dani (http://syscript.blogspot.com)
Original Date: July 5, 2013
Modified Date: September 18, 2013
Version: 1.0
#>
#Computer List of Servers to check
$ComputerList = @("Server1","Server2","Server3")
#Domain List of User profile to specify (Roaming profiles store needs this information)
$DomainList = @("DOMAIN1","DOMAIN2")
#Roaming Profiles Store, include tailing back-slash
$RoamingProfilesStorage = "\\Server\Store\Profiles"

Function Ping($computername)
{
$query = "select * from win32_pingstatus where address = '" + $computername + "'"
$wmi = get-wmiobject -query $query
if ($($wmi.statuscode) -eq $null)
{
$rtnvalue = $($wmi.PrimaryAddressResolutionStatus)
}
else {$rtnvalue = $($wmi.statuscode)}
if ( $rtnvalue -eq 0 ) {$true}
else {$false}
}

Function WMIConnectionTest($computername)
{
$wmiClass = "Win32_OperatingSystem"
if ((Get-WmiObject -ComputerName $computerName win32_operatingsystem -ErrorAction silentlycontinue))
{ return $true }
else { return $false }
}

Function CleanProfile($username,$domain)
{
Foreach ($Computername in $ComputerList)
{
#echo $computername
if (Ping($ComputerName) -eq $true -and if (WMIConnectionTest($ComputerName) -eq $true))
{
if ((Test-Path ("\\$ComputerName\c$\Users\$username")) )
{
Remove-Item -Recurse -Force ("\\$ComputerName\c$\Users\$username")
}
if ((Test-Path ("\\$ComputerName\c$\Users\$username`.$Domain")) )
{
Remove-Item -Recurse -Force ("\\$ComputerName\c$\Users\$username`.$Domain")
}
}

}
if (Test-Path ("$RoamingProfilesStorage\$username`.$domain`.V2"))
{
Remove-Item -Recurse -Force ("$RoamingProfilesStorage\$username`.$domain`.V2")
}

}

#GUI Controls
#Layout is specified as (X,Y)


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "DeleteCitrixUserProfile.ps1"
$objForm.Size = New-Object System.Drawing.Size(350,140)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x = $objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,75)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x = $objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,75)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,5)
$objLabel.Size = New-Object System.Drawing.Size(300,20)
$objLabel.Text = "Please enter username to clean up:"
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,25)
$objTextBox.Size = New-Object System.Drawing.Size(300,20)
$objForm.Controls.Add($objTextBox)

$objDomainList = New-Object System.Windows.Forms.ComboBox
$objDomainList.Location = New-Object System.Drawing.Size(10,45)
$objDomainList.Size = New-Object System.Drawing.Size(150,30)
foreach($userdomains in $DomainList)
{
$objDomainList.Items.Add($userdomains) > $null
}
$objDomainList.SelectedIndex= 0
$objForm.Controls.Add($objDomainList)

$objForm.Topmost = $True

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

$x = $objTextBox.Text
$y = $objDomainList.SelectedItem.ToString()
if ($x -ne "" -and $x -ne $null)
{
$confirm = [System.Windows.Forms.MessageBox]::Show("Are you sure you would like to remove the Citrix profile for $y\" + $x + "?","Confirmation","YesNo","Warning")

if ($confirm -eq "Yes" -and $x -ne "" -and $x -ne $null)
{
# Do Code Here
#[System.Windows.Forms.MessageBox]::Show("Presed yes, will perform operation on " + $x + ".","Confirmation","OK","Information")
#Executes actual code
CleanProfile -username $x -domain $y
}
else {
[System.Windows.Forms.MessageBox]::Show("Will not perform operation on " + $x + ".","Confirmation","OK","Error")
}
}
[System.Windows.Forms.MessageBox]::Show("The Program has completed")

No comments:

Post a Comment