Link to home
Start Free TrialLog in
Avatar of Glenn M
Glenn MFlag for United States of America

asked on

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

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

Avatar of zalazar
zalazar

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

Avatar of Glenn M

ASKER

Checking...
Avatar of Glenn M

ASKER

It appears the script thus far, works.
When executed the script found 2.0.4.1028 in the Changes.txt and presented the following:
User generated image
When I changed the values to 2.0.4.1027 in the Changes.txt and ran the script, it presented the following:
User generated image
Avatar of Glenn M

ASKER

Can you help me by putting our two scripts together, to work as I hope?  :)
Avatar of Glenn M

ASKER

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

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.
Avatar of Glenn M

ASKER

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?
Avatar of Glenn M

ASKER

Also...


I ran into a problem where this needs to run Elevated and through the UAC prompt.
Can you help here too?
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.
Avatar of Glenn M

ASKER

Script worked, I think. It seemed to entered the password and then closed.
Avatar of Glenn M

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of zalazar
zalazar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Glenn M

ASKER

Testing...
Avatar of Glenn M

ASKER

Sorry... I am back, as I needed to wait for a maint. window... Testing again...
thanks for the feedback, good luck with testing.
Avatar of Glenn M

ASKER

The scripting is solid and works. Thank you!