WMI script for find correct System Volume

Discussion in 'Windows 7' started by Mr Jinje, Aug 25, 2009.

  1. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #1 Mr Jinje, Aug 25, 2009
    Last edited by a moderator: Apr 20, 2017
    I write little VBS hook to pull the SystemVolume from the OS. Should be language independent as it is based on the SystemVolume Property (true or false).

    Filter the results for "SystemVolume: True" to find the correct volume.

    Here how should run it, change path to your needs. Send results to text will prevent 100 little pop up echo windows.

    cscript c:\SystemVolume.vbs > C:\SystemVolumeResults.txt

    SystemVolume.vbs

    Code:
    
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Volume WHERE SystemVolume = True",,48) 
    For Each objItem in colItems
        Wscript.Echo "-----------------------------------"
        Wscript.Echo "Win32_Volume instance"
        Wscript.Echo "-----------------------------------"
        Wscript.Echo "BootVolume: " & objItem.BootVolume
        Wscript.Echo "Capacity: " & objItem.Capacity
        Wscript.Echo "Compressed: " & objItem.Compressed
        Wscript.Echo "Description: " & objItem.Description
        Wscript.Echo "DriveLetter: " & objItem.DriveLetter
        Wscript.Echo "DriveType: " & objItem.DriveType
        Wscript.Echo "FileSystem: " & objItem.FileSystem
        Wscript.Echo "FreeSpace: " & objItem.FreeSpace
        Wscript.Echo "Label: " & objItem.Label
        Wscript.Echo "SystemVolume: " & objItem.SystemVolume
    Next
    
    
     
  2. nononsence

    nononsence MDL Addicted

    Aug 18, 2009
    806
    826
    30
    #2 nononsence, Aug 25, 2009
    Last edited by a moderator: Apr 20, 2017

    nice, Works for me.
     
  3. Hazar

    Hazar MDL Guru

    Jul 29, 2009
    2,507
    456
    90
    Did ou use WMI code creator? It makes things like this really easy.
     
  4. ifeife2

    ifeife2 MDL Member

    Apr 25, 2008
    134
    3
    10
    #4 ifeife2, Aug 25, 2009
    Last edited by a moderator: Apr 20, 2017
    Great work! This is really needed to break away from language differences.

    If you tell BCDEDIT to create a new entry do you know how to capture that very long GUID into a variable in VBS or .BAT ?

    Code:
    The entry {27385c40-b336-11dc-a4e7-9b7501ef3253} was successfully created
    
    I think someone said its possible to add a new entry using WMI, but they said it was like a LOT of lines of code.
     
  5. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #5 Mr Jinje, Aug 25, 2009
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Interesting.

    Would take many lines with WMI (everything seems too), but from command line is less work. Quickest dirtiest way is.

    bcdedit /create {grldr} /d "Windows 7" > C:\SomeFile.txt

    Then use a For /F Do to get and manipulate the results from SomeFile.txt, Easy way is use the SPACE as your delimiter and GUID should be third string.

    Tried pipe BCDEdit directly to the variable, with both | and >& commands, but BCDedit did not likes it (some apps don't play nice). Guess need spend more than 5 minutes to make working correctly. But it's definite no impossible.
     
  6. ifeife2

    ifeife2 MDL Member

    Apr 25, 2008
    134
    3
    10
    #6 ifeife2, Aug 26, 2009
    Last edited by a moderator: Apr 20, 2017
  7. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #7 Mr Jinje, Aug 26, 2009
    Last edited: Aug 26, 2009
    (OP)
    So do I pass your test, obviously you knew the answer before you asked.
     
  8. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #8 Mr Jinje, Aug 27, 2009
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Put another 5 minutes into this for everyone, here is new snippet, it auto assigns the system volume to R:\ letter.

    Code:
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colVolumes = objWMIService.ExecQuery _
        ("Select * from Win32_Volume Where SystemVolume = True")
    
    For Each objVolume in colVolumes
        objVolume.DriveLetter = "R:"
        objVolume.Put_
    Next
    
     
  9. nononsence

    nononsence MDL Addicted

    Aug 18, 2009
    806
    826
    30
    #9 nononsence, Aug 27, 2009
    Last edited by a moderator: Apr 20, 2017
    That one didn't work, try this one

    also this works
     
  10. ifeife2

    ifeife2 MDL Member

    Apr 25, 2008
    134
    3
    10
    #10 ifeife2, Aug 27, 2009
    Last edited: Aug 27, 2009
    I'm sorry to appear rude. I really DID NOT KNOW how to do this. I was able to google after you told me what commands to use (FOR /F). I am just a thorough forum poster. For example, I want to give links to stuff I take from other forums, etc. Trust me, I am learning from you, not the other way around ;-)

    I am still having problems with this partition/bcd stuff. My current task is to find out how to add an OS and then grub entry into the bcd store using WMI (I've no idea how yet).
     
  11. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #11 Mr Jinje, Aug 27, 2009
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Just give you the hard time :D Sarcasm never works over the internet.

    Well if you must do it inside of WMI/VBS. Why not just encapsulate each CMD separately. See how I run the robocopy from WMI.

    Code:
    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run("robocopy %windir%\Setup\Scripts\GRLDR R:\")
    
    With windows there is usually about 4-5 ways to do everything, assuming you know what you want done. Is this for a loader you are write so is multi-boot aware, or I guess I don't know what you are really try to do.
     
  12. ifeife2

    ifeife2 MDL Member

    Apr 25, 2008
    134
    3
    10
    #12 ifeife2, Aug 27, 2009
    Last edited: Aug 27, 2009
    Haha funny. Internet humour translation : )

    Well my real problem lies in the BCD store. I have so many entries in there and the OS chooser (bootmgr) never displays them on the boot menu, it just thinks they are invisible or something. The only way I know how to RELIABLY 100% of the time add an entry is to script a BCD store from scratch each time I want to patch a newly installed Win7. This is why I need to find the two partition letters (with WMI), so that they can be added back into the store. It would be WAY BETTER if I could just get a script that always works to add a single entry so I don't have to manually recreate the already existing ones that are getting erased when I create a new store.

    What happens now is:
    add new entry
    Bcdedit /displayorder {GUID} /addlast
    confirm with bcdedit /enum all /v
    reboot and it won't show it on the boot screen.
    I log on and confirm with bcdedit /enum all /v , the new entry is there BUT be usable boot time menu option for totally unknown reasons. I tried booting the vista DVD and doing a repair on the bcdstore, really I don't know what the cause could be.

    If you're curious about helping to fix this issue, I can work on screenshots the next time I try it.
     
  13. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    OK, so you problem is keep re-installing W7 onto secondary partition, and that you have Vista on the first partition. Every time you re-install it creates a new BCD entry and on next re-install you are left we ghost entry. And for whatever reason none of these show up as boot choices.

    Just a thought, here me out. Why not just grab EasyBCD, remove all the ghost entries. once that done, then wipe and reload you W7 and configure all you favorite settings/application. Then Sysprep it, and use ImageX to capture a WIM from the partition.

    Now that you have a sysprepped WIM, instead of re-installing, you can simply use imagex script to lay the image on your second partition. WIM installation is only file copy so it only take about 5 minutes total. After imagex done with filecopy, then machine is ready to boot.

    Added benefit, the installation is already configured to your liking, and will install on any machine (sysprep make hardware independent). Can probably be activate before sysprep and not have to mess with activation each re-install.

    Then just need open up EasyBCD and add entry to W7 on second partition and you all set. But I think this only be need done the first time. With sysprep wim, the installer will not be add new ghost boot entries.

    What do you think ?
     
  14. ifeife2

    ifeife2 MDL Member

    Apr 25, 2008
    134
    3
    10
    Actually I am working in the virtual machine world where it creates 2 partitions on a fake hard drive. And my other machine is a mac with HFS+ then NTFS and no extra partition. But even on the mac I still want to add grub via script (yea, im crazy). The BCD is my enemy, lol.
     
  15. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    Let's forget MAC for now. Just focus on VM -

    2 partitions in virtual world - you mean default install and format (100mb system reserved) + Windows 7 on second partition. Nothing complicated there, why even need to create BCD entry, Windows 7 DVD does all that during installation.

    and you are meaning Grub = grldr from act*vators, or full GRUB4DOS not hacked version - Makes a difference
     
  16. nononsence

    nononsence MDL Addicted

    Aug 18, 2009
    806
    826
    30
    #16 nononsence, Aug 28, 2009
    Last edited: Aug 28, 2009
    hey I did some more work on this script, so far it finds a freedrive letter
    and mounts the SystemVolume. now its time to start moving files around :)

    and here is the bit to unmount the SystemVolume

     
  17. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #17 Mr Jinje, Aug 28, 2009
    Last edited by a moderator: Apr 20, 2017
    (OP)
  18. nononsence

    nononsence MDL Addicted

    Aug 18, 2009
    806
    826
    30
    I had "path with spaces" issues
    and had to do this
    and couldnt make things like %windir% work
    so i had to do this

    still have to find a SystemVolume if it's mounted
    and error checking :)
     
  19. nononsence

    nononsence MDL Addicted

    Aug 18, 2009
    806
    826
    30
    #19 nononsence, Aug 28, 2009
    Last edited: Aug 29, 2009
    Finished :)

    script kiddies rejoice....
     
  20. Mr Jinje

    Mr Jinje MDL Expert

    Aug 19, 2009
    1,770
    1,101
    60
    #20 Mr Jinje, Sep 3, 2009
    Last edited by a moderator: Apr 20, 2017
    (OP)