JadeKnight
IS-IT--Management
We're writing a script wich is going to do the following :
A) Run on startup, resumbit itself
B) When resubmit is running, connect to a url and get the content.
C) Write the content from the url to a vbs file
D) Launch the vbs from C)
E) Put result back to web
I have some format problems with the text from winhttp. If I echo it out to screen, it seems fine. However, if I open the text in ex : notepad, the file contains a lot of odd characters... and the content/format is pretty far from the original vbs, or the echo result.
I've tried a couple of things with adodb.stream, but with no good result. Anyone know about a smart way to get text in proper format?
A) Run on startup, resumbit itself
B) When resubmit is running, connect to a url and get the content.
C) Write the content from the url to a vbs file
D) Launch the vbs from C)
E) Put result back to web
I have some format problems with the text from winhttp. If I echo it out to screen, it seems fine. However, if I open the text in ex : notepad, the file contains a lot of odd characters... and the content/format is pretty far from the original vbs, or the echo result.
I've tried a couple of things with adodb.stream, but with no good result. Anyone know about a smart way to get text in proper format?
Code:
'Name : Start.vbs
'Ver. : None yet
'Changelog =
'Syntax : Start.vbs [/t:(n)minutes] or [/Now]
'/T = Resubmit the script to run in n minutes
'/Now = Script will run imediatly
'If script launched without options, script will resumbit itself within Const DEFAULTMIN
'Script will silently terminate if wrong argument provided, or illegal combination
'Supported OS (minimum req) :
'----------------------------
'Windows Server 2003 family
'Windows XP SP1
'Windows 2000 SP3, (except Datacenter Server)
'-----------
'Prefix used
'-----------
'a = Array
'b = Boolean
'c = Collection
'i = Integer
'o = Object
's = String
't = Time
's_(Subname) = Sub
'f_(FunctionName) = Function
'CAPITAL LETTER = Constant
'-----
'Rules
'-----
'Code lines split before column 125
Option Explicit
Dim oShell,oFso,oWinHttpReq
Dim sArg,sInventory,sRunPath
Dim tStart
Dim iTime
Dim bNow
Dim aNamedArg
Const DEFAULTMIN = 15
Const FORREADING = 1
Const FORWRITING = 2
Const URLGET = "[URL unfurl="true"]http://someurl/script.vbs"[/URL]
Const URLPOST = "[URL unfurl="true"]http://someurl/"[/URL]
'Start now flag
bNow = False
'Check/Parse arguments
If Wscript.Arguments.Named.Count = 0 Then
iTime = DEFAULTMIN
ElseIf Wscript.Arguments.Named.Count = 1 Then
'Bind to named arguments
Set aNamedArg = Wscript.Arguments.Named
'Parse arguments
For Each sArg in aNamedArg
Call s_ParseArg(sArg,aNamedArg(sArg))
Next
ElseIf Wscript.Arguments.Named.Count >= 2 Then
wscript.quit
End If
'Create Shell object
Set oShell = Wscript.CreateObject("Wscript.Shell")
'Resubmit
If bNow = False Then
'Assign current time + iTime min to tStart
tStart = DateAdd("n", iTime, FormatDateTime(Now, vbShortTime))
oShell.Run "AT \\Localhost " & tStart & " " & "Cscript.exe" & " " & Wscript.ScriptFullName & " //B //NoLogo /Now" & Chr(34), ,1
'Disassosiate oShell from Object
Set oShell = Nothing
wscript.quit
End If
'Path to dir where script is executed
sRunPath = Left(Wscript.ScriptFullName, InStrRev(Wscript.ScriptFullName, "\") - 1)
Set oFso = CreateObject("Scripting.FilesystemObject")
Dim sDownload
sDownload = f_GetFromUrl()
Call s_CreateScript(sRunPath & "\Test.vbs")
'oShell.Popup sDownload
Set oFso = Nothing
Set oShell = Nothing
'---------------------------------------------------------------------------------------------------------------------------
' Function Procedures
'---------------------------------------------------------------------------------------------------------------------------
Function f_GetFromUrl()
'Instantiate a WinHttp object
Set oWinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'Initialize HTTP request
'False = Opens the HTTP connection in synchronous mode,
'so a call to Send does not return until WinHTTP has completely received the response.
oWinHttpReq.Open "GET", URLGET, false
'Send the HTTP request
oWinHttpReq.Send()
'Put result/response into function
f_GetFromUrl = oWinHttpReq.ResponseText
'Disassosiates object
Set oWinHttpReq = Nothing
End Function
'---------------------------------------------------------------------------------------------------------------------------
' Sub Procedures
'---------------------------------------------------------------------------------------------------------------------------
Sub s_CreateScript(sFile)
Dim oFile
Set oFile = oFso.OpenTextFile(sFile, FORWRITING, True)
oFile.Write sDownload
oFile.Close
Set oFile = Nothing
End Sub
Sub s_ParseArg(sArg,aNamedArg)
On Error Resume Next
'Convert time argument from string to integer
If UCase(sArg) = "T" Then
iTime = CInt(aNamedArg)
'If conversion fail, then it's a illegal value
If err.number <> 0 Then
wscript.quit
End If
ElseIf UCase(sArg) = "NOW" Then
bNow = True
Else
wscript.quit
End If
End Sub