danielkrause
Enthusiast
Enthusiast

VMWare PowerCLI & Horizon Assigned User

Jump to solution

Hi volks,

we are using VMWare PowerCLI to export information of specific machines for an external inventory tool.

There is one information that is missing:

I need the assigned user of the horizon machine. 

Any ideas how to grab the user using PowerCLI? 

0 Kudos
2 Solutions

Accepted Solutions
fabio1975
Expert
Expert

Ciao 

Try this script 

$vms = Get-HVMachineSummary -PoolName "<Pool Name>"

$results = @()

foreach($vm in $vms){

$properties = @{

UserName = $vm.NamesData.UserName

MachineName = $vm.Base.Name

VDIPool = $vm.NamesData.DesktopName

}

$results += New-Object psobject -Property $properties

}

$results | export-csv -NoTypeInformation -path  c:\Test.csv 

Fabio
BLOG:https://vmvirtual.blog

if satisfied give me a kudos

View solution in original post

danielkrause
Enthusiast
Enthusiast

Great!!! I modified it for also showing me VM Hardware Information.

I#This is the Destination Folder for the Export File
$LocationPath = "\\SERVER\path\"

#This is the function for Export my Data
function Get-PoolInfo{
Param (
$a
)

$vms = Get-HVMachineSummary -PoolName $a
$results = @()
foreach($vm in $vms){
write-Host $vm.Base.Name
$properties = @{
MachineName = $vm.Base.Name
VDIPool = $vm.NamesData.DesktopName
UserName = $vm.NamesData.UserName
GB = get-vm -Name $vm.Base.Name | Get-Harddisk | Select-Object -ExpandProperty CapacityGB
CPUs = get-vm -Name $vm.Base.Name | Select-Object -ExpandProperty NumCPU
RAM = get-vm -Name $vm.Base.Name | Select-Object -ExpandProperty MemoryMB
OS = (get-vm -Name GE-TEAMEDS-0013 | Get-Harddisk | Select-Object @{N="OS";E={$_.Parent.Guest.OSFullName}}) -replace "@{OS=", "" -replace "}", ""
Datastore = (get-vm -Name $vm.Base.Name | Get-Harddisk | Select-Object @{N="DS";E={$_.Filename.Split(']')[0].Trim('[')}}) -replace "@{DS=", "" -replace "}", ""

}
$results += New-Object psobject -Property $properties
}
$FilePath = $LocationPath + "Machines_" + $a + ".csv"
$results | export-csv -NoTypeInformation -path $FilePath
}

# Execute the function (Get-PoolInfo <Poolname>)
Get-PoolInfo <Poolname>

 

View solution in original post

8 Replies
fabio1975
Expert
Expert

Ciao 

Have you tried this command?

 

get-hvmachinesummary -PoolName "<PoolName>" | Ft -AutoSize

Or if you want to see the active sessions:

 

$query = New-Object "Vmware.Hv.QueryDefinition"

$query.queryEntityType = 'SessionLocalSummaryView'

$qSrv = New-Object "Vmware.Hv.QueryServiceService"

$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) |

Select -ExpandProperty Results |

Select -ExpandProperty NamesData |

Select-Object -Property UserName,DesktopType,DesktopName,MachineOrRDSServerDNS

 

 

 

Fabio
BLOG:https://vmvirtual.blog

if satisfied give me a kudos
danielkrause
Enthusiast
Enthusiast

Thank you sooo much.😊

 

The 1st Command is not working, don't know why:

get-hvmachinesummary : The term 'get-hvmachinesummary' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

 

 

The 2nd Command is not bad, but i need to see all Machines and not only open session and i need to filter for a pool

0 Kudos
fabio1975
Expert
Expert

Ciao 

Ok, you need to install the add-on module follow the following link:

Run Example Horizon PowerCLI Scripts (vmware.com)

 

 

 

 

Fabio
BLOG:https://vmvirtual.blog

if satisfied give me a kudos
danielkrause
Enthusiast
Enthusiast

