Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Stop/Restart Windows Services within VBA and How to replace mdb 3

Status
Not open for further replies.

ranshe

Programmer
Oct 5, 2004
27
US
1. How can I stop/restart Windows Services which are in Windows Administrative Tools automatically from within VBA?
2. How can you replace a remote mdb with another using VBA?
 
I am going to mark this one for email notice. I sure as hell hope this (#1 that is) can NOT be done. In my opinion that would be a huge security lapse - not that Microsoft is incapable of such. Having VBA able to directly affect OS Services is hacking. I have certain Services off, or disabled, for users. Heck, I have many Services disabled on my own account. If is is possible to restart them without verifiable admin access, or unknown to me, makes me shudder.

Gotta ask...WHY are you asking this? This does not appear, to me, to be in the scope of normal Office/VBA. What is the purpose of wanting to affect Services from within a VBA client?

Gerry
 
Essentially anything you can do on your computer you can do from VBA. It is a full featured language and environment. The only constraints that are applied are applied to the target functionality. There is an almost limitless number of source environments which could control Windows services - it simply isn't possible to restrict them all, you must restrict the services themselves.

As far as I know the only environment where there is serious restriction at source is the browser environment. Javascript, for example, has no mechanism for changing things outside its own environment - it just wouldn't be acceptable if it had.

You are assumed to have given your permission for VBA to run and shouldn't run anything if you don't know what it does. Once given your permission, the program can do anything - there is no half way house - you can't say, well, run, but don't do anything I wouldn't do - in practice it would be virtually impossible to control without making VBA a fraction of the language it now is.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
I stand humbled and afraid.

Sigh, a whole other area to explore. I have done very very little with WMI. I was afraid you guys would come back with that answer. So now I have to seriously explore this.

Oh well, something new to learn. Any suggestions as to getting me up to speed? PHV, thanks for the link. I will certainly check it out. Lordy, I feel so stupid sometimes.

Makes sense though, VBA IS full featured (as well I know), so sigh, here we go....

Thnaks for the heads up. I am only an egg.

Tony, you wrote:
You are assumed to have given your permission for VBA to run

Is this an implicit assumption, or is there somewhere to explicitly set this? In other words, is there someplace I can set an explicit "No!". We have a real hacking user here who is constantly making a mess of things. He is, in fact, one of the people who has certain Services disabled. He has enough brains to be dangerous, and not enough ethics OR common sense to be careful.

Gerry
 

Don't feel stupid, Gerry - nobody knows it all - though PHV comes close.

fumei said:
He has enough brains to be dangerous, and not enough ethics OR common sense to be careful.
I think I know him!

There are two ways the user gives permission:

[ol][li]For Add-Ins & Templates, you give, usually implicit, permission when you install them - as you know, of course, all you need to do to "install" is to put something in the right place; nonetheless anything "installed" is deemed to have permission.[/li][li]For documents, depending on your security settings (and Office release), you Enable/Disable macros at open time[/li][/ol]

AFAIK there is no global override you can set - but I certainly don't know it all. And, yes, it really isn't a very secure environment.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Anyway, you must have admin privileges to start/stop services.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Well .....whew on the admin priviledges. That lowers my blood pressure. OK, fine, that is....sort of Ok. Got to say that we have a few admin access people who, hmmmm, are shall we say, acting above their proper pay level. Plus we have a number of senior management type ( "the guy" mentioned earlier) who do not admin work, but insisted that they have admin priviledges. It is those people who most concern me.

I have since got hold of the Windows Scripting Guide, which is going to make for some light reading this weekend. Oh well, I am going off by myself for three days, up as high and remote as I can push my vehicle into the mountains. Reading it will keep my mind off the bears, and more dangerous those cougars.

Thank you gentlemen, a whole other world.

Gerry
 
Gerry,

I am the originator of this thread.
I understand your concerns. In my case though, I am the one with admin rights and I was needing to stop services and replace the mdb with a new one nightly.
In my situation, it is very secure. My boss wants the process automated and asked if I could make it happen.
Anyway thanks so much for your and others input. Also, thanks PHV.

NOTE: The other part of the question was how to replace an mdb with one of the same name. I cannot find the code command to do this. I could not even find how to delete an mdb via VBA.
 
Fair enough, as originator, you can certainly redirect us back on topic. Thank you for your indulgence.

My mind is drifting a little. Replace the mdb???

Assuming you have the replacement safely away in another folder, could you not simply delete the first one, and copy over the new one? This should work, especially as the new one will have the same name.



Gerry
 
Yes,
What is the code statement to do this deleting or copying.
At the risk of sounding stupid, I cannot find where you can replace one mdb with another of the same name.
I was using a compact/repair segment of code and when it came to writing back to the same folder with the same mdb name it simply gave me a message that it already existed and stopped on me.
I have searched for a new command to no avail. It probably is right in front of me but I cannot seem to see it.
 
Hi ranshe,

You should be able to do everything you want with the FileSystemObject object - the DeleteFile and MoveFile methods would be a good place to start looking.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Tony beat me to it, and is kicking my ass on another thread at the same time. That man is an octopus.

Yes. Use the FileSystemObject. Look it up in Help. It is not that difficult, the examples there are good. NOTE though, you will need to add a reference to the Scripting Runtime Library.

In the Visual Basic Editor,

Tools > References then scroll a loooooog way down, until you find Microsoft Scripting Runtime. Click the check box beside it, and click OK. You will now have a reference that allows you to use the FileSystemObject. Again, see Help. It is not hard.

Gerry
 
ranshee,
One thing that's not been said yet, but is fairly self explanatory, is that you won't be able to copy over a file that is already open, ie you are running some code from file1.mdb you won't be able to copy a new file1.mdb over the top of it. File1.mdb can copy NewFile2.mdb over OldFile2.mdb, as long as OldFile2.mdb isn't opened by anyone else.

fumei said:
We have a real hacking user here who is constantly making a mess of things
Sorry to take this a bit off thread, but if this guy is so dangerous, he needs a stern talking to by someone above. Doing things you're not supposed to (like trying to start services you're not allowed to use) is a breach of any AUP worth it's salt.




