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?
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
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>
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
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
Ciao
Ok, you need to install the add-on module follow the following link:
Run Example Horizon PowerCLI Scripts (vmware.com)
😊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
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
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>
Last question: Is there a possibility only export activ Sessions with Users in Automated pools?
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
Thanks for your support and script. can you help what should I add to remove the assigned user?
Thank you for writing this script. Very helpful!