😊Looks better, now i can use the command and i see all information.

get-hvmachinesummary -PoolName "XXXX"

 

...but i cannot add a "|" command.

I would like to filter or export to CSV, but if i do so, i get only clear data  

Like that:
| Sort-Object -Property Machine
| Export-Csv -Path c:\Test.csv -NoTypeInformation
| Select-Object -Property Machine, User

 

0 Kudos
fabio1975
Expert
Expert

Ciao 

Try this script 

$vms = Get-HVMachineSummary -PoolName "<Pool Name>"

$results = @()

foreach($vm in $vms){

$properties = @{

UserName = $vm.NamesData.UserName

MachineName = $vm.Base.Name

VDIPool = $vm.NamesData.DesktopName

}

$results += New-Object psobject -Property $properties

}

$results | export-csv -NoTypeInformation -path  c:\Test.csv 

Fabio
BLOG:https://vmvirtual.blog

if satisfied give me a kudos
danielkrause
Enthusiast
Enthusiast

Great!!! I modified it for also showing me VM Hardware Information.

I#This is the Destination Folder for the Export File
$LocationPath = "\\SERVER\path\"

#This is the function for Export my Data
function Get-PoolInfo{
Param (
$a
)

$vms = Get-HVMachineSummary -PoolName $a
$results = @()
foreach($vm in $vms){
write-Host $vm.Base.Name
$properties = @{
MachineName = $vm.Base.Name
VDIPool = $vm.NamesData.DesktopName
UserName = $vm.NamesData.UserName
GB = get-vm -Name $vm.Base.Name | Get-Harddisk | Select-Object -ExpandProperty CapacityGB
CPUs = get-vm -Name $vm.Base.Name | Select-Object -ExpandProperty NumCPU
RAM = get-vm -Name $vm.Base.Name | Select-Object -ExpandProperty MemoryMB
OS = (get-vm -Name GE-TEAMEDS-0013 | Get-Harddisk | Select-Object @{N="OS";E={$_.Parent.Guest.OSFullName}}) -replace "@{OS=", "" -replace "}", ""
Datastore = (get-vm -Name $vm.Base.Name | Get-Harddisk | Select-Object @{N="DS";E={$_.Filename.Split(']')[0].Trim('[')}}) -replace "@{DS=", "" -replace "}", ""

}
$results += New-Object psobject -Property $properties
}
$FilePath = $LocationPath + "Machines_" + $a + ".csv"
$results | export-csv -NoTypeInformation -path $FilePath
}

# Execute the function (Get-PoolInfo <Poolname>)
Get-PoolInfo <Poolname>

 

danielkrause
Enthusiast
Enthusiast

Last question: Is there a possibility only export activ Sessions with Users in Automated pools?

0 Kudos
fabio1975
Expert
Expert

Ciao 

try this script:

 

Connect-HVServer <Connection Server Name>

$query = New-Object "Vmware.Hv.QueryDefinition"

$query.queryEntityType = 'SessionLocalSummaryView'

$qSrv = New-Object "Vmware.Hv.QueryServiceService"

$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query).Results |

Select-Object -Property @{Name = 'Session Start Time'; Expression = {$_.sessiondata.startTime}},@{Name = 'Display Protocol'; Expression = {$_.sessiondata.SessionProtocol}},@{Name = 'Username'; Expression = {$_.namesdata.username}},@{Name = 'Pool Name'; Expression = {$_.namesdata.desktopname}},@{Name = 'Machine Name'; Expression = {$_.namesdata.machineorrdsservername}},@{Name = 'Session Type'; Expression = {$_.sessiondata.sessiontype}},@{Name = 'State'; Expression = {$_.sessiondata.sessionstate}},@{Name="TypePool";Expression={$_.NamesData.DesktopType}} | Where {$_.State -eq "CONNECTED"} | Where {$_.TypePool -eq "AUTOMATED"} | Export-Csv -path <pathcsvfile> -NoTypeInformation

 

Fabio
BLOG:https://vmvirtual.blog

if satisfied give me a kudos