Home All Groups Group Topic Archive Search About

Convert VBScript to Visual Basic .Net

Author
26 May 2006 1:27 PM
Lou Civitella
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.arguments.count = 0 Then
    WScript.Echo "Syntax:  recycleap server [AppPoolName]"
    WScript.Echo
    WScript.Echo "Example: recycleap servername DefaultAppPool"
    WScript.Quit (0)
Else
    strServer = WScript.arguments(0)
    If WScript.arguments.count > 1 Then
        strAppPoolName = WScript.arguments(1)
        bRecycleAll = 0
    End If
End If

'Connect to the specified server using WMI
set Locator = CreateObject("WbemScripting.SWbemLocator")
Locator.Security_.AuthenticationLevel = 6
set Service = locator.connectserver(strServer,"root/MicrosoftIISv2")

'Get a collection of WMI apppools
set APCollection = Service.InstancesOf("IISApplicationPool")

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.Recycle

     WScript.Echo "Waiting " & iRecycleDelay & " seconds..."
        Wscript.Sleep(iRecycleDelay*1000)
    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(ApInstance.Name) = UCase("W3SVC/AppPools/" &
strAppPoolName) Then
         WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
            APInstance.Recycle
        End If
    Next
End If

WScript.Echo "Done!"

Author
26 May 2006 2:19 PM
AMDRIT
WScript is the parsing engine
WScript.echo is the sampe as debug.print

WbemScripting.SWbemLocator 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/default.asp?url=/library/en-us/wmisdk/wmi/swbemlocator_connectserver.asp

  Sub Main()

    Dim Locator As WbemScripting.SWbemLocator
    Dim Service As WbemScripting.SWbemServices
    Dim APCollection As WbemScripting.SWbemObjectSet

    If Recycleap.My.Application.CommandLineArgs.Count = 0 Then
      Console.WriteLine("Syntax:  recycleap server [AppPoolName]")
      Console.WriteLine()
      Console.WriteLine("Example: recycleap servername DefaultAppPool")
      End
    Else
      strServer = Recycleap.My.Application.CommandLineArgs(0)
      If Recycleap.My.Application.CommandLineArgs.Count > 1 Then
        strAppPoolName = Recycleap.My.Application.CommandLineArgs(1)
        bRecycleAll = 0
      End If
    End If

    'Connect to the specified server using WMI
    Locator = CreateObject("WbemScripting.SWbemLocator")
    Locator.Security_.AuthenticationLevel = 6
    Service = Locator.connectserver(strServer, "root/MicrosoftIISv2")

    'Get a collection of WMI apppools
    APCollection = Service.InstancesOf("IISApplicationPool")

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

        Console.WriteLine("Recycling " & strServer & "/" & APInstance.Name)
        APInstance.Recycle()

        Console.WriteLine("Waiting " & iRecycleDelay & " seconds...")
        Threading.Thread.Sleep(iRecycleDelay * 1000)
      Next
    Else
      'If we're not recycling all apppools find the one that was specified
and recycle it
      For Each APInstance As WbemScripting.SWbemObject In APCollection
        If UCase(APInstance.Name) = UCase("W3SVC/AppPools/" &
strAppPoolName) Then
          Console.WriteLine("Recycling " & strServer & "/" &
APInstance.Name)
          APInstance.Recycle()
        End If
      Next
    End If

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

    Console.ReadLine()


  End Sub

End Module


Show quoteHide quote
"Lou Civitella" <l**@webersystems.com> wrote in message
news:%23hjaxgMgGHA.1208@TK2MSFTNGP02.phx.gbl...
>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.arguments.count = 0 Then
>    WScript.Echo "Syntax:  recycleap server [AppPoolName]"
>    WScript.Echo
>    WScript.Echo "Example: recycleap servername DefaultAppPool"
>    WScript.Quit (0)
> Else
>    strServer = WScript.arguments(0)
>    If WScript.arguments.count > 1 Then
>        strAppPoolName = WScript.arguments(1)
>        bRecycleAll = 0
>    End If
> End If
>
> 'Connect to the specified server using WMI
> set Locator = CreateObject("WbemScripting.SWbemLocator")
> Locator.Security_.AuthenticationLevel = 6
> set Service = locator.connectserver(strServer,"root/MicrosoftIISv2")
>
> 'Get a collection of WMI apppools
> set APCollection = Service.InstancesOf("IISApplicationPool")
>
> 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.Recycle
>
>     WScript.Echo "Waiting " & iRecycleDelay & " seconds..."
>        Wscript.Sleep(iRecycleDelay*1000)
>    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(ApInstance.Name) = UCase("W3SVC/AppPools/" &
> strAppPoolName) Then
>         WScript.Echo "Recycling " & strServer & "/" & APInstance.Name
>            APInstance.Recycle
>        End If
>    Next
> End If
>
> WScript.Echo "Done!"
>