• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1402
  • Last Modified:

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?
0
ALH78
Asked:
ALH78
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
WiGgLrCommented:
This question has been open for a while now, are you still having problems?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now