Author Posts

September 7, 2015 at 9:03 pm

I have the below script which i am using to retrive information for a specific desktop .
Now i wanted to retrive details for all the destkop that i have it in a text file. Problem
is i could only get the one record of the desktop , not all records . Could some one help me what i am doing wrong here. Thanks.

$Desktops = get-content c:\temp\desktoplist.txt

foreach ($Desktop in $Desktops) {
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
    New-Object -Typename PSObject -property @{ DodName = $desktopdetail.DNSName
                                                                                 State = $destkopdetail.SessionState
                        }
}

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

September 7, 2015 at 10:50 pm

You assign $DesktopDetail variable outside of your cycle (and btw use it inside cycle!)
properly worked version of code can be like this

[...]
$haveresult = $false # error cheking if no result found at any server
foreach ([...]) {
  $desktopdetail = Get-BrokerDesktop [...]
  if ($desktopdetail) {
    $haveresult = $true
    New-Object [...] #use $desktopdetail here and output result to screen/pipeline
  }
}
if (-not $haveresult)
{
  write-error '[...]'
}
[...]

September 8, 2015 at 1:02 am

Hi Suresh,

You did not assign the object returned by Get-BrokerDesktop to a variable to access the properties DNSName and SessionState for your custom PSObject, and a closing curly bracket was missing to close the first foreach loop.

Try below:

Best,
Daniel

September 8, 2015 at 1:05 am

An alternative would be to pipe Get-BrokerDesktop to Select-Object and select only the properties you are interested in.

Example:

September 8, 2015 at 3:46 am

Thanks Daniel /Max

I will give it a try and let you know :)..

September 8, 2015 at 6:12 am

Hi Daniel
I have tried your method. But it is only showing the one desktop record. The text contains 10 desktop . Basically i am trying to export the result to CSV file. When i excecute this script it displays the result for all 10 but it does not store the result in the $desktopdetail . 🙁

September 8, 2015 at 6:22 am

That is correct. You will need an array variable outside of "foreach ($Desktop in $Desktops)" to collect the value of $DesktopDetail for each iteration of the loop.

Example:

September 8, 2015 at 6:49 am

Thanks Daniel Works perfectly . Cheers.
Max i have tried your method as well it works too . Thank you for spending your valuble time to help me 🙂 Cheers :):)