Converting VB Script Questions

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #5773

    by Lery at 2013-04-05 06:26:29

    I wanted to create a new thread so that I could take a different approach than I previously did in thread http://powershell.org/discuss/viewtopic.php?f=2&t=1668 It got so confusing that I think I lost myself 🙂 So with that said, let me ask for what I'm trying to accomplish in a simpler way. I have a VB script that I want to convert to Powershell. I've used http://technet.microsoft.com/en-us/library/hh848796.aspx but clearly cannot get it working. So maybe you fine folks can? Here is the VB script:

    '===================================================================================================================
    ' On Error Resume Next

    'Create instance of Wbem service object and connect to namespace
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")

    'Fire WMI Query
    Set objCIMObj = objWMIService.ExecQuery("select * from Win32_BIOS")

    '===================================================================================================================
    strBIOSVersion = "Unknown"

    'Create instance of NSE component
    dim nse
    set nse = WScript.CreateObject ("Altiris.AeXNSEvent")

    ' Set the header data of the NSE
    ' Please don't modify this GUID
    nse.To = "{1592B913-72F3-4C36-91D2-D4EDA21D2F96}"
    nse.Priority = 1

    'Create Inventory data block. Here assumption is that the data class with below guid is already configured on server
    dim objDCInstance
    set objDCInstance = nse.AddDataClass ("{53f7ce34-2007-4169-80aa-0109e38686ea}")

    dim objDataClass
    set objDataClass = nse.AddDataBlock (objDCInstance)

    For each objInfo in objCIMObj

    strBiosCharacteristics = Join(objInfo.BiosCharacteristics, ",")
    On Error Resume Next
    strBIOSVersion = Join(objInfo.BIOSVersion, ",")

    'Add a new row
    dim objDataRow
    set objDataRow = objDataClass.AddRow
    'Set columns

    objDataRow.SetField 0, strBiosCharacteristics
    objDataRow.SetField 1, strBIOSVersion
    objDataRow.SetField 2, objInfo.BuildNumber
    objDataRow.SetField 3, objInfo.Caption
    objDataRow.SetField 4, objInfo.CodeSet
    objDataRow.SetField 5, objInfo.CurrentLanguage
    objDataRow.SetField 6, objInfo.Description
    objDataRow.SetField 7, objInfo.IdentificationCode
    objDataRow.SetField 8, objInfo.InstallableLanguages
    objDataRow.SetField 9, objInfo.InstallDate
    objDataRow.SetField 10, objInfo.LanguageEdition
    objDataRow.SetField 11, objInfo.Manufacturer
    objDataRow.SetField 12, objInfo.Name
    objDataRow.SetField 13, objInfo.OtherTargetOS
    objDataRow.SetField 14, objInfo.PrimaryBIOS
    objDataRow.SetField 15, objInfo.ReleaseDate
    objDataRow.SetField 16, objInfo.SerialNumber
    objDataRow.SetField 17, objInfo.SMBIOSBIOSVersion
    objDataRow.SetField 18, objInfo.SMBIOSMajorVersion
    objDataRow.SetField 19, objInfo.SMBIOSMinorVersion
    objDataRow.SetField 20, objInfo.SMBIOSPresent
    objDataRow.SetField 21, objInfo.SoftwareElementID
    objDataRow.SetField 22, objInfo.SoftwareElementState
    objDataRow.SetField 23, objInfo.Status
    objDataRow.SetField 24, objInfo.TargetOperatingSystem
    objDataRow.SetField 25, objInfo.Version

    Next

    nse.SendQueued

    by coderaven at 2013-04-05 07:37:52

    ok. Lets just break it down into pieces and just get the data first.

    Function Get-BiosData
    {
    [CmdletBinding()]
    Param([String[]]$ComputerName)
    Foreach ($Computer in $ComputerName)
    {
    try {
    Write-Verbose "Processing Computer $Computer"
    $Bios = Get-WMIObject -ComputerName $Computer -Class Win32_BIOS
    New-Object -TypeName PSObject -Property @{ `
    "0"=([string]::Join($Bios.BiosCharacteristics, ","));
    "1"= ([string]::Join($Bios.strBIOSVersion,","));
    "2"=$Bios.BuildNumber;
    "3"=$Bios.Caption;
    .
    .
    .
    "25"=$Bios.Version;}
    }#end try
    catch { Write-Error "Unable to connect to $Computer"}
    }
    }

    When you run this and call the function, the data you are wanting will be available to you an can be stored in CSV, XML, etc. You can change the 0,1,2 to a name that you would like.

    Once you get this then we will look into the data import to Altiris.

    by Lery at 2013-04-05 08:49:14

    [quote="coderaven"]ok. Lets just break it down into pieces and just get the data first.

    Function Get-BiosData
    {
    [CmdletBinding()]
    Param([String[]]$ComputerName)
    Foreach ($Computer in $ComputerName)
    {
    try {
    Write-Verbose "Processing Computer $Computer"
    $Bios = Get-WMIObject -ComputerName $Computer -Class Win32_BIOS
    New-Object -TypeName PSObject -Property @{ `
    "0"=([string]::Join($Bios.BiosCharacteristics, ","));
    "1"= ([string]::Join($Bios.strBIOSVersion,","));
    "2"=$Bios.BuildNumber;
    "3"=$Bios.Caption;
    .
    .
    .
    "25"=$Bios.Version;}
    }#end try
    catch { Write-Error "Unable to connect to $Computer"}
    }
    }

    When you run this and call the function, the data you are wanting will be available to you an can be stored in CSV, XML, etc. You can change the 0,1,2 to a name that you would like.

    Once you get this then we will look into the data import to Altiris.[/quote]

    I had to comment out lines 15, 16, and 17. It ran fine. I called the function with Get-BiosData localhost Output is valid.

    by coderaven at 2013-04-05 10:25:48

    The ... Was where you add the rest of your data. Your vbscript is pulling a lot of fields that you can just translate to the PowerShell psobject.

    by Lery at 2013-04-05 12:33:44

    coderaven, I'm not I'm understanding you. Your function works. Works great. The ... I can modify to what I want returned. But, I'm asking to have the VBScript code I entered translated to PowerShell.

    by coderaven at 2013-04-05 18:44:44

    As for collecting the data, here is the completed script.
    Function Get-BiosData
    {
    [CmdletBinding()]
    Param([String[]]$ComputerName = ".")
    Foreach ($Computer in $ComputerName)
    {
    try {
    Write-Verbose "Processing Computer $Computer"
    $Bios = Get-WMIObject -ComputerName $Computer -Class Win32_BIOS
    New-Object -TypeName PSObject -Property @{ `
    "0"=([string]::Join($Bios.BiosCharacteristics, ","));
    "1"= ([string]::Join($Bios.strBIOSVersion,","));
    "2"=$Bios.BuildNumber;
    "3"=$Bios.Caption;
    "4"=$Bios.CodeSet
    "5"=$Bios.CurrentLanguage
    "6"=$Bios.Description
    "7"=$Bios.IdentificationCode
    "8"=$Bios.InstallableLanguages
    "9"=$Bios.InstallDate
    "10"=$Bios.LanguageEdition
    "11"=$Bios.Manufacturer
    "12"=$Bios.Name
    "13"=$Bios.OtherTargetOS
    "14"=$Bios.PrimaryBIOS
    "15"=$Bios.ReleaseDate
    "16"=$Bios.SerialNumber
    "17"=$Bios.SMBIOSBIOSVersion
    "18"=$Bios.SMBIOSMajorVersion
    "19"=$Bios.SMBIOSMinorVersion
    "20"=$Bios.SMBIOSPresent
    "21"=$Bios.SoftwareElementID
    "22"=$Bios.SoftwareElementState
    "23"=$Bios.Status
    "24"=$Bios.TargetOperatingSystem
    "25"=$Bios.Version;}
    }#end try
    catch { Write-Error "Unable to connect to $Computer"}
    }#end foreach
    }

    Now you just need to connect to the NSE object and load it.

    $nse = new-object -comobject Altiris.AeXNSEvent
    $nse.priority = 1
    $nse.To = "{1592B913-72F3-4C36-91D2-D4EDA21D2F96}"
    $objDCInstance = $nse.AddDataClass("{53f7ce34-2007-4169-80aa-0109e38686ea}")
    $objDataClass = $nse.AddDataBlock($objDCInstance)
    $nse.sendqueued()

    The is a snip of the com object workings.

    All of that together should allow you to get your data loaded using PowerShell.

You must be logged in to reply to this topic.