Ben

----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
 
Oh, he has had a serious talking to from tech support. However, he is SENIOR management, as in the boss of technical support reports to someone, and THAT person reports to this guy.

Still, no one has the guts to really tell him, so I did.

I told him that:

1. All the stuff he has installed is so far beyond and away from our standard image that he can not expect support for unauthorized software.

2. Even for authorized software, he can no longer expect any technical support beyond blowing everything away, and re-imagining his machine.

3. This re-imaging will be done within 24 bours of a serious request for assistance; and will be done without warning OR any technical support backup of data from his computer.

As he insisted that he could do his own backup, and he DOES have full admin rights (except for stuff on the Exchange server (!!!), I believed that this was fair.

I am just waiting.....

Gerry
 
Give a man a long enough piece of rope....

----------------------------------------------
Ben O'Hara "Where are all the stupid people from...
...And how'd they get so dumb?"
rockband.gif
NoFX-The Decline
----------------------------------------------
Want to get great answers to your Tek-Tips questions? Have a
 
I have been testing the code from the link found in the 3rd response(from PVS)in this thread. The link is to the "MS 2000 Scripting Guide".

The code with preceding comments is as follows:

Scripting Steps
The scripts for stopping and starting dependent services perform similar steps but in the opposite order
Stopping dependent services

Listing 15.16 contains a script that stops the IIS Admin Service and all its dependents. To carry out this task, the script must perform the following steps:

1. Create a variable to specify the computer name.

2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

3. Use the ExecQuery method to query the Win32_Service class.

This query must use an Associators of query and specify the following information:

• The instance of the service on which the query is performed (Win32_Service.Name = 'IISAdmin').

• The name of the Association class (AssocClass = Win32_DependentService). If the class name is not specified, the query returns all associated classes and their instances.

• The role played by the IISAdmin Service. In this case, IISAdmin is Antecedent to the services to be returned by the query.


The query returns a collection consisting of all the services dependent on the IIS Admin Service.

4. For each service in the collection, use the StopService method to stop the service.

5. After a stop control has been sent to each dependent service, pause for 60 seconds (60,000 milliseconds) to give the SCM time to stop each service.

6. Use a the ExecQuery method to retrieve the instance of the IISAdmin Service.

7. Use the StopService method to stop the IISAdmin Service.


Listing 15.16 Stopping a Service and Its Dependents


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Service.Name='
iisadmin'} WHERE " _
& "AssocClass=Win32_DependentService Role=Antecedent" )
[COLOR=red yellow]For Each objService in colServiceList

errReturn = objService.StopService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='[/color]iisadmin'")
For Each objService in colServiceList
errReturn = objService.StopService()
Next


I did not include the restart proceedure.

The Problem I am having:
Since I do not have the service 'iisadmin' I switched the service in, orange above, to one I had (which, by the way, has no Dependent Services): 'DHCP Client'. This was for testing purposes only. When I ran the code I got the following error message relating to the line in [COLOR=red yellow]Red highlighted[/color] above:
[COLOR=red yellow] Run-time error
'-2147217406(80041002)':
Automation error[/color]
On the error line the "objService" would be 'empty' when I would hover the mouse pointer over it.

In my debugging process I tried to comment out various portions of code and other such things to no avail. I also tried to read up on the problem at the link and other places with out success.

If anyone would like to take a stab at it I would greatly appreciate your help.
Thanks!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top