Unable to get expected result in hashtable
Hi Everyone,
I am unable to get the expected result when using a hash table as i have a need to export the results to a CSV file but Write-Host works fine, below is the script
$workergroups = Get-XAWorkerGroup
foreach ($workergroup in $workergroups)
{
$session = Get-XAServer -WorkerGroupName $workergroup |get-xasession
Write-host $workergroup"," `t $session.count
}
I tried the below but not getting the same result as Writ-Host, tried multiple options but no luck. Can anyone help me out here.
Function Get-WSessioncount {
$workergroups = Get-XAWorkerGroup
foreach ($workergroup in $workergroups)
{
$session = Get-XAServer -WorkerGroupName $workergroup |get-xasession
$wgroup = [PSCustomObject]@{WorkerGroupnaame = $workergroup; Sessioncount = $session.count;};
}
# OUTPUT object
$wgroup |FT WorkerGroupname, Sessioncount -Autosize
}
Get-wsessioncount
-
Edited by
Dinesh Kumar C
Wednesday, January 28, 2015 11:52 AM
January 28th, 2015 2:50pm
Get rid of the Format-Table in your function, and just return the object.
Function Get-WSessioncount {
$workergroups = Get-XAWorkerGroup
foreach ($workergroup in $workergroups)
{
$session = Get-XAServer -WorkerGroupName $workergroup |get-xasession
[PSCustomObject]@{WorkerGroupnaame = $workergroup; Sessioncount = $session.count;};
}
}
Get-wsessioncount
If you want a table format when you output it to the console, specify that after you call the function and get your object back:
Get-wssessioncount | |FT WorkerGroupname, Sessioncount -Autosize
January 28th, 2015 3:17pm
Hi
mjolinor,
Still no result, i am just getting a blank screen.
January 28th, 2015 4:02pm
Function Get-WSessioncount {
Get-XAWorkerGroup |
ForEach-Object{
Write-Host "WORKERGROUP:$_" -fore green
$session=Get-XAServer -WorkerGroupName $_ |get-xasession
[PSCustomObject]@{
WorkerGroup=$_
Sessioncount=$session.count
}
}
}
Get-WSessioncount
January 28th, 2015 6:55pm
Hi JRV, Yep it worked for me, thanks a lot.. and below are the results.Also, What should i need to do to get a result in Workergroup, Sessioncount format?
WORKERGROUP:Controller
Name Value
---- -----
WorkerGroup Office
Sessioncount 15
WORKERGROUP:Office 2010
WorkerGroup General
Sessioncount 4066
WORKERGROUP:Desktop
WorkerGroup Desktop
Sessioncount 834
January 28th, 2015 8:16pm
Can someone assist me getting the above out put column wise.
January 28th, 2015 10:31pm
Function Get-WSessioncount {
Get-XAWorkerGroup |
ForEach-Object{
$session=Get-XAServer -WorkerGroupName $_ |get-xasession
[PSCustomObject]@{
WorkerGroup=$_
Sessioncount=$session.count
}
}
}
Get-WSessioncount | Format-Table -auto
Get-WSessioncount | Export-Csv <filename>
Works fine fo
January 28th, 2015 10:38pm
Tried the above and only getting
IsReadOnly IsFixedSize IsSynchronized Keys Values SyncRoot Count
FALSE FALSE FALSE System.Collections.Hashtable+KeyCollection System.Collections.Hashtable+ValueCollection System.Object 2
-
Edited by
Dinesh Kumar C
Wednesday, January 28, 2015 8:11 PM
January 28th, 2015 11:10pm
January 28th, 2015 11:51pm
My Code:
ADD-PSsnapin Citrix.* -ErrorAction SilentlyContinue
Function Get-WSessioncount {
$workergroups= Get-XAWorkerGroup
ForEach ($workergroup in $workergroups)
{
# Write-Host "WORKERGROUP:$_" -fore green
$session=Get-XAServer -WorkerGroupName $workergroup |get-xasession
[PSCustomObject]@{
WorkerGroup=$workergroup; Count = $session.count+0;
}
}
}
Get-WSessioncount
Result i am getting:
Name Value
---- -----
WorkerGroup Control
Count 60
WorkerGroup Office
Count 3021
WorkerGroup Desktop
What i am looking for is that i need the result column wise (Workergroup, Count) and need the output in a CSV file.
January 29th, 2015 12:46am
What version of PowerShell are you running.
This will produce correct objects and not hashes on a properly installed system.
ADD-PSsnapin Citrix.*
Function Get-WSessioncount {
$workergroups = Get-XAWorkerGroup
ForEach ($workergroup in $workergroups) {
$session = Get-XAServer -WorkerGroupName $workergroup | get-xasession
[PSCustomObject]@{
WorkerGroup=$workergroup
Count=$session.count
}
}
}
Get-WSessioncount Select-Object WorkerGroup,Coun
We can do an alternate that may avoid your broken issues.
ADD-PSsnapin Citrix.*
Function Get-WSessioncount {
$workergroups = Get-XAWorkerGroup
ForEach ($workergroup in $workergroups) {
$session = Get-XAServer -WorkerGroupName $workergroup | get-xasession
$props@{
WorkerGroup=$workergroup
Count=$session.count
}
New-Object PsObject -Property $props
}
}
Get-WSessioncount Select-Object WorkerGroup,Count
Be sure to start a new PowerShell CLI session before running the code. Do not disable errors inb any way.
January 29th, 2015 1:20am
Hi Jrv,
I am using 2.0 and the second script works cool and i am getting the result column wise. Thanks for your help. Also, i learned some thing here which i can use it for future reference and stop bugging you guys in some way.
Thanks for your help again..
January 30th, 2015 1:13am
You cannot use [pscustomobject] or [ordered] in PS 2.0.
January 30th, 2015 2:16am