Need help with my script

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Suresh krishnan Suresh krishnan 1 year, 8 months ago.

  • Author
    Posts
  • #26983
    Profile photo of Suresh krishnan
    Suresh krishnan
    Participant

    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.

  • #26984
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    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
    
  • #26987
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

  • #26995
    Profile photo of Suresh krishnan
    Suresh krishnan
    Participant

    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.

You must be logged in to reply to this topic.