Convert VBScript to Visual Basic .Net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lou Civitella

    #1

    Convert VBScript to Visual Basic .Net

    I have found code that Recycles the Application Pool but it was written in
    VBScript can anyone convert this for me to VB.Net?
    Thanks!

    'Appendix B: Application Pool Recycle script - Save as Recycleap.vbs

    'By Default Recycle All App Pools
    bRecycleAll = 1
    'Delay between each apppool recycle in seconds (if all apppools are being
    recycled)
    iRecycleDelay = 5

    If Wscript.argumen ts.count = 0 Then
    WScript.Echo "Syntax: recycleap server [AppPoolName]"
    WScript.Echo
    WScript.Echo "Example: recycleap servername DefaultAppPool"
    WScript.Quit (0)
    Else
    strServer = WScript.argumen ts(0)
    If WScript.argumen ts.count > 1 Then
    strAppPoolName = WScript.argumen ts(1)
    bRecycleAll = 0
    End If
    End If

    'Connect to the specified server using WMI
    set Locator = CreateObject("W bemScripting.SW bemLocator")
    Locator.Securit y_.Authenticati onLevel = 6
    set Service = locator.connect server(strServe r,"root/MicrosoftIISv2" )

    'Get a collection of WMI apppools
    set APCollection = Service.Instanc esOf("IISApplic ationPool")

    If bRecycleAll = 1 Then
    'Recycle each apppool returned in the collection with a delay of
    iRecycleDelay
    For each APInstance in APCollection

    WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
    APInstance.Recy cle

    WScript.Echo "Waiting " & iRecycleDelay & " seconds..."
    Wscript.Sleep(i RecycleDelay*10 00)
    Next
    Else
    'If we're not recycling all apppools find the one that was specified and
    recycle it
    For each APInstance in APCollection
    If UCase(ApInstanc e.Name) = UCase("W3SVC/AppPools/" &
    strAppPoolName) Then
    WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
    APInstance.Recy cle
    End If
    Next
    End If

    WScript.Echo "Done!"


  • AMDRIT

    #2
    Re: Convert VBScript to Visual Basic .Net

    WScript is the parsing engine
    WScript.echo is the sampe as debug.print

    WbemScripting.S WbemLocator apears to be a WMI wrapper to peer into the
    application pools. Cerainly you could use createobject in .Net as well,
    just that it would be COM Interop.

    Everything else is pure VB.

    Here is my once over pass at it.

    1. Create a new console application in .Net,
    2. Add a reference to COM | WBemScripting (Microsoft WMI Scripting V1.2
    Library)
    Set Copy Local to false
    3. Paste in this code
    4. Test

    Module Module1

    'Appendix B: Application Pool Recycle script - Save as Recycleap.vbs>
    'By Default Recycle All App Pools
    Private bRecycleAll As Boolean = 1
    'Delay between each apppool recycle in seconds (if all apppools are being
    recycled)
    Private iRecycleDelay As Integer = 5

    Private strServer As String = String.Empty
    Private strAppPoolName As String = String.Empty

    'More information can be found at
    http://msdn.microsoft.com/library/de...nectserver.asp

    Sub Main()

    Dim Locator As WbemScripting.S WbemLocator
    Dim Service As WbemScripting.S WbemServices
    Dim APCollection As WbemScripting.S WbemObjectSet

    If Recycleap.My.Ap plication.Comma ndLineArgs.Coun t = 0 Then
    Console.WriteLi ne("Syntax: recycleap server [AppPoolName]")
    Console.WriteLi ne()
    Console.WriteLi ne("Example: recycleap servername DefaultAppPool" )
    End
    Else
    strServer = Recycleap.My.Ap plication.Comma ndLineArgs(0)
    If Recycleap.My.Ap plication.Comma ndLineArgs.Coun t > 1 Then
    strAppPoolName = Recycleap.My.Ap plication.Comma ndLineArgs(1)
    bRecycleAll = 0
    End If
    End If

    'Connect to the specified server using WMI
    Locator = CreateObject("W bemScripting.SW bemLocator")
    Locator.Securit y_.Authenticati onLevel = 6
    Service = Locator.connect server(strServe r, "root/MicrosoftIISv2" )

    'Get a collection of WMI apppools
    APCollection = Service.Instanc esOf("IISApplic ationPool")

    If bRecycleAll = 1 Then
    'Recycle each apppool returned in the collection with a delay of
    iRecycleDelay
    For Each APInstance As Object In APCollection

    Console.WriteLi ne("Recycling " & strServer & "/" & APInstance.Name )
    APInstance.Recy cle()

    Console.WriteLi ne("Waiting " & iRecycleDelay & " seconds...")
    Threading.Threa d.Sleep(iRecycl eDelay * 1000)
    Next
    Else
    'If we're not recycling all apppools find the one that was specified
    and recycle it
    For Each APInstance As WbemScripting.S WbemObject In APCollection
    If UCase(APInstanc e.Name) = UCase("W3SVC/AppPools/" &
    strAppPoolName) Then
    Console.WriteLi ne("Recycling " & strServer & "/" &
    APInstance.Name )
    APInstance.Recy cle()
    End If
    Next
    End If

    Console.WriteLi ne("Done!")
    Console.Beep()
    Console.WriteLi ne("Press Enter to Close.")

    Console.ReadLin e()


    End Sub

    End Module


    "Lou Civitella" <lou@webersyste ms.com> wrote in message
    news:%23hjaxgMg GHA.1208@TK2MSF TNGP02.phx.gbl. ..[color=blue]
    >I have found code that Recycles the Application Pool but it was written in
    >VBScript can anyone convert this for me to VB.Net?
    > Thanks!
    >
    > 'Appendix B: Application Pool Recycle script - Save as Recycleap.vbs
    >
    > 'By Default Recycle All App Pools
    > bRecycleAll = 1
    > 'Delay between each apppool recycle in seconds (if all apppools are being
    > recycled)
    > iRecycleDelay = 5
    >
    > If Wscript.argumen ts.count = 0 Then
    > WScript.Echo "Syntax: recycleap server [AppPoolName]"
    > WScript.Echo
    > WScript.Echo "Example: recycleap servername DefaultAppPool"
    > WScript.Quit (0)
    > Else
    > strServer = WScript.argumen ts(0)
    > If WScript.argumen ts.count > 1 Then
    > strAppPoolName = WScript.argumen ts(1)
    > bRecycleAll = 0
    > End If
    > End If
    >
    > 'Connect to the specified server using WMI
    > set Locator = CreateObject("W bemScripting.SW bemLocator")
    > Locator.Securit y_.Authenticati onLevel = 6
    > set Service = locator.connect server(strServe r,"root/MicrosoftIISv2" )
    >
    > 'Get a collection of WMI apppools
    > set APCollection = Service.Instanc esOf("IISApplic ationPool")
    >
    > If bRecycleAll = 1 Then
    > 'Recycle each apppool returned in the collection with a delay of
    > iRecycleDelay
    > For each APInstance in APCollection
    >
    > WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
    > APInstance.Recy cle
    >
    > WScript.Echo "Waiting " & iRecycleDelay & " seconds..."
    > Wscript.Sleep(i RecycleDelay*10 00)
    > Next
    > Else
    > 'If we're not recycling all apppools find the one that was specified
    > and recycle it
    > For each APInstance in APCollection
    > If UCase(ApInstanc e.Name) = UCase("W3SVC/AppPools/" &
    > strAppPoolName) Then
    > WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
    > APInstance.Recy cle
    > End If
    > Next
    > End If
    >
    > WScript.Echo "Done!"
    >[/color]


    Comment

    Working...