Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

Monday, June 23, 2014

runas.vbs

Very simple script to run a command line application with elevated privileges: 

if WScript.Arguments.Length > 0 Then
    Set objShell = CreateObject("Shell.Application")
    dim a : a = ""
    for i=1 to WScript.Arguments.Length-1
        a = a & WScript.Arguments(i) & " "
    next
    objShell.ShellExecute WScript.Arguments(0), a, "", "runas"
end if

Wednesday, February 22, 2012

passing an array from vbscript to .net

.net < 4.0 does not support passing an array to its COM visible method by reference.

So, if there is a .net method in an object:
void f( object ary );

from VBscript it has to be called this way:

dim aa(1) : a(0)=1 : a(1)=2
obj.f( (aa) )

Note the extra parentheses around the array variable!

Tuesday, January 18, 2011

If you need to send a BLOB to a HTTP server using vbscript, then you need
pass to the send() method the stream. But if it's multipart/form-data format, aside of the binary data the POST body should contain also boundaries and other metadata.
So, we have to store in a stream text data then binary then again some text.
It can be done by using the ADODB.Stream object.
It`s type (binary or text) can be converted dynamically, during the writing.
But! Before the setting the new type, the position has to be set to 0.
It can be then restored, after the type conversion to value taken from the stream.Size property.


option explicit

dim stream, file_loader, file_name, bnd
set stream = CreateObject("ADODB.Stream")
set file_loader = CreateObject("ADODB.Stream")
const adTypeBinary = 1
const adTypeText = 2
stream.Open
stream.Type = adTypeText
stream.CharSet = "iso-8859-1"

bnd
= "boundary"
file_name = "test.png"
stream.WriteText( "--" & bnd & vbCrLf & _
"Content-Disposition: form-data; name=""the_file""; filename=""" & file_name & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf )

stream.Position = 0
stream.Type = adTypeBinary
stream.Position = stream.Size

WScript.Echo "Position before file loading: " & stream.Position

file_loader.Open
file_loader.Type = adTypeBinary
file_loader.LoadFromFile(
file_name )

file_loader.CopyTo stream

stream.Position = 0
stream.Type = adTypeText
stream.CharSet = "iso-8859-1"
stream.Position = stream.Size
WScript.Echo "Position after file loading: " & stream.Position
stream.WriteText( vbCrLf & "--" & bnd & "--" & vbCrLf )
stream.Position = 0
stream.Type = adTypeBinary

dim http_req
set http_req = CreateObject("WinHTTP.WinHTTPRequest.5.1")
'WinHTTP.WinHTTPRequest.5.1 better than Msxml2.XMLHTTP because it lets you override the http headers
call http_req.SetProxy( 2, "127.0.0.1:8888" ) ' for Fiddler proxy
call http_req.open( "POST", "http://127.0.0.1/", false )
call http_req.send( stream )