Solved

Script to Check for a file, if it is out of date, then run Setup. If up to date, END.

Posted on 2014-12-15
16
44 Views
Last Modified: 2015-08-19
Experts,

I have a script that runs setup based on a specific username and password. I need to keep this function, but I desire to add a little intelligence to it.

Ideal Script Solution
I need the script to check a file on the local machine for version. If the file contains txt with the value of "2.0.4.1028", then end script. If it doesn't have those values in the txt of the file, continue to script to run setup. Ff the file is not present, then abort.

File name to check for version in Text:
C:\Program Files (x86)\Malwarebytes Anti-Malware\changes.txt

Discoverable Values that show Version number in Txt file:
2.0.4.1028

Here is what I have so far in my script:
strUsername = "SetupAdmin	"
strPassword = "Cookies99"
strPath = "\\server\sestup\Upgrade_Mbam.bat"

Set objShell = CreateObject("wscript.shell")
objShell.run "cmd /c title RUNASINSTALL & runas /user:"&strUsername&" "&chr(34)&strPath&chr(34)
Success=False
For i=0 to 5 or Success
        wscript.sleep 100
        Success=objShell.AppActivate("RUNASINSTALL")
        i=i+1
Next
If Success then
        wscript.sleep 100
        objShell.SendKeys strPassword&"{ENTER}"
End if

Open in new window

0
Comment
Question by:Glenn M
  • 11
  • 5
16 Comments
 
LVL 11

Expert Comment

by:zalazar
ID: 40501551
Can you please try the following code.

strFile = "C:\Program Files (x86)\Malwarebytes Anti-Malware\changes.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFile) = False Then
  Wscript.echo "File " & strFile & " does not exist"
  Wscript.Quit 1
End If

Set fInput = fso.OpenTextFile(strFile , 1)
strReadAll = fInput.ReadAll
fInput.Close
Set fInput = Nothing
vCheck1 = Instr(LCase(strReadAll), "2.0.4.1028")
If vCheck1 > 0 Then
  Wscript.echo "Version info (2.0.4.1028) present in file " & strFile
  Wscript.Quit 0
End If
Wscript.echo "Continue"

Open in new window

0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503052
Checking...
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503066
It appears the script thus far, works.
When executed the script found 2.0.4.1028 in the Changes.txt and presented the following:
Found.bmp
When I changed the values to 2.0.4.1027 in the Changes.txt and ran the script, it presented the following:
Not.Found.bmp
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503395
Can you help me by putting our two scripts together, to work as I hope?  :)
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503633
I think I merged the two together properly, can you review for bad coding?
strUsername = "SetupAdmin	"
strPassword = "Cookies99"
strPath = "\\server\sestup\Upgrade_Mbam.bat"
strFile = "C:\Program Files (x86)\Malwarebytes Anti-Malware\changes.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFile) = False Then
  'Wscript.echo "File " & strFile & " does not exist"
  Wscript.Quit 1
End If

Set fInput = fso.OpenTextFile(strFile , 1)
strReadAll = fInput.ReadAll
fInput.Close
Set fInput = Nothing
vCheck1 = Instr(LCase(strReadAll), "2.0.4.1028")
If vCheck1 > 0 Then
  'Wscript.echo "Version info (2.0.4.1028) present in file " & strFile
  Wscript.Quit 1
End If
'Wscript.echo "Continue"

Set objShell = CreateObject("wscript.shell")
objShell.run "cmd /c title RUNASINSTALL & runas /user:"&strUsername&" "&chr(34)&strPath&chr(34)
Success=False
For i=0 to 5 or Success
        wscript.sleep 100
        Success=objShell.AppActivate("RUNASINSTALL")
        i=i+1
Next
If Success then
        wscript.sleep 100
        objShell.SendKeys strPassword&"{ENTER}"
End if

Open in new window

0
 
LVL 11

Expert Comment

by:zalazar
ID: 40503787
The code looks ok to me.
I do see some spaces after "SetupAdmin      " but I guess this is intentional.
You might want to increase the number of 5 to e.g. 20 in the for loop "For i=0 to 5"  to make sure it also work correctly on slower systems.
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503832
Glad you responded...

This isn't your part of the code, but it is sending the password, but is not sending ENTER on my Windows 7 machines.
So it hangs for a ENTER.

If I hit Enter, the script continues to run as expected.

Thoughts?
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40503999
Also...


I ran into a problem where this needs to run Elevated and through the UAC prompt.
Can you help here too?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:zalazar
ID: 40504333
I have tested it but the AppActivate method is not working for me unfortunately.
Can you test the following code:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd.exe /c runas.exe /user:" & strUsername & " " & Chr(34) & strPath & Chr(34), 1
Wscript.Sleep 1000
objShell.SendKeys strPassword
Wscript.Sleep 100
objShell.SendKeys "{ENTER}"

Open in new window


About the UAC.
Can you maybe tell what you like to accomplish ?
E.g. do you want the vbs script to elevate so you can press Yes at the UAC prompt ?
Bypassing the UAC completely will be difficult.
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40509162
Script worked, I think. It seemed to entered the password and then closed.
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40509316
What I am trying to do is have the script run as an domain admin and install/ Update malwarebytes as a login script.

Here is what I have put together :

strUsername = "setup.admin@sdomain.com"
strPassword = "Password99"
strPath = "\\domain.comt\netlogon\TC\TC.Upgrade_Mbam.bat"
strFile = "C:\Program Files (x86)\Malwarebytes Anti-Malware\changes.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFile) = False Then
  'Wscript.echo "File " & strFile & " does not exist"
  Wscript.Quit 1
End If

Set fInput = fso.OpenTextFile(strFile , 1)
strReadAll = fInput.ReadAll
fInput.Close
Set fInput = Nothing
vCheck1 = Instr(LCase(strReadAll), "2.0.4.1028")
If vCheck1 > 0 Then
  'Wscript.echo "Version info (2.0.4.1028) present in file " & strFile
  Wscript.Quit 1
End If
 'Wscript.echo "Continue"

Set objShell = CreateObject("wscript.shell")
objShell.run strPath
Success=False
For i=0 to 20 or Success
        wscript.sleep 1000
        Success=objShell.AppActivate("RUNASINSTALL")
        i=i+1
Next
If Success then
        wscript.sleep 1000
        objShell.SendKeys strPassword
	wshShell.SendKeys "{ENTER}"
End if

Open in new window


and

C:
CD\
CD windows\system32
net use  v: \\server\setup$
V:
cd mbam
mbam-setup-2.0.4.1028.exe /SILENT /NORESTART /CLOSEAPPLICATIONS /NOICONS
Net use v: /Delete /Y

Open in new window


Everything seems to work but like I mentioned before, it hangs at the login on any Window7 machine.

Thanks in advance!
0
 
LVL 11

Accepted Solution

by:
zalazar earned 500 total points
ID: 40511185
It's difficult to say why this is happening but as an alternative you can try a different approach via PsExec.
This Microsoft tool has also an elevate option.
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

Please download PsTools.zip from the following location:
http://download.sysinternals.com/files/PSTools.zip
From the zip you only need the file "PsExec.exe".

Place the script and PsExec.exe in the same directory.
And modify the script to:
strUsername = "setup.admin@sdomain.com"
strPassword = "Password99"
strScriptDir = Mid(Wscript.ScriptFullName, 1, InstrRev(Wscript.ScriptFullName, "\") - 1)
strProgram = "cmd.exe"
strArguments = "/c \\domain.comt\netlogon\TC\TC.Upgrade_Mbam.bat"
strCommand = strScriptDir & "\psexec.exe -u " & strUsername & " -p " & strPassword & " -i -h -accepteula " & strProgram & " " & Chr(34) & strArguments & Chr(34)
strFile = "C:\Program Files (x86)\Malwarebytes Anti-Malware\changes.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFile) = False Then
  'Wscript.echo "File " & strFile & " does not exist"
  Wscript.Quit 1
End If

Set fInput = fso.OpenTextFile(strFile , 1)
strReadAll = fInput.ReadAll
fInput.Close
Set fInput = Nothing
vCheck1 = Instr(LCase(strReadAll), "2.0.4.1028")
If vCheck1 > 0 Then
  'Wscript.echo "Version info (2.0.4.1028) present in file " & strFile
  Wscript.Quit 1
End If
'Wscript.echo "Continue"

Set objShell = CreateObject("wscript.shell")
'Wscript.echo "Executing command: " & strCommand
objShell.Run strCommand, 7, True

Open in new window

0
 
LVL 1

Author Comment

by:Glenn M
ID: 40519007
Testing...
0
 
LVL 1

Author Comment

by:Glenn M
ID: 40573528
Sorry... I am back, as I needed to wait for a maint. window... Testing again...
0
 
LVL 11

Expert Comment

by:zalazar
ID: 40576305
thanks for the feedback, good luck with testing.
0
 
LVL 1

Author Closing Comment

by:Glenn M
ID: 40937662
The scripting is solid and works. Thank you!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now