Tuesday, July 2, 2013

Citrix - Adding virtual Workstations running on VMware ESXi to a Citrix DDC (machine catalog & desktop group)

######################################
# Add needed PS Snapins - Citrix SDKs
######################################

#Load Citrix Snapins
if (Get-PSSnapin Citrix.Broker.Admin.V1 -ea 0)
{
Write-Host "Citrix snapins already loaded" -ForegroundColor Yellow
}
else
{
if (Get-PSSnapIn Citrix.Broker.Admin.V1 -registered -ea 0)
{
Write-Host "Loading Citrix snapins..." -ForegroundColor Yellow
Add-PSSnapin Citrix.Broker.Admin.V1
}
}

######################################
# menu function
######################################

Function Show-Menu {

Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter your menu text")]
[ValidateNotNullOrEmpty()]
[string]$Menu,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[string]$Title=”Menu”,
[switch]$ClearScreen
)

if ($ClearScreen) {Clear-Host}

#build the menu prompt
$menuPrompt=$title
#add a return
$menuprompt+=”`n”
#add an underline
$menuprompt+=”-”*$title.Length
$menuprompt+=”`n”
#add the menu
$menuPrompt+=$menu

Read-Host -Prompt $menuprompt

} #end function

######################################
# Prompt for username / password
######################################

# Get user credentials
$Credentials = Get-Credential

######################################
# Prompt to specify vCenter
######################################

#define a menu here string
$Menu=@"
1 ENV 1
2 ENV 2
3 ENV 3
4 ENV 4

Select a vCenter by number
"@

Switch (Show-Menu $Menu "Select ENV" -clear) {
"1" {"** ENV1 **";
Connect-VIServer -server vcenter1.server.com -Protocol https -User "domain\$Username" -Password $Password;
$clusterName = "cluster1";
$DDCName = "DDC1";
break;}
"2" {"** ENV2 **";
Connect-VIServer -server vcenter2.server.com -Protocol https -User "domain\$Username" -Password $Password;
$clusterName = "cluster2";
$DDCName = "DDC2";
break;}
"3" {"** ENV3 **";
Connect-VIServer -server vcenter3.server.com -Protocol https -User "domain\$Username" -Password $Password;
$clusterName = "cluster3";
$DDCName = "DDC3";
break;}
"4" {"** ENV4 **";
Connect-VIServer -server vcenter4.server.com -Protocol https -User "domain\$Username" -Password $Password;
$clusterName = "cluster4";
$DDCName = "DDC4";
break;}
Default {Write-Warning "Invalid Choice. Try again.";
break;}
}

######################################
# Prompt to specify Workstation name
######################################

$WorkstationName = Read-Host "Which Workstation Name (or wildcard)"

##############################
# List & Select Catalog UID
##############################

Write-Host "Machine Catalog List" -ForegroundColor Green
get-brokercatalog -filter {allocationType -eq 'permanent'} -AdminAddress $DDCName | select name,uid
$CatalogUID = Read-Host "Which Catalog UID"

##############################
# List & Select Desktop Group UID
##############################

Write-Host "Desktop Group List" -ForegroundColor Green
get-brokerdesktopgroup -filter {desktopkind -eq 'private'} -AdminAddress $DDCName | select name
$DesktopGroupName = Read-Host "Which DesktopGroup Name"

##############################
# List & Select HyperVisor UID
##############################

Write-Host "vCenter List" -ForegroundColor Green
get-brokerhypervisorconnection -AdminAddress $DDCName | select name,uid
$HyperVisorUID = Read-Host "Which HyperVisor UID"

##############################
# Get collection of VMs, create log file
##############################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "W:\scripts\ScriptOutput\Citrix-XD-AddMachincesToDDC_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"


##############################
# Get variables for each VM
##############################

foreach ($VM in $colVMs) {
$VMName = $VM.name
$DomainVMName = "domain\$VMName"
$DNSVMName = "$VMName.domain.com"
$BrokerUserName = "domain\username"
if ($VMName -like $WorkstationName) {
$UUID = Get-VM $VM.name | %{(Get-View $_.Id).config.uuid}
new-brokermachine -catalogUid $CatalogUID -machinename $DomainVMName -HostedMachineID $UUID -HypervisorConnectionUid $HyperVisorUID -AdminAddress $DDCName
Add-BrokerUser $BrokerUserName -Machine $DomainVMName
Add-BrokerMachine $DomainVMName -DesktopGroup $DesktopGroupName
Write-Host $VMName `t $DesktopGroupName `t $UUID -ForegroundColor Green
Add-Content -Path $logFile -Value ($VMName + "`t-`t" + $DesktopGroupName + "`t-`t" + $UUID)
}

else {
Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`t")
}


}


##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

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