VBS: Is it possible to retrieve systemdrive & windowsdirectory and enter those into variables?

I've put together a simple vbs to obtain the System Drive, and WindowsDirectory. What I'm not sure of now is how to pass the retrieved information into a flag for a command to run.

Here is what I have so far.

"On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

arrComputers = "."
For Each strComputer In arrComputers
   
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
      WScript.Echo "SystemDrive: " & objItem.SystemDrive
      WScript.Echo "WindowsDirectory: " & objItem.WindowsDirectory
      WScript.Echo
   Next
Next
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "cacls C:\Windows /E /C /G Users:R", 6, True
set wshshell = Nothing"

What I would like to do is replace the C:\Windows in the wshshell.run with the values returned from the SystemDrive & WindowsDirectory.

Any examples or a point in the right direction?
ALH78Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ALH78Author Commented:
Instead of :
Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "cacls C:\Windows /E /C /G Users:R", 6, True
set wshshell = Nothing"

Can I just use:
Set oProc=oShell.Exec("cacls %windowsdirectory% /E /C /G Users:R")

Anything I should look for if I want to run more than one program after retriveing the systemdrive/windowdirectory?
0
hesCommented:
Dim SD
Dim WD
SD = objItem.SystemDrive
WD = objItem.WindowsDirectory

wshshell.run "cacls " &SD & WD &   " /E /C /G Users:R", 6, True
'I did not check if SD or WD are formatted properly with the trailing \
0
ALH78Author Commented:
When I checked the objitem.WindowsDirectory it included the system drive already in it.

But for some reason using wshshell.run "cacls " & WD & " /E /C /G Users:R", 6, True isn't working.

The permisions don't change after running it.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

WiGgLrCommented:
To get the windows directory into a string, use this code

Dim WSHSHell
Dim strWinDir

Set WSHShell = CreateObject("WScript.Shell")
strWinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")


you can then use the string the way you want like this:

WSHShell.Run "cacls " & strWinDir & " /E /C /G Users:R", 6, True



0
JesterTooCommented:
Here is a modified version of your script.  It originally contained a fatal error that was being hidden by the use of the "on error resume next" statement.  I've left the problem lines in the code but commented out with 3 apostrophes.  Also, it looks like this code was originally intended to retrieve values from multiple REMOTE computers (not the local machine the script is running on).  However, I can't tell if you want to run cacls on the remote machines or on the local one.  If it should run locally, then the script could be simplified a bit.  If it should run against remote machines, then there is some more work that must be done since the "run" method is presently being executed locally (you may need to use "remote scripting" which will require COM to be configured on each target machine to allow that).

HTH,
Lynn

'''On Error Resume Next                                ' This should NOT be used except in certain limited circumstances!!!

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Dim arrComputers
Dim strComputer
Dim wshShell
Dim objWMIService
Dim colItems
Dim objItem

Set wshShell = WScript.CreateObject ("WSCript.shell")
'''arrComputers = "."
arrComputers = array(".")
For Each strComputer In arrComputers

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
   '''wshshell.run "cacls C:\Windows /E /C /G Users:R", 6, True
      wshshell.run "cacls " & objItem.WindowsDirectory & " /E /C /G Users:R", 6, True
   Next
Next
Set objItem = Nothing
Set colItems = Nothing
Set objWMIService = Nothing
Set wshshell = Nothing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WiGgLrCommented:
This question has been open for a while now, are you still having problems?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.