r/scripting • u/Keegcar • Oct 28 '15
VB script embedded in a BATCH file ?
Hello,
I need to write a batch script that will write taken variables of username and password, write them out to a file, and then use those variables of username and password to update Windows service credentials (using something like sc.exe). FINALLY, I also need to add the LOGON AS A SERVICE right to the account specified in the username and password variables (that would be variable #1 of the 2 variables passed).
So far I'm able to do this by writing a batch file with an embedded VB script.
- The script takes variables %1 and %2 and write out the required file :
echo username=%1 > .\startup.ini && echo password=%2 >> .\startup.ini sc config "Windows Agent Service" obj= %1 password= %2
- I have an embedded VB script that gets parsed out of the script, written to a file "temp.vbs" and then called with CSCRIPT:
:Dim strUserName,ConfigFileName,OrgStr,RepStr,inputFile,strInputFile,outputFile,obj
:strUserName = "%1"
: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
:ConfigFileName = "config.inf"
:OrgStr = "SeServiceLogonRight ="
:RepStr = "SeServiceLogonRight = " & strUserName & ","
: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")
findstr ":" "%~sf0">temp.vbs & cscript //nologo temp.vbs
The only issue is ... my VARIABLE that is in the parsed text is parsed as my $string and not as my variable...I'd be done if I could get the parsed text to replace %1 with my first variable that is passed to the batch file.
Can anyone help me with this ... or is there another way to accomplish my goals?
Thanks all!
View all comments
1
u/Get-ADUser Oct 29 '15
PowerShell