Author Posts

July 1, 2015 at 2:13 am

Hi All

This is suresh here and i am very new powershell. I need your advise to with my below script.

I have multiple XenDesktop sites and i need to get the information about a particular desktop from one of the sites. i come up with the below script.

 
$Desktop = Read-Host “ Enter your Desktop name”

if ($Desktop -like “UK”){ $Adminaddress = “uksite1.com”,$Adminaddress1 = “uksite1.com”}
if ($Desktop -like “US” {  $Adminaddress = “ussite1.com”}
if ($Desktop -like “SG” {  $Adminaddress = “sgsite1.com”}
$DesktopDetail = Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress $Adminaddress  
$DesktopDetail1 = Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress $Adminaddress1  
Write-Output $DesktopDetail

The issue i have is that Desktop names starts with UK is in both uksite1 and uksite 2 .

Currently i have assigned two variables for each site Adminaddress and Adminaddress1

I would like to check the desktop in each site and assign to one variable (adminaddress) instead of 2 variables for each site.

Your help is appreciated. Thanks.

July 1, 2015 at 2:45 am

Hey Suresh,

Whichever way you look at it, you're going to need to do a lookup twice with Get-BrokerDesktop to identify the site where the system actual system is located if it begins with UK, because it can be in one of two.

I'd suggest something like below. What you are doing is that if the name supplied begins with UK, it does a check to see if it doesnt exist at UKSite1. If not, it will get the details from UKSite1.

I've gone on the basis that you only need $Adminaddress because you want to do a lookup once, so am not setting it if is a UK address, since you need to lookup both servers.

Let me know if that is the type of thing you are try to do, or if i'm barking up completely the wrong tree!

#requires -Version 1
$Desktop = Read-Host -Prompt ' Enter your Desktop name'

if ($Desktop -like 'UK*')
{
    If (!($DesktopDetail = Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress 'uksite1')) 
    {
        ($DesktopDetail = Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress 'uksite2')
    }
}
    
Else
{
    if ($Desktop -like 'US*') 
    {
        $Adminaddress = 'ussite1.com'
    }
    if ($Desktop -like 'SG*') 
    {
        $Adminaddress = 'sgsite1.com'
    }
    $DesktopDetail = Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress $Adminaddress 
}

Write-Output -InputObject $DesktopDetail

July 1, 2015 at 4:05 am

Another possible approach:

$Desktop = Read-Host -Prompt ' Enter your Desktop name'

switch -wildcard ($Desktop) {
    "UK*"{$servers = @("UKSite1.com", "UKSite2.com")}
    "US*"{$servers = @("USSite1.com")}
    "SG*"{$servers = @("SGSite1.com")}
}

$DesktopDetail = foreach ($server in $servers) {
    Get-BrokerDesktop -machinename domain\$Desktop -Adminaddress $server
}

if ($DesktopDetail) {
    $DesktopDetail
}
else {
    "No machine with name {0} found on server(s): {1}" -f $Desktop, ($Servers -join ",")
}

You could search one site and check to see if it returned a result and then break the loop, but it's only 2 searches for a specific desktop, so it should return quickly.

July 1, 2015 at 7:18 am

Hi Tim/Rob

Thank you so much guys . Both methods worked like a charm 🙂 .. This is my first post in powershell.org..

i am really amazed to see reply in one hour.. I have also learned something new from both scripts.

Thanks for taking your valuable time to answer my queries.