Update: Wow my script got a mention on the PowerScripting podcast 🙂 Episode 101 – Matthew Giles from Connect-SMART « PowerScripting Podcast
After doing lots of work with VMware View recently I have decided to try and write a few Powershell scripts to assist with View rollouts / administration. This first small script will update a custom field for each VM with the current logged in user. This helps considerably when your are in the vSphere client with a large amount of VM’s and need to track down the user of a particular VM. Maybe you need to log in to check an issue their having, investigate high CPU usage or just quickly find out which VM is assigned to which users. Obviously this can be done through the View Manager but most people won’t have this open on a daily bases.
001
002 003 004 005 006 |
connect-viserver yourvc
$vms = get-vm *xp* ForEach($vm in $vms) {$QueryString = Gwmi Win32_ComputerSystem -Comp $vm $QueryString = $QueryString.userName if ($QueryString -eq $null) {$vm | Set-CustomField -Name “Logged in User” -Value "No One Logged In"} ELSE {$vm | Set-CustomField -Name “Logged in User” -Value $QueryString}} |
The script can either be ran as needed or scheduled to run using Windows Scheduled Tasks, for information on how to do this check out Alan Renouf’s blog post http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/
As the script uses WMI to get the information regarding the logged in users, you must ensure you run this as user with sufficient privileges over the machines you are querying.
I am using a wild card in the get-vm command to find all my VM’s that contain xp in the name, this can easily be changed to meet the requirements of your View environment.
If no one is logged in when the script runs the custom field is updated with “No One Logged In”
As ever this is a work in progress and I am sure their are numerous ways it could be improved so I look forward to your suggestions and comments.
Example of how it looks in a list
Single VM View
As ever many thanks to Alan Renouf and his blog www.virtu-al.net for the inspiration.
Update
I have mixed success with this script, sometimes I am getting the logged in user returned correctly other time not. Would be interested to head what anyone else’s experiences. It seems the logged in user isn’t always returned from WMI, this seems strange considering I am using a domain admins account.
I think this works a little better a combination of your script and this script:
http://my.opera.com/troyan3000/blog/2009/10/17/how-to-get-current-logged-on-user
Here it is
connect-viserver virtualcenter
Function Get-LoggedOn
{
$Comp = $args[0]
Get-WmiObject -computername $Comp -query “Select logonid from Win32_LogonSession where logontype = 2 or logontype = 10” |
foreach {Get-WmiObject -computername $Comp -query “Associators of {Win32_LogonSession.LogonId=$($_.logonid)} Where AssocClass=Win32_LoggedOnUser Role=Dependent”}
}
$vms = get-vm *xp*
foreach($vm in $vms)
{
$QueryString = Get-LoggedOn $vm | Select-Object -First 1
$QueryString = $QueryString.FullName
if ($QueryString -eq $null) {$vm | set-customfield -name “Logged in User” -value “No One Logged In”}ELSE{$vm | set-customfield -name “Logged in User” -value $QueryString}
}
Hi,
Here is another way to get pretty much the same thing – except I pull the data from view, rather than query the os in the vm.
http://www.blog.shonkyholdings.com/2010/09/view-powershell-and-adam-mapping-users.html