Page 1 of 2 12>
Topic Options
#161685 - 2006-05-06 12:32 PM Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Anyone have a good (long-term) approach to checking if one is running in a VM-Ware session. By long-term I mean - a method that is not likely to change with newer versions of VM-Ware ... any help, suggestions or just plain-old thoughts would be welcome.

-Shawn

Top
#161686 - 2006-05-06 03:58 PM Re: Running in VMWare ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
That is a good question. I have sent an email to our VM-Ware expert to see if he can shed some light on the subject. I also might have a need to determine if a computer is a virtual computer or not.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#161687 - 2006-05-06 04:25 PM Re: Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Thanks Howard, much appreciated. I have to come up with a method by Monday morning - hope your expert reads his email on the weekend ;0)
Top
#161688 - 2006-05-06 04:35 PM Re: Running in VMWare ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
What is reason you need to make this identification?
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#161689 - 2006-05-06 04:37 PM Re: Running in VMWare ...
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
In the short term can't you use ip address? If I understand the virtual computer has an ip address in a range close to the base address of the VM server.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#161690 - 2006-05-06 04:44 PM Re: Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Writing a package that will be adding varying global groups into a local group, depending on whether running in vmware or not. The ip-address thing sounds dodgey to me. It has to flawless.
Top
#161691 - 2006-05-06 08:36 PM Re: Running in VMWare ...
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Not sure it's 100%, but it worked in my VMWare test sessions.

Code:
Break On


if isVMWare
? "VMWare Session"
else
? "Not VMWare Session"
endif




function IsVMWare
dim $objWMIService, $colItem, $objItem
$objWMIService = GetObject("winmgmts:\\.\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_BIOS")
For each $objItem in $colItems
if instr($objItem.SerialNumber,"VMWARE")
$ISVMWare=1
endif
Next
Endfunction


Top
#161692 - 2006-05-06 08:51 PM Re: Running in VMWare ...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
MyComputerInfo script actually checks this for you Shawn.

Not the full code but something like this which also checks if the Microsoft Virtual PC is running.

Haven't ran it for awhile though so not positive if it works on all the new versions.
I wrote this about 3 years ago originally.


Code:
$VMWUser            = Trim(ReadValue($HKLMSMWCV + '\Run','VMware User Process'))
$VMWPath = ReadValue($HKLMSMWCV + '\App Paths\vmware.exe','Path')
$VMWVer = GetFileVersion($VMWPath +'\vmware.exe','ProductVersion')
$VMWToolsVer = GetFileVersion(ReadValue($HKLMSCCS + '\Services\VMware Tools Service','ImagePath'),'ProductVersion')
$CVMVer = GetFileVersion(Left(ReadValue('HKLM\SOFTWARE\Classes\Virtual.PC.Floppy\DefaultIcon',""),-5),'ProductVersion')
$CVMMapper = ReadValue($HKLMSCCS + '\Services\VPCMap','DisplayName')
$CVMMapperVer = GetFileVersion(ReadValue($HKLMSCCS + '\Services\VPCMap','ImagePath'),'ProductVersion')



Edited by NTDOC (2006-05-06 08:53 PM)

Top
#161693 - 2006-05-06 10:20 PM Re: Running in VMWare ...
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
Win32_ComputerSystem also has two fields that may be useful:

Manufacturer
Model

Code:
 
Manufacturer: VMware, Inc.
Model: VMware Virtual Platform


Top
#161694 - 2006-05-07 12:01 AM Re: Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Doc, those values - they tell if your running within a vmware session ? or whether you have wmware installed on your machine ? Allen - I really think you hit the nail on the head there.
Top
#161695 - 2006-05-07 02:14 AM Re: Running in VMWare ...
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
I just tested the UDF on a PC with just the VMWare Player installed and it worked in that session too.
Top
#161696 - 2006-05-07 07:41 AM Re: Running in VMWare ...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
It tells you both if you have it installed and if you're running it.

The WMI method is good too on current versions but you said you wanted something that was working on what would amount to older and newer versions, not so sure that exact same code would work on the older versions or not.

At the time back when I wrote it (as I recall) the names of the service were different on different versions or something like that that disuaded me from using that method.
That may no longer be the case. VMware was not quite as large or "Corporate" back when I wrote the code.


If I were to do it now days I'd go with WMIConfirm() and VMwareState($Running) sort of code.
 

Top
#161697 - 2006-05-07 09:23 AM Re: Running in VMWare ...
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4563
Loc: USA
...but he wants to know if you are actually IN the vmware session....
Top
#161698 - 2006-05-07 10:31 AM Re: Running in VMWare ...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Quote:

It tells you both if you have it installed and if you're running it.



Top
#161699 - 2006-05-07 11:13 AM Re: Running in VMWare ...
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
What VMware? Since there is Workstation, (GSX) Server which has to be installed on a host OS and there is ESX Server which has its own kernel... Take a look at www.VMTN.net, You will find there a great forum and knowledge base...
_________________________
Co


Top
#161700 - 2006-05-07 11:40 AM Re: Running in VMWare ...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Here Shawn,

This code will tell you IF you're running under a VMware hosted Windows session.
Not tested on GSX/ESX but should work on them as well due to the nature of the check.

If the system is not running the VMware Tools then it's not setup very well, and if it's
not running the VMware Tray then it's been highly modified and is unlikely.

This method should work even if WMI is broken and a little faster as well.
 


Dim $VMRunning
$VMRunning = WithinVMware()
If $VMRunning = 1
'Logon is running under a VMware session.' ?
Else
'Logon is NOT running under a VMware session.' ?
EndIf

Function WithinVMware()
Dim $InWMware
$InWMware = 0
Select
Case 1 = KeyExist('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\VMip.exe')
$InWMware = 1
Case 1 = KeyExist('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\VMwareTray.exe')
$InWMware = 1
Case 1
EndSelect
$WithinVMware = $InWMware
EndFunction

Top
#161701 - 2006-05-07 12:28 PM Re: Running in VMWare ...
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
If you don't like that method you could use one like this then or a combination of all the suggestions posted here.

Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

Dim $VMRunning,$Item
$VMRunning = VMwareRunning()
If @ERROR
'Error checking VMware Service State. ' + @ERROR + ' - ' + @SERROR ?
Else
For Each $Item In $VMRunning
If $Item
$Item ?
EndIf
Next
EndIf

Function VMwareRunning()
Dim $VMTools $VMTools = 0
Dim $VMProcess $VMProcess = 0
Dim $VMToolsRunning $VMToolsRunning = 0
Dim $Key, $ChkWMI
$Key = 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'
$VMTools = ReadValue($Key,'VMware Tools')
$VMProcess = ReadValue($Key,'VMware User Process')
If 1 = KeyExist('HKLM\SYSTEM\CurrentControlSet\Services\VMTools')
$ChkWMI = WMIConfirm()
If @ERROR
Exit @ERROR
Else
If $ChkWMI > 1
$VMToolsRunning = fnWMIService('VMTools','STATE')
EndIf
EndIf
Else
$VMwareRunning = 0
EndIf
$VMwareRunning = $VMTools, $VMProcess, $VMToolsRunning
EndFunction

Function WMIConfirm(optional $sComputer)
Dim $objWMIService, $objWMISetting, $colWMISettings
$sComputer = IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!"+$sComputer+'root\cimv2')
; Failed - return 0 and exit with error value
If @ERROR
$WMIConfirm = 0
Exit Val('&' + Right(DecToHex(@ERROR), 4))
EndIf
$colWMISettings = $objWMIService.ExecQuery("Select * from Win32_WMISetting")
For Each $objWMISetting In $colWMISettings
$WMIConfirm = $objWMISetting.BuildVersion
Next
Exit 0
EndFunction

Function fnWMIService($sService,$sMethod,Optional $sComputer)
Dim $objWMI,$objSrvc,$nul
If Not $sComputer $sComputer=@WKSTA EndIf
$objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"+$sComputer+"\root\cimv2")
If @ERROR<0 Exit VAL("&"+Right(DecToHex(@ERROR),4)) EndIf
$objSrvc = $objWMI.ExecQuery('Select * from Win32_Service WHERE Name = "'+$sService+'"')
For Each $objSrvc in $objSrvc
$nul=Execute("$"+"fnWMIService = $"+"objSrvc."+$sMethod)
Next
EndFunction

Top
#161702 - 2006-05-07 10:13 PM Re: Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Ok guys - I have tested all the solutions and they all work great. Will be having a meeting on Monday to decide which one to go with ... also, found this other method using srvinfo:

srvinfo.exe | find /I "SYSTEM BIOS VERSION" | find /i "PTLTD" >nul

Thanks guys.

-Shawn

Top
#161703 - 2006-05-07 11:11 PM Re: Running in VMWare ...
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
would think that could be reachable with wmi's bios class as well.
_________________________
!

download KiXnet

Top
#161704 - 2006-05-08 04:50 PM Re: Running in VMWare ...
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Just by way of an update. Just finished our meeting. We had four options on the table:

1) Use script and WMI (allen)
2) Check registry run values (ntdoc)
3) Use srvinfo | find
4) Check for VMTools service (ntdoc)

We liked options 1 and 2 equally - but decided to go with option #2 because we cant call-out to vb-script using the packaging method that we are using (some kind of install-shield thing).

Thanks for everyone's input.

-Shawn

Top
Page 1 of 2 12>


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 519 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.194 seconds in which 0.109 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org