Convert VBScript statement to VB.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BHo15
    New Member
    • Feb 2014
    • 143

    Convert VBScript statement to VB.Net

    Hi.

    I'm neither accomplished at vb.net or vbscript, but have been fooling with both of them as of late. I have a vb.net GUI that calls 2 vbscripts, and I would like to cut it down to just one (my skill level is not such that I could even attempt to convert the big one to vb.net).

    The smaller of the 2 vbscripts just calls the larger one by running a command line statement. I wonder if someone can help me get started in getting this to vb.net. Here is the statement...

    Code:
    Set oShell = CreateObject("WScript.Shell")
    
    If Not WScript.FullName = CurrentPath & "cscript.exe" Then
    	oShell.Run "cmd.exe /c" & WScript.Path _
            & "\cscript.exe //NOLOGO " & Chr(34) _
            & "TweakedFetch.vbs" & Chr(34) & " " & Chr(34) _
            & strEmail & Chr(34) & " " & Chr(34) _
            & strNetwork & Chr(34) & " " & Chr(34) _
            & BDate & Chr(34) & " " & Chr(34) _
            & EDate & Chr(34) & " " & Chr(34) _
            & strP & Chr(34) & " >DNLD.csv"
        WScript.Quit 0
    End If
    I have no problem passing the variables to the vbscript. My problems come in dealing with the oshell, WScript, CScript, and the sending results to DNLD.csv.

    Anyone willing to take a stab at it?

    Thanks,
    Brad
  • BHo15
    New Member
    • Feb 2014
    • 143

    #2
    Well... I made some progress.

    I used Dim oShell = CreateObject("W Script.Shell"), and then left off the If statement and the WScript.Quit. The rest of it works, except...

    The & " >DNLD.csv" does not work. It is just printing results to the cmd window.

    So how do I send the results to the DNLD.csv?

    Thanks,
    Brad

    Comment

    • Luk3r
      Contributor
      • Jan 2014
      • 300

      #3
      Looks to me like you should specify a drive and/or directory in front of DNLD.csv (example: C:\TestFolder\D NLD.csv). I also know that you state you aren't very proficient in neither VBS nor VB.NET, but anything that you can do in a VBScript, you can do in VB.NET. So, it's worth your time doing the research and creating only one program in VB.NET to carry out what the VBScripts do, rather than calling scripts from a VB.NET program. Please feel free to post as many questions as you'd like (as new questions, not in this thread) and I'd be more than happy to help.

      Comment

      • BHo15
        New Member
        • Feb 2014
        • 143

        #4
        Thanks for the insight. Here is what I tried...

        Code:
         Dim CurrentPath As String = _
                               Application.StartupPath & "\"
               Dim oShell = CreateObject("WScript.Shell")
        
        oShell.Run "cmd.exe /c" & WScript.Path _
                & "\cscript.exe //NOLOGO " & Chr(34) _
                & "TweakedFetch.vbs" & Chr(34) & " " & Chr(34) _
                & strEmail & Chr(34) & " " & Chr(34) _
                & strNetwork & Chr(34) & " " & Chr(34) _
                & BDate & Chr(34) & " " & Chr(34) _
                & EDate & Chr(34) & " " & Chr(34) _
                & strP & Chr(34) & " >" & CurrentPath &   
                                              "DNLD.csv"
        But... to no avail. Still just prints in CMD window.

        Once we get this done... I'll be visiting more with you about the complete conversion to VB.NET. I really would like to do it that way.

        Comment

        • Luk3r
          Contributor
          • Jan 2014
          • 300

          #5
          Can you post the code for your larger script and possibly your .NET app that is calling both scripts?

          Comment

          • BHo15
            New Member
            • Feb 2014
            • 143

            #6
            Sure. Would you like it in this thread, or a new one?

            Comment

            • Luk3r
              Contributor
              • Jan 2014
              • 300

              #7
              Since we are still dealing with figuring out why it won't save, I think this one is fine for now. But when it comes to constructing a .NET app from the scripts, you will want to open a new one :)

              Comment

              • BHo15
                New Member
                • Feb 2014
                • 143

                #8
                The vb.net code is a bit too much to post all of, but what I posted before is the button click event that should kick things off.

                This is the code from the larger of the two vbscripts. It is a modification of a vbscript built by Brian Hartvigsen and Richard Crowley for OpenDNS stat pulling. Towards the end of the code, I open Excel, and a VBA macro that actually makes the pulled data a little cleaner. I know that VB.NET could also do that without using VBA, but maybe one thing at a time. :)

                Code:
                ' Modified from OpenDNS-Fetch for Windows  Brad Hodge <brad.h.hodge@gmail.com>
                ' 	Based on original fetchstats script from Richard Crowley
                ' 	Brian Hartvigsen <brian.hartvigsen@opendns.com>
                
                
                Dim CurPath
                	CurPath = Replace(WScriptFullName, WScript.ScriptName, "")
                Dim strK
                	strK="#$UnEqu1v0cal!?"
                Dim strNetwork
                Dim UserName
                Dim Password,strP
                Dim BDate,EDate, DateRange
                Dim objHTTP
                	Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
                Dim URL
                	URL = "https://dashboard.opendns.com"
                Dim strEmail
                Dim objPassword
                Dim regEx
                Dim data
                
                Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
                
                Function GetUrlData(strUrl, strMethod, strData)
                        objHTTP.Open strMethod, strUrl
                        If strMethod = "POST" Then
                                objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
                        End If
                        objHTTP.Option(6) = False
                        objHTTP.Send(strData)
                
                        If Err.Number <> 0 Then
                                GetUrlData = "ERROR: " & Err.Description & vbCrLf & Err.Source & " (" & Err.Nmber & ")"
                        Else
                                GetUrlData = objHTTP.ResponseText
                        End If
                End Function
                
                URL="https://dashboard.opendns.com"
                
                
                Username = Wscript.Arguments.Item(0)
                If Len(Username) = 0 Then: Usage
                Network = Wscript.Arguments.Item(1)
                If Len(Network) = 0 Then: Usage
                BDate = Wscript.Arguments.Item(2)
                If Len(BDate) = 0 Then: Usage
                EDate = Wscript.Arguments.Item(3)
                If Len(EDate) = 0 Then: Usage
                strP = Wscript.Arguments.Item(4)
                
                Set regDate = new RegExp
                regDate.Pattern = "^\d{4}-\d{2}-\d{2}$"
                
                DateRange = BDate & "to" & EDate
                
                ' Are they running Vista or 7?
                On Error Resume Next
                Set objPassword = CreateObject("ScriptPW.Password")
                	If strP="" Then strP= Inputbox("Please enter your password.")
                    Password = strP
                
                Wscript.StdErr.Write vbCrLf
                On Error GoTo 0
                
                Set regEx = New RegExp
                regEx.IgnoreCase = true
                regEx.Pattern = ".*name=""formtoken"" value=""([0-9a-f]*)"".*"
                
                data = GetUrlData(URL & "/signin", "GET", "")
                
                Set Matches = regEx.Execute(data)
                token = Matches(0).SubMatches(0)
                
                data = GetUrlData(URL & "/signin", "POST", "formtoken=" & token & "&username=" & Escape(Username) & "&password=" & Escape(Password) & "&sign_in_submit=foo")
                If Len(data) <> 0 Then
                        Wscript.StdErr.Write "Login Failed. Check username and password" & vbCrLf
                        WScript.Quit 1
                End If
                
                page=1
                Do While True
                        data = GetUrlData(URL & "/stats/" & Network & "/topdomains/" & DateRange & "/page" & page & ".csv", "GET", "")
                        If page = 1 Then
                                If LenB(data) = 0 Then
                                        WScript.StdErr.Write "You can not access " & Network & vbCrLf
                                        WScript.Quit 2
                                ElseIf InStr(data, "<!DOCTYPE") Then
                                 Wscript.StdErr.Write "Error retrieving data. Date range may be outside of available data."
                                 Wscript.Quit 2
                                End If
                        Else
                                ' First line is always header
                                data=Mid(data,InStr(data,vbLf)+1)
                        End If
                        If LenB(Trim(data)) = 0 Then
                                Exit Do
                        End If
                        Wscript.StdOut.Write data
                        page = page + 1
                Loop
                '--------------------------------------------------------
                'DISPLAY AND FORMAT THE DATA IN EXCEL
                Dim xlApp, xlBook
                Dim CurrentPath
                	CurrentPath=Replace(WScript.ScriptFullName, WScript.ScriptName, "")
                Set xlApp = CreateObject("Excel.Application")
                Set xlBook = xlApp.Workbooks.Open(CurrentPath & "\StatTemplate.xltm")
                xlApp.Run "Process",(CurrentPath)
                '--------------------------------------------------------
                
                Function Usage()
                	MsgBox("Username, OpenDNS Network, Begin Date, and Ending Date must be passed from 001-CallFetch.vbs")
                End Function

                Comment

                • BHo15
                  New Member
                  • Feb 2014
                  • 143

                  #9
                  One more bit of info... When I was using nothing but vbscript, I was using an INI file to store the user account info and preferences. When I built the GUI in vb.net, I started using My.Settings to store all of that information. Hence, when vb.net calls the vbscript, it is sending the account information from the vb.net settings. And when the VBA kicks off in Excel, it would eventually use the same to decide which categories to display.

                  Comment

                  • Luk3r
                    Contributor
                    • Jan 2014
                    • 300

                    #10
                    I do have another question for you. Where are you specifying the values for the variables in your small script?

                    Comment

                    • BHo15
                      New Member
                      • Feb 2014
                      • 143

                      #11
                      My apologies. Here is the part I left off of the post regarding the vb app button click.

                      Code:
                        Dim strUser As String = Me.txtUser.Text
                              Dim strNet As String = Me.txtNetwork.Text
                              Dim strBDate As String = Me.txtBDate.Text
                              Dim strEDate As String = Me.txtEDate.Text
                              Dim strPW As String = rc4(My.Settings.appPW, K)
                      The user obviously adds the info to the form, and then that info is stored into My.Settings.

                      Comment

                      • Luk3r
                        Contributor
                        • Jan 2014
                        • 300

                        #12
                        The variables in your VB.NET application don't match those that you're trying to pass via cscript. If I was you, I'd do away with the small script all together and use something like this. You have to understand, this is merely an example but it worked fine for me. (Note: I used this code under a Button Click event within a VB.NET app)

                        Code:
                                Dim strUser As String = Me.txtUser.Text
                                Dim strNet As String = Me.txtNetwork.Text
                                Dim strBDate As String = Me.txtBDate.Text
                                Dim strEDate As String = Me.txtEDate.Text
                                Dim strPW As String = rc4(My.Settings.appPW, K)
                        
                                'replace both instances of c:\directory with your own
                                Shell("cmd /c cscript //NoLogo c:\directory\TweakedFetch.vbs " & strUser & " " & strNet & " " & strBDate & " " & strEDate & " " & strPW & " > c:\directory\DNLD.csv")

                        Comment

                        • BHo15
                          New Member
                          • Feb 2014
                          • 143

                          #13
                          Thanks for the help. That worked fine (and was much more concise than mine :)), but it is still printing to cmd window.

                          Should I instead look to load the contents into a string, and then put that string into the DNLD.csv?

                          Comment

                          • Luk3r
                            Contributor
                            • Jan 2014
                            • 300

                            #14
                            I'm still not able to replicate the code printing to the cmd window. Can you now include what you have setup under your Button Click event?

                            Comment

                            • BHo15
                              New Member
                              • Feb 2014
                              • 143

                              #15
                              Sorry this is being so problematic.

                              Here is the .net code under the button click.

                              Code:
                                  Private Sub btnFetchClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFetch.Click
                                      Dim strUser As String = Me.txtUser.Text
                                      Dim strNet As String = Me.txtNetwork.Text
                                      Dim strBDate As String = Me.txtBDate.Text
                                      Dim strEDate As String = Me.txtEDate.Text
                                      Dim strPW As String = rc4(My.Settings.appPW, K)
                                      Dim CurrentPath As String = Application.StartupPath & "\"
                                      Dim strFetch As String = ""
                              
                                      Shell("cmd /c cscript //NoLogo " & CurrentPath & "TweakedFetch.vbs " & strUser & " " & strNet & " " & strBDate & " " & strEDate & " " & strPW & " >" & CurrentPath & "DNLD.csv")
                              
                                  End Sub

                              And it is calling the vbscript that I sent on the last post. I am currently bypassing the shorter vbscript that I was using before, but just in case you are curious, this was it...

                              Code:
                              strEmail=wscript.arguments(0)
                              strNetwork=wscript.arguments(1)
                              BDate=wscript.arguments(2)
                              EDate=wscript.arguments(3)
                              strP=wscript.arguments(4)
                              
                              Dim oShell
                              
                              Set oShell = CreateObject("WScript.Shell")
                              
                              If Not WScript.FullName = CurrentPath & "cscript.exe" Then
                              	oShell.Run "cmd.exe /k" & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & "C:\Users\bhodge\Dropbox\OpenDNS\Stats\TweakedFetch.vbs" & Chr(34) & " " & Chr(34) & strEmail _
                              	& Chr(34) & " " & Chr(34) & strNetwork & Chr(34) & " " & Chr(34) & BDate & Chr(34) & " " & Chr(34) & EDate & Chr(34) & " " _
                              	& Chr(34) & strP & Chr(34) & " >DNLD.csv"
                                 WScript.Quit 0
                              End If

                              Comment

                              Working...