Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB script to remove user logon as service right

Posted on 2014-04-04
7
831 Views
Last Modified: 2014-05-19
Hi Everybody,

I need to write a VBscript that will take remove service account from “Log on as a service” policy on the local computer.

I found a VB script which give user logon as service right but I need a VB script to remove user account from “Logon as a service” on local computer.

Thanks.

Szuromi
0
Comment
Question by:Szuromi
  • 3
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 39979392
Use the same script you found, except modify it to find the line with

SeServiceLogonRight =

After it is found, find either ",domain\user" and replace it with nothing or
after it is found, find either "domain\user," and replace it with nothing or
after it is found, find either "pcname\user," and replace it with nothing or
after it is found, find either ",pcname\user" and replace it with nothing or
after it is found, find either "user," and replace it with nothing or
after it is found, find either ",user" and replace it with nothing or (yes, let's try to be complete :) ), because you don't want the rest to be replaced, as mine says:

SeServiceLogonRight = *S-1-5-20,ASPNET

which would mess up a couple of things if I just empy the whole string to
SeServiceLogonRight =

(meaning no one will have the right)
0
 

Author Comment

by:Szuromi
ID: 40002560
Hi Wizard,


Here is the vb script from ...
http://social.technet.microsoft.com/Forums/scriptcenter/en-US/bef48a92-6b59-481f-aab7-990ae2d4f31a/vbscript-to-give-user-logon-as-service-right?forum=ITCG

------------------------------------------------------
Username = <domain\username>  'modify with your username
Dim oShell
Set oShell = CreateObject ("WScript.Shell")
  oShell.Run "secedit /export /cfg config.inf", 0, true
  oShell.Run "secedit /import /cfg config.inf /db database.sdb", 0, true
FileName = "config.inf"
OrgStr = "SeServiceLogonRight ="
RepStr = "SeServiceLogonRight = " & Username & ","
Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("config.inf", 1,1,-1)
    strInputFile = inputFile.ReadAll
inputFile.Close
Set inputFile = Nothing
   
Set outputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile("config.inf",2,1,-1)
outputFile.Write (Replace(strInputFile,OrgStr,RepStr))
outputFile.Close
    Set outputFile = Nothing
   
oShell.Run "secedit /configure /db database.sdb /cfg config.inf",0,true
set oShell= Nothing

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("config.inf")
obj.DeleteFile("database.sdb")

-----------------

I tried to change it as you recommended but it does not seem to work for me. Would you please let me know what am I missing?

Thanks.
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 400 total points
ID: 40042017
This should be it:

Username = "domain\username"  'modify with your username

FileName = "config.inf"
Dim oShell 
Set oShell = CreateObject ("WScript.Shell")
oShell.Run "secedit /export /cfg " & Filename, 0, true 
oShell.Run "secedit /import /cfg config.inf /db database.sdb", 0, true

Set inputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(Filename, 1,1,-1)
    strInputFile = inputFile.ReadAll
inputFile.Close
Set inputFile = Nothing

splitstring = split(strInputFile,vbcrlf)
for each line in splitstring
	if instr(line,"SeServiceLogonRight") >  0 then
		OrgStr =  line
	end if
next
RepStr = Replace(OrgStr,Username,"")
RepStr = Replace(RepStr," = ,"," = ")
RepStr = Replace(RepStr,",,",",")

Set outputFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(Filename,2,1,-1)
outputFile.Write (Replace(strInputFile,OrgStr,RepStr))
outputFile.Close
Set outputFile = Nothing
    
oShell.Run "secedit /configure /db database.sdb /cfg config.inf",0,true
set oShell= Nothing

Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("config.inf") 
obj.DeleteFile("database.sdb")

Open in new window

0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40042079
Also, if using newer Windows, you don't have the correct rights when double clicking the vbs. So, run cmd in administrator mode, then use:
"cscript script.vbs"
0
 

Author Closing Comment

by:Szuromi
ID: 40075989
Thanks your help!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question