none
convert vbscript to vb.net

    Question

  • does anyone know if it is possible to convert the following code to vb.net?

    Const ForWriting = 2
    Const ForAppending = 8
    
    Set objNetwork1 = createobject("Wscript.Network")
    login_name = objNetwork1.UserName
    
    Set fileSys = CreateObject("Scripting.FileSystemObject")
    If Not filesys.FolderExists("c:\users\" & login_name & "\desktop") Then
    strFileName = "c:\documents and settings\" & login_name & "Desktop\connected-psts.log" 
    Else
    strFileName = "c:\users\" & login_name & "\Desktop\PST-Files-Connected.log"
    End If
    
    'WScript.Echo strFileName
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strFileName) Then
    objFSO.DeleteFile(strFileName)
    Else
    End If
    
    If objFSO.FileExists(strFileName) Then
        Set objFile = objFSO.OpenTextFile(strFileName, ForAppending)
        Else
        Set objFile = objFSO.CreateTextFile(strFileName, ForWriting)
    End If
    
    objFile.WriteLine "Current Time: " & Time 
    objFile.WriteLine "Current Date: " & Date 
    objFile.WriteLine "Current User: " & login_name
    objFile.WriteLine ""
    objFile.WriteLine "Currently Connected PST Files:"
    objFile.WriteLine ""
    
    objFile.Close
    
    Set oshell = createobject("wscript.shell")
    mailboxName = oShell.RegRead ("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles\DefaultProfile")
    Set oOutLapp = WScript.CreateObject("Outlook.Application")
    Set oNameSpace = oOutLapp.GetNamespace("MAPI")
    
    oNameSpace.Logon mailboxName,,True,True
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(strFileName, ForAppending)
    	
    For Each vStore In oNameSpace.Folders
    	vMypos = InStr(1,HextoAsc(vStore.storeid),"\")
    	If vMypos <> "0" Then    
        vMystring = Mid (HexToAsc(vStore.storeid),vMyPos -2)
        objFile.writeline vStore.Name & ", " & vMystring  
        End If
        
    Next
    
    WScript.Sleep 500
    
    objFile.WriteLine ""
    objFile.WriteLine "End Time: " & Time
    
    Function HexToAsc(vhstr)
        Dim vX
        Dim vNstr
        Dim vHexstr
       
        For vX = 1 To Len(vhstr) Step 2
        If Mid(vhstr, vX, 2) = "00" then
        Else
        vNstr = vNstr & Chr("&H" & Mid(vhstr, vx, 2))
        End If
        Next
       
        HexToAsc = vNstr
    End Function 
    
    WScript.Sleep 200
    
    strComputer = "."
    strProcessKill = "'outlook.exe'" 
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = " & strProcessKill )
    For Each objProcess in colProcess
    objProcess.Terminate()
    Next 

    Sunday, April 29, 2012 10:45 PM

Answers

  • Since this question and ones like it are appearing more an moreopften I though I would take it as an opportunity to post a script to the repository that would get the Outlook store information.

    Here is part of it.

    Set shell = createobject("wscript.shell")
    deskTopPath = shell.SpecialFolders("Desktop")
    strlogName = deskTopPath & "\connected-psts.log"
    '
    WriteLog "Begin processing"
    '
    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.GetNamespace("MAPI")
    WriteLog vbTab & "CurrentUser:" & ns.Session.CurrentUser.Name
    WriteLog vbTab & "ExchangeConnectionMode:" & ns.Session.ExchangeConnectionMode
    For Each store In ns.Stores
        Set st=ns.GetStoreFromID(store.storeID)
        With st
            WriteLog vbTab & "DisplayName:" & .displayname
            WriteLog vbTab & vbTab & "FilePath:" & .filepath
            WriteLog vbTab & vbTab & "ExchangeStoreType:" & .ExchangeStoreType
        End With
    Next
    '
    ol.Quit
    Set ol = Nothing
    '
    '
    Function WriteLog(msg)
        msg = "[" & Now & "]" & msg
        WScript.Echo msg
        Set fso = CreateObject("Scripting.FileSystemObject")
        'open or create a file for appending
        Set logFile = fso.OpenTextFile(strlogName, 8, True)
        logFile.WriteLine msg
        logFile.Close
    End Function

    I know that the reasons for why I have done it this way may be confusing for a non-programmer but there are good reasons to structure this this way.  For one thing we can change the logging format very easily without having to pay attention to the rest of the code.

    Many with experience scripting would do somthing like this automatically because they also know that it is aa much easier way of laying out a script for debugging.

    Note also that we can get the users path very easily and event get the path to all of the users main folders by using the shell.


    ¯\_(ツ)_/¯



    Monday, April 30, 2012 10:11 AM

All replies

  • Yes - it is possible but only if you are a VB.NET programmer.

    This is a scripting forum.  For VB.NET you need the developers forums.

    By the way. You do not need to convert teh hex storeisd to ascii to get its name.  Just get the store and it has a name and the full path to the file.

    In fact nearly all of the lines of code in that script are unneeded.  Everything can be done in just a few lines.


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, April 30, 2012 9:29 AM
    Monday, April 30, 2012 9:05 AM
  • Here is a sample of how to do it in POowerShell.  Try it. Play with teh object and learn to inspect all of its properties.

    The way it is done in PowerSHell is similar to the way we would do it in VB.NET.  PowerShell is dotNet. It is a console into the dotNet classes as well as many other thigs.

    $ol=new-object -com Outlook.Application
    $ns=$ol.GetNamespace('MAPI')
    $ns.stores |
        ForEach-Object{$ns.GetStoreFromId($_.StoreID)} |
        Select-Object displayname,filepath,ExchangeStoreType
    $ns.Session
    $ns.Session.CurrentUser


    ¯\_(ツ)_/¯

    Monday, April 30, 2012 9:32 AM
  • Here is pretty much the same thing in VBScript.

    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.GetNamespace("MAPI")
    WScript.Echo "CurrentUser:" & ns.Session.CurrentUser.Name
    WScript.Echo "ExchangeConnectionMode:" & ns.Session.ExchangeConnectionMode
    For Each store In ns.Stores
        Set st=ns.GetStoreFromID(store.storeID)
        With st
            WScript.Echo "DisplayName:" & .displayname
            WScript.Echo vbTab & "FilePath:" & .filepath
            WScript.Echo vbTab & "ExchangeStoreType:" & .ExchangeStoreType
        End With
    Next
    ol.Quit
    Set ol = Nothing

    You can log this by using the redirector.

    @echo %username%  >> mylog.log
    time/t >> mylog.log
    date/t >> mylog.log
    myscript.vbs >> mylog.log


    ¯\_(ツ)_/¯

    Monday, April 30, 2012 9:55 AM
  • Since this question and ones like it are appearing more an moreopften I though I would take it as an opportunity to post a script to the repository that would get the Outlook store information.

    Here is part of it.

    Set shell = createobject("wscript.shell")
    deskTopPath = shell.SpecialFolders("Desktop")
    strlogName = deskTopPath & "\connected-psts.log"
    '
    WriteLog "Begin processing"
    '
    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.GetNamespace("MAPI")
    WriteLog vbTab & "CurrentUser:" & ns.Session.CurrentUser.Name
    WriteLog vbTab & "ExchangeConnectionMode:" & ns.Session.ExchangeConnectionMode
    For Each store In ns.Stores
        Set st=ns.GetStoreFromID(store.storeID)
        With st
            WriteLog vbTab & "DisplayName:" & .displayname
            WriteLog vbTab & vbTab & "FilePath:" & .filepath
            WriteLog vbTab & vbTab & "ExchangeStoreType:" & .ExchangeStoreType
        End With
    Next
    '
    ol.Quit
    Set ol = Nothing
    '
    '
    Function WriteLog(msg)
        msg = "[" & Now & "]" & msg
        WScript.Echo msg
        Set fso = CreateObject("Scripting.FileSystemObject")
        'open or create a file for appending
        Set logFile = fso.OpenTextFile(strlogName, 8, True)
        logFile.WriteLine msg
        logFile.Close
    End Function

    I know that the reasons for why I have done it this way may be confusing for a non-programmer but there are good reasons to structure this this way.  For one thing we can change the logging format very easily without having to pay attention to the rest of the code.

    Many with experience scripting would do somthing like this automatically because they also know that it is aa much easier way of laying out a script for debugging.

    Note also that we can get the users path very easily and event get the path to all of the users main folders by using the shell.


    ¯\_(ツ)_/¯



    Monday, April 30, 2012 10:11 AM