Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

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

0
Glenn M
Asked:
Glenn M
  • 11
  • 5
1 Solution
 
zalazarCommented:
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
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Checking...
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Glenn MSystems Engineer / ManagerAuthor Commented:
Can you help me by putting our two scripts together, to work as I hope?  :)
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
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
 
zalazarCommented:
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
 
Glenn MSystems Engineer / ManagerAuthor Commented:
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
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Also...


I ran into a problem where this needs to run Elevated and through the UAC prompt.
Can you help here too?
0
 
zalazarCommented:
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
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Script worked, I think. It seemed to entered the password and then closed.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
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
 
zalazarCommented:
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
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Testing...
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
Sorry... I am back, as I needed to wait for a maint. window... Testing again...
0
 
zalazarCommented:
thanks for the feedback, good luck with testing.
0
 
Glenn MSystems Engineer / ManagerAuthor Commented:
The scripting is solid and works. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now