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
48 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
[X]
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
  • 11
  • 5
16 Comments
 
LVL 12

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 2

Author Comment

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

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

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

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 12

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 2

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 2

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
 
LVL 12

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 2

Author Comment

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

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 12

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 2

Author Comment

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

Author Comment

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

Expert Comment

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

Author Closing Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 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