Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pass variable from Visual Basic .NET 2008 to VBScript

Posted on 2011-02-20
4
Medium Priority
?
726 Views
Last Modified: 2012-06-27
I am writing a Visual Basic 2008 .NET program with a Textbox that will contain a user entered IP Address or Hostname. I'd like to pass this value to a VBScript.

I've done this successfully with some other things in the past, however this particular VBScript has an "InputBox" function that I'd like to replace with something like C:\RestartService.vbs <Hostname>

Even after doing some extensive searching, I'm just not getting it on how to accomplish this. I'm completely stuck on how to get VBScript to accept a single command line variable and I will have issues with my Visual Basic .NET example posted below once the VBS items are working.

Thanks for any help you can provide

I realize the functionality of this VBScript is probably something I could do in Visual Basic, but I really like how it functions and don't know how to convert the code.
'Restart Service VBScript
strComputer = InputBox ("Enter IP Address or Computer Name" , "Restart the Automatic Updates Service")
RestartServices ("strComputer"), "wuauserv"
'RestartServices ".", "SQLSERVERAGENT,MSSQLSERVER"


Sub RestartServices(Computer, ServiceNames)
  Dim ServiceName, Counter, aServiceNames

  'Get the array of service names  
  aServiceNames = split(ServiceNames,",")
  
  'loop services from beginning, stop them 
  For Each ServiceName In aServiceNames 
    StopService Computer, ServiceName, True
  Next 

  'loop services from end, start them 
  For Counter = ubound(aServiceNames) To 0 Step -1 
    StartService Computer, aServiceNames(Counter), True 
  Next 
End Sub

Sub StopService(Computer, ServiceName, Wait)
  Dim cimv2, oService, Result

  'Get the WMI administration object    
  Set cimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
	

  'Get the service object
  Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
  
  
  'Check base properties
  If Not oService.Started Then
    ' the service is Not started
    wscript.echo "The service " & ServiceName & " is Not started"
    exit Sub
  End If

  If Not oService.AcceptStop Then
    ' the service does Not accept stop command
    wscript.echo "The service " & ServiceName & " does Not accept stop command"
    exit Sub
  End If
  
  'wscript.echo oService.getobjecttext_

  'Stop the service
  Result  = oService.StopService
  If 0 <> Result Then
    wscript.echo "Stop " & ServiceName & " error: " & Result
    exit Sub 
  End If 
  
  Do While oService.Started And Wait
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")

    wscript.echo now, "StopService", ServiceName, oService.Started, _
      oService.State, oService.Status
    Wscript.Sleep 200
  Loop   
End Sub


Sub StartService(Computer, ServiceName, Wait)
  Dim cimv2, oService, Result

  'Get the WMI administration object    
  Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")

  'Get the service object
  Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
  
  
  'Path = "winmgmts:{impersonationLevel=impersonate}!\\" & Computer & _
  '  "\root\cimv2:Win32_Service.Name='" & ServiceName & "'" 

  'Get the WMI administration object of the service    
  'Set oService = GetObject(Path)

  'Check base properties
  If oService.Started Then
    ' the service is Not started
    wscript.echo "The service " & ServiceName & " is started."
    exit Sub
  End If
  
  'Start the service
  Result = oService.StartService
  If 0 <> Result Then
    wscript.echo "Start " & ServiceName & " error:" & Result
    exit Sub 
  End If 
  
  Do While InStr(1,oService.State,"running",1) = 0 And Wait 
    'get the current service state
    Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
    
    wscript.echo now, "StartService", ServiceName, oService.Started, _
      oService.State, oService.Status
    Wscript.Sleep 200
  Loop   
End Sub

Open in new window

Visual Basic example code to pass variable

Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim arguments As String

        arguments = "RestartService.vbs " & TextBox1.Text

        Process.Start("wscript.exe", arguments)

    End Sub

Open in new window

0
Comment
Question by:hpops
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:holthd
ID: 34938611
Setup your VBScript to handle arguments.
More information: http://technet.microsoft.com/en-us/library/ee156618.aspx
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 34939119
In the script, change these lines:
strComputer = InputBox ("Enter IP Address or Computer Name" , "Restart the Automatic Updates Service")
RestartServices ("strComputer"), "wuauserv"

to this:
strComputer = WScript.Arguments.Item(0)
RestartServices strComputer, "wuauserv"

Then your VB.NET call should work.

Having said that, this is the VB.NET equivalent that should work.

Dim controller As New ServiceController("wuauserv")
controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.Stopped)
controller.Start()


Regards,

Rob.
0
 

Author Closing Comment

by:hpops
ID: 34939239
Thank you Rob. This is exactly what I needed.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34939278
No problem. I forgot to mention, if you want the VB.NET code to work, use this declaration:
Imports System.ServiceProcess

Regards,

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question