+ Post New Thread
Results 1 to 2 of 2
Scripts Thread, Convert VBS to PowerShell in Coding and Web Development; @ Arthur / @ Steve21 or anyone else Using version 2 of powershell at work but am going to attempt ...
  1. #1

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,051
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377

    Convert VBS to PowerShell

    @Arthur / @Steve21 or anyone else

    Using version 2 of powershell at work but am going to attempt to install version 3 on my laptop ( not sure if it will work or not due to restrictions possibly )

    I have the below script but want it converting to powershell

    Code:
    const HKEY_CURRENT_USER = &H80000001
    const HKEY_LOCAL_MACHINE = &H80000002
    Const ForReading = 1, ForWriting = 2, ForAppending = 8 
    strComputer = "."
    Set StdOut = WScript.StdOut
    Dim username, asset, hdd, manuhdd
    Dim colIPResults, objFile, objFSO, objNIC, objWMI, objWSHNetwork, strAddresses, strIPAddress, strWQL, strComp, osVer
    Dim strRAM
    dim filesys, filetxt
    dim newfolder
    Dim usbPath
    Dim spVer
    Dim strSerial, strModel, strVendor, strNumModel
    Set objNetwork = CreateObject("Wscript.Network")
    Set filesys = CreateObject("Scripting.FileSystemObject")
    username = objNetwork.username
    strComputer = objNetwork.ComputerName
    usbPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
    
    Sub DestroyObjects()
    If IsObject(objWMI) Then Set objWMI = Nothing
    If IsObject(objWSHNetwork) Then Set objWSHNetwork = Nothing
    End Sub
    
    Dim strMAC
    Set objWSHNetwork = CreateObject("WScript.Network")
    Set objWMI = GetObject("WinMGMTS:root\cimv2")
    strWQL = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'True'"
    Set colIPResults = objWMI.ExecQuery(strWQL)
    For Each objNIC In colIPResults
    For Each strIPAddress in objNIC.IPAddress
    If strAddresses = "" Then
    strAddresses = strIPAddress
    Else
    strAddresses = strAddresses
    End If
    Next
    strMAC = objNIC.MACAddress
    Next
    
    'IPAddress
    
    
    'Set objWMIService = GetObject("WinMGMTS:root\cimv2")
    'Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True",,48)
    'For Each objItem in colItems
     '  strMAC = objItem.MACAddress
    'Next
    
    If strAddresses ="0.0.0.0" Or strAddresses ="" or strAddresses = "undefined" Then
        strAddresses = "No Connection Detected or invalid ip address"
    Else
        strAddresses = strAddresses
    End If
    
    DestroyObjects()
    
    'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    'Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
    'For Each Item In colItems
    'manu = Item.Manufacturer
    '    model = Item.Model    
    'Next
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystemProduct",,48)
    For Each objItem in colItems
    Select Case objItem.Vendor
    Case "Dell Inc.":
    
    strSerial = objItem.IdentifyingNumber
    strModel = objItem.Name
    strVendor = objItem.Vendor 
    strNumModel = objItem.Version
    
    If strNumModel = "" Or IsNull(strNumModel)=true Then
    strNumModel = "N/A"
    End If
    
    
    Case "LENOVO":
    
    strSerial = objItem.IdentifyingNumber
    strNumModel =  objItem.Name
    strVendor =  objItem.Vendor 
    strModel = objItem.Version
    
    
    End Select
    
    Next
    
    
    
    
    
    
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PhysicalMemory", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
    totalMemory = 0
    For Each item In colItems
        totalMemory = totalMemory + item.Capacity / (1024^2)
    Next
    strRam = FormatNumber((totalMemory / 1024), 2) & " Gb"
    
    'Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
    'For Each objItem in colItems
    '    serial = objItem.SerialNumber
    'Next
    
    Set colItems = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
    For Each objItem in colItems
    If Len(objItem.SMBIOSAssetTag) <= 0 Then
        asset = "N/A"
    Else
        asset =  objItem.SMBIOSAssetTag
        
    End If
    Next
    
    Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
    
    strKeyPath = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
    strValue = "ProcessorNameString"
     
    objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValue,strScriptStatus
    
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colDisks = objWMIService.ExecQuery("select * from Win32_DiskDrive where Name like '%PHYSICALDRIVE0%'")
    For Each objDisk in colDisks
         
                hdd = ConvertSize(objDisk.Size)
                manuhdd = Int(objDisk.Size /(1000000000)) & " GB" 
    
    Next
    
    Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set QueryWMI = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each oItem In QueryWMI
    spVer = oItem.Caption & " with Service Pack " & oItem.ServicePackMajorVersion
    osVer = oItem.Version
    Next
    
    Set filetxt = filesys.OpenTextFile(usbPath & "\" & ucase(username) & " - " & Trim(strComputer) & ".htm" , 2, True) 
    'filetxt.WriteLine "Username,Manufacturer,Serial Number,Model,Computer Name,RAM,HDD,Asset Tag,Processor"
    'filetxt.WriteLine ucase(username) & "," & manu & "," & serial & "," & model & "," & strComputer & "," & strRAM & "," & hdd & "( " & manuhdd & " )" &  "," & asset & "," & strScriptStatus
    
    filetxt.WriteLine "<H1>Computer Spec for : " & strComputer & "</H1>"
    filetxt.WriteLine "<Center>"
    filetxt.WriteLine "<Table Border=1>"
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Username :</b></td>"
    filetxt.WriteLine "<td>" & ucase(username) & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Operating System & Service Pack Level :</b></td>"
    filetxt.WriteLine "<td>" & spVer & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Operating System Version :</b></td>"
    filetxt.WriteLine "<td>" & osVer & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Manufacturer :</b></td>"
    filetxt.WriteLine "<td>" & strVendor & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Serial Number :</b></td>"
    filetxt.WriteLine "<td>" & strSerial & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Numerical Model :</b></td>"
    filetxt.WriteLine "<td>" & strNumModel & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Model Name :</b></td>"
    filetxt.WriteLine "<td>" & strModel & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Computer Name :</b></td>"
    filetxt.WriteLine "<td>" & strComputer & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>I.P Address :</b></td>"
    filetxt.WriteLine "<td>" & strAddresses & "</td>"
    filetxt.WriteLine "</tr>"
    
    'filetxt.WriteLine "<tr>"
    'filetxt.WriteLine "<td><b>MAC Address :</b></td>"
    'filetxt.WriteLine "<td>" & strMAC & "</td>"
    'filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Total RAM :</b></td>"
    filetxt.WriteLine "<td>" & strRAM & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Actual HDD Capacity :</b></td>"
    filetxt.WriteLine "<td>" & HDD & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Manufacturer HDD Capacity :</b></td>"
    filetxt.WriteLine "<td>" & manuhdd & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Asset Tag :</b></td>"
    filetxt.WriteLine "<td>" & asset & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>Processor :</b></td>"
    filetxt.WriteLine "<td>" & Trim(strScriptStatus) & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "<tr>"
    filetxt.WriteLine "<td><b>A.D Description :</b></td>"
    filetxt.WriteLine "<td>" & ProperCase(username) & space(1) & strModel & space(1) & "(" & strSerial & ")" & "</td>"
    filetxt.WriteLine "</tr>"
    
    filetxt.WriteLine "</table>"
    filetxt.WriteLine "<Center>"
    filetxt.WriteLine "<P>"
    filetxt.WriteLine "==========================================================="
    
    
    Dim ObjExec
    Dim strFromProc
    Dim strOutput
    'output.innerhtml = ""
    Set objShell = CreateObject("WScript.Shell")
    Set ObjExec = objShell.Exec("%comspec% /c ipconfig /all 2>&1")
    Do
        strFromProc = ObjExec.StdOut.ReadLine()
        strOutput = strOutput & strFromProc & "<br>"
        
    Loop While Not ObjExec.Stdout.atEndOfStream
    
    filetxt.WriteLine "<Table Border=1>"
    filetxt.WriteLine "<td>" & strOutput & "</td>"
    filetxt.WriteLine "</table>"
    
    
    
    
    
    filetxt.Close 
    
    msgbox ucase(username) & vbcrlf & spVer & vbcrlf & osVer & vbcrlf & strVendor & vbcrlf & strModel & " ( " & strNumModel & " ) " & vbcrlf & strSerial & vbcrlf & asset & vbcrlf & vbcrlf & strComputer & vbcrlf & strAddresses  & vbcrlf & strScriptStatus & vbcrlf & strRAM & vbcrlf & hdd & "( " & manuhdd & " )" & vbcrlf & ProperCase(username) & space(2) & strModel & space(2) & "(" & strSerial & ")"
    
    wscript.sleep 1000
    
    
    Function ConvertSize(Size) 
    Do While InStr(Size,",") 'Remove commas from size 
        CommaLocate = InStr(Size,",") 
        Size = Mid(Size,1,CommaLocate - 1) & _ 
            Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
    Loop
    
    Suffix = " Bytes" 
    If Size >= 1024 Then suffix = " KB" 
    If Size >= 1048576 Then suffix = " MB" 
    If Size >= 1073741824 Then suffix = " GB" 
    If Size >= 1099511627776 Then suffix = " TB" 
    
    Select Case Suffix 
        Case " KB" Size = Round(Size / 1024, 1) 
        Case " MB" Size = Round(Size / 1048576, 1) 
        Case " GB" Size = Round(Size / 1073741824, 1) 
        Case " TB" Size = Round(Size / 1099511627776, 1) 
    End Select
    
    ConvertSize = Size & Suffix 
    End Function
    
    Function ProperCase(sText)
        '*** Converts text to proper case e.g.  ***'
        '*** surname = Surname                  ***'
        '*** o'connor = O'Connor                ***'
         
            Dim a, iLen, bSpace, tmpX, tmpFull
         
            iLen = Len(sText)
            For a = 1 To iLen
            If a <> 1 Then 'just to make sure 1st character is upper and the rest lower'
                If bSpace = True Then
                    tmpX = UCase(mid(sText,a,1))
                    bSpace = False
                Else
                tmpX=LCase(mid(sText,a,1))
                    If tmpX = " " Or tmpX = "'" Or tmpX ="." Then bSpace = True
                End if
            Else
                tmpX = UCase(mid(sText,a,1))
            End if
            tmpFull = tmpFull & tmpX
            Next
            ProperCase = tmpFull
        End Function
    
    Set objMemory = Nothing
    Set objWMIService = Nothing
    Thank you
    Last edited by mac_shinobi; 13th March 2014 at 11:52 AM.

  2. #2

    Join Date
    Sep 2013
    Location
    Wyoming
    Posts
    32
    Thank Post
    2
    Thanked 5 Times in 4 Posts
    Rep Power
    3
    Converting VBScript to Windows PowerShell

    And about PSv3. I found that you must have .Net Framework 4.5 installed before you attempt to install PSv3
    Last edited by amckinley; 20th March 2014 at 09:03 PM.



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 29th November 2013, 01:26 PM
  2. Replies: 0
    Last Post: 16th August 2011, 01:49 PM
  3. convert vbs to exe
    By TechSupp in forum Scripts
    Replies: 12
    Last Post: 12th January 2010, 02:33 PM
  4. Convert DVD to AVI or MPEG or something
    By adamyoung in forum How do you do....it?
    Replies: 6
    Last Post: 29th March 2006, 11:30 PM
  5. Convert .mrw to .jpg
    By richard in forum Windows
    Replies: 6
    Last Post: 28th February 2006, 02:21 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •