Announcement

Collapse
No announcement yet.

VBS script error (access denied)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS script error (access denied)

    I am modifying a script i found on the internet to serve my needs.
    But keep getting a error on line 171.
    I get an access denied.
    Script:
    Code:
    strComputer="."
    
    
    
    dim fs
    dim fictxt
    
    wscript.echo "This list all installed software on this computer"
    wscript.echo "Output can be found in c:\asoftware.txt"
    
    
    set fs=createobject("Scripting.FileSystemObject")
    set fictxt=fs.opentextfile("C:\asoftware.txt",2,true)
    
    Const BASE_KEY = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    Const HKLM = &H80000002
    Dim strDisplayName
    Dim strDisplayVersion
    Dim strInstallLocation
    Dim strDisplayVersionMajor
    Dim strDisplayVersionMinor
    Dim strInstallDate
    Dim strInstallFrom
    Dim lngRtn
    Dim astrSubKeys
    Dim strKey
    Dim vntTmp
    
    Dim j 
    Dim colsoftware
    Dim objsoftware
    
    ReDim vntTmp(5)
    
    debTitre = indiceLg
    indiceLg = indiceLg + 2
    
    on error resume next
    err.clear 
    
    Set colsoftware = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2").ExecQuery("Select * from Win32_Product")
    
    If Err.Number <> 0 Then
    fictxt.writeline "Connexion error " & strComputer
    on error goto 0
    Else
    on error goto 0 
    
    For Each objsoftware In colsoftware
    
    if not(isnull(objsoftware.Caption)) then strCaption=objsoftware.Caption else strCaption=""
    if not(isnull(objsoftware.Description)) then strDesc=objsoftware.Description else strDesc=""
    if not(isnull(objsoftware.IdentifyingNumber)) then strId=objsoftware.IdentifyingNumber else strId=""
    if not(isnull(objsoftware.InstallDate)) then strInstallDate=objsoftware.InstallDate else strInstallDate=""
    if not(isnull(objsoftware.InstallLocation)) then strInstLoc=objsoftware.InstallLocation else strInstLoc=""
    if not(isnull(objsoftware.Name)) then strName=objsoftware.Name else strName=""
    if not(isnull(objsoftware.PackageCache)) then strPC=objsoftware.PackageCache else strPC=""
    if not(isnull(objsoftware.Vendor)) then strVendor=objsoftware.Vendor else strVendor=""
    if not(isnull(objsoftware.Version)) then strVersion=objsoftware.Version else strVersion=""
    
    
    fictxt.writeline strCaption & space(60-len(strCaption)) & _
    strDesc & space(60-len(strDesc)) & _
    strId & space(50-len(strId)) & _
    strInstallDate & space(25-len(strInstallDate)) & _
    strInstLoc & space(70-len(strInstLoc)) & _
    strName & space(70-len(strName)) & _
    strPC & space(40-len(strPC)) & _
    strVendor & space(35-len(strVendor)) & _
    strVersion & space(25-len(strVersion))
    
    
    Next
    
    End If
    
    
    '**************** Registry ***********************
    
    fictxt.writeline vbcrlf & "--------------" & vbcrlf
    
    on error resume next
    err.clear
    
    Set colsoftware = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "/root/default:StdRegProv")
    
    lngRtn = colsoftware.EnumKey(HKLM, BASE_KEY, astrSubKeys)
    
    If Err.Number <> 0 Then
    fictxt.writeline "Connexion error " & strComputer
    on error goto 0
    Else
    on error goto 0
    
    For Each strKey In astrSubKeys
    
    strDisplayName = vbNullString
    strDisplayVersion = vbNullString
    strInstallLocation = vbNullString
    strDisplayVersionMajor = vbNullString
    strDisplayVersionMinor = vbNullString
    strInstallDate = vbNullString
    strInstallFrom = vbNullString
    
    ' ---------------------------------------
    ' Get the display name
    ' ---------------------------------------
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "DisplayName", strDisplayName)
    
    ' If blank, try "Quiet Name
    If Trim(strDisplayName) = vbNullString Then
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "QuietDisplayName", strDisplayName)
    End If
    
    ' If other 2 fail, use the key name
    If Trim(strDisplayName) = vbNullString Then
    strDisplayName = strKey
    End If
    
    if isnull(strDisplayName) then vntTmp(1)="" else vntTmp(1) = strDisplayName
    
    ' ---------------------------------------
    ' Get the Display version or alternate
    ' ---------------------------------------
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "DisplayVersion", strDisplayVersion)
    
    ' Else, concatenate the version info
    If Trim(strDisplayVersion) = vbNullString Then
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "VersionMajor", strDisplayVersionMajor)
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "VersionMinor", strDisplayVersionMinor)
    strDisplayVersion = strDisplayVersionMajor & "." & strDisplayVersionMinor
    End If
    
    ' If all else fails, try just the "Version" string
    If Trim(strDisplayVersionMajor) = vbNullString Then
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "Version", strDisplayVersion)
    End If
    
    if isnull(strDisplayVersion) then vntTmp(2)="" else vntTmp(2) = strDisplayVersion
    
    ' ---------------------------------------
    ' Get the installation location
    ' ---------------------------------------
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "InstallLocation", strInstallLocation)
    
    If Trim(strInstallLocation) = vbNullString Then
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "UninstallString", strInstallLocation)
    End If
    
    if isnull(strInstallLocation) then vntTmp(3)="" else vntTmp(3) = strInstallLocation
    
    ' ---------------------------------------
    ' Get the installation Date
    ' ---------------------------------------
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "InstallDate", strInstallDate)
    
    If Len(Trim(strInstallDate)) > 0 Then
    if isnull(strInstallDate) then vntTmp(4)="" else vntTmp(4)=strInstallDate
    End If
    
    ' ---------------------------------------
    ' Get the install-from location
    ' ---------------------------------------
    lngRtn = colsoftware.GetStringValue(HKLM, BASE_KEY & strKey, "InstallSource", strInstallFrom)
    if isnull(strInstallFrom) then vntTmp(5) ="" else vntTmp(5)=strInstallFrom
    
    ' ---------------------------------------
    ' Export Excel
    ' ---------------------------------------
    If Trim(vntTmp(1)) <> vbNullString And InStr(vntTmp(1), "{") = 0 Then
    fictxt.writeline vntTmp(1) & space(100-len(vntTmp(1))) & _
    vntTmp(4) & space(80-len(vntTmp(4))) & _
    vntTmp(3) & space(110-len(vntTmp(3))) & _
    vntTmp(5) & space(80-len(vntTmp(5))) & _
    vntTmp(2) & space(80-len(vntTmp(2))) 
    
    End If
    
    Next
    
    End If
    
    
    Set colsoftware = Nothing
    Set objWMIService = Nothing
    
    fictxt.close
    
    set fictxt=nothing 
    set fs=nothing
    
    
    wscript.echo "Done"
    Error line 171:
    Code:
    167-' ---------------------------------------
    168-' Export Excel
    169-' ---------------------------------------
    170-If Trim(vntTmp(1)) <> vbNullString And InStr(vntTmp(1), "{") = 0 Then
    171-fictxt.writeline vntTmp(1) & space(100-len(vntTmp(1))) & _
    172-vntTmp(4) & space(80-len(vntTmp(4))) & _
    173-vntTmp(3) & space(110-len(vntTmp(3))) & _
    174-vntTmp(5) & space(80-len(vntTmp(5))) & _
    175-vntTmp(2) & space(80-len(vntTmp(2)))
    Thx
    [Powershell]
    Start-DayDream
    Set-Location Malibu Beach
    Get-Drink
    Lay-Back
    Start-Sleep
    ....
    Wake-Up!
    Resume-Service
    Write-Warning
    [/Powershell]

    BLOG: Therealshrimp.blogspot.com

  • #2
    Re: VBS script error (access denied)

    It is good to show the source, that can give a better description of the script.
    I think you found the script here? http://www.governmentsecurity.org/fo...78&#entry25678
    Remember to give credit where credit is due

    ---------------
    About the 'error on line 171' error
    aah "don't (trouble-) shoot the messenger" if you did not first comment out all the 'On Error ..' statements in the script.

    In fact an error was already raised in an earlier stage.
    It has to do with the way the value for the Space() function is retrieved, this is not scripted here as being error proof.

    try this
    Code:
    '------<snip>
    
    For Each objsoftware In colsoftware
    
      if not(isnull(objsoftware.Caption)) _
        then strCaption=objsoftware.Caption else strCaption=""
      if not(isnull(objsoftware.Description)) _
        then strDesc=objsoftware.Description else strDesc=""
      if not(isnull(objsoftware.IdentifyingNumber)) _
        then strId=objsoftware.IdentifyingNumber else strId=""
      if not(isnull(objsoftware.InstallDate)) _
        then strInstallDate=objsoftware.InstallDate else strInstallDate=""
      if not(isnull(objsoftware.InstallLocation)) _
        then strInstLoc=objsoftware.InstallLocation else strInstLoc=""
      if not(isnull(objsoftware.Name)) _
        then strName=objsoftware.Name else strName=""
      if not(isnull(objsoftware.PackageCache)) _
        then strPC=objsoftware.PackageCache else strPC=""
      if not(isnull(objsoftware.Vendor)) _
        then strVendor=objsoftware.Vendor else strVendor=""
      if not(isnull(objsoftware.Version)) _
        then strVersion=objsoftware.Version else strVersion=""
    
      ' determine internal spaces (is) for filling up to column width !
      isCaption = 60-len(strCaption)
       If isCaption <1 Then isCaption = 1
      isDesc = 60-len(strDesc)
       If isDesc <1 Then isDesc = 1
      isId = 50-len(strId)
       If isId <1 Then isId = 1
      isInstallDate = 25-len(strInstallDate)
       If isInstallDate <1 Then isInstallDate = 1
      isInstLoc = 70-len(strInstLoc)
       If isInstLoc <1 Then isInstLoc = 1
      isName = 70-len(strName)
       If isName <1 Then isName = 1
      isPC = 40-len(strPC)
       If isPC <1 Then isPC = 1
      isVendor = 35-len(strVendor)
       If isVendor <1 Then isVendor = 1
      isVersion = 25-len(strVersion)
       If isVersion <1 Then isVersion = 1
    
      fictxt.writeline strCaption & space(isCaption) & _
        strDesc & space(isDesc) & _
        strId & space(isId) & _
        strInstallDate & space(isInstallDate) & _
        strInstLoc & space(isInstLoc) & _
        strName & space(isName) & _
        strPC & space(isPC) & _
        strVendor & space(isVendor) & _
        strVersion & space(isVersion)
    
    Next 
    
    '------<snip>
    
    ' ---------------------------------------
    ' Export Excel   EDIT: Excel???
    ' ---------------------------------------
    If Trim(vntTmp(1)) <> vbNullString And InStr(vntTmp(1), "{") = 0 Then
    
      isvntTmp1 = 100-len(vntTmp(1))
        If isvntTmp1 < 1 then isvntTmp1 = 1
      isvntTmp4 = 80-len(vntTmp(4))
        If isvntTmp4 < 1 then isvntTmp4 = 1
      isvntTmp3 = 110-len(vntTmp(3))
        If isvntTmp3 < 1 then isvntTmp3 = 1
      isvntTmp5 = 80-len(vntTmp(5))
        If isvntTmp5 < 1 then isvntTmp5 = 1
      isvntTmp2 = 80-len(vntTmp(2))
        If isvntTmp2 < 1 then isvntTmp2 = 1
    
      fictxt.writeline vntTmp(1) & space(isvntTmp1) & _
        vntTmp(4) & space(isvntTmp4) & _
        vntTmp(3) & space(isvntTmp3) & _
        vntTmp(5) & space(isvntTmp5) & _
        vntTmp(2) & space(isvntTmp2) 
    
    End If
    
    '------<snip>
    When afterwards it would show that the standard width of some columns is not sufficient, then raise the fixed value,
    e.g. isCaption = 60-len(strCaption) -> to raise the column to 65 characters width: isCaption = 65-len(strCaption)

    \Rems
    Last edited by Rems; 10th March 2008, 17:59. Reason: Excel?

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: VBS script error (access denied)

      I was not planning on taking credit for that script, as stated before i am a script illiterate. Which means that i have search the net for usable scripts .
      And indeed i found the script on that site.

      Well rems, you have done it again thx!
      [Powershell]
      Start-DayDream
      Set-Location Malibu Beach
      Get-Drink
      Lay-Back
      Start-Sleep
      ....
      Wake-Up!
      Resume-Service
      Write-Warning
      [/Powershell]

      BLOG: Therealshrimp.blogspot.com

      Comment


      • #4
        Re: VBS script error (access denied)

        I was not planning on taking credit for that script
        I know, and that was not what I meant (btw you did already wrote that you found the script, and not wrote the script your self).

        By mentioning the author's name or linking to the source when publishing most of his/here script on an other public site you give him credit for his efforts, I'm sure he/she would appreciate it.
        (And of course, we hope others will do the same when a solution from the petri site or one of the forums is used in an forum on an other site )

        In this case we are not sure who the author is though, but if you remember where you got it from, just give the link.
        Also, in this case we can use the link to get more details and background about that script.

        -----------------------------------

        Glad I could help Killerbe


        I noticed the line "Export Excel" somewhere in the script.
        The samples in my previous reply, will always show the complete value of each variable in the column, even when it is longer than the column width.
        That gives you the posibility to adjust the width of each column.
        In your script different columns have different sizes.

        But.. when it is important that all columns must have exact the same width (e.g. when your intentions are to import the outputfile to tables), then you 'cannot adjust the max characters value of each column!!!, no execptions allowed in that case.
        Instead of changing some columns, you must cut the length of some string values to fit in the column.

        'Here is just an example
        Code:
        Const COLUMN = 35 'characters width
        
        strMyVariable1 = "This is a textstring"
        strMyVariable2 = "this is a kind of very long textstring, it is too long to fit in the column"
        strMyVariable3 = "this textstring will easily fit"
        
        '*
        ' Cut textstring length to fit column width:
        ' ------------------------------------------
           strMyVariable1  = Left(strMyVariable1, COLUMN) 'maximum textstring size now fits column width
           intspacesMyVariable1 = (COLUMN - Len(strMyVariable1))
        
           strMyVariable2  = Left(strMyVariable2, COLUMN) 'maximum textstring size now fits column width
           intspacesMyVariable2 = (COLUMN - Len(strMyVariable2))
        
           strMyVariable3  = Left(strMyVariable3, COLUMN) 'maximum textstring size now fits column width
           intspacesMyVariable3 = (COLUMN - Len(strMyVariable3))
           
        '*
        ' Values to rows:
        ' ---------------
        strRow1 = strMyVariable1 & Space(intspacesMyVariable1) & "NEXT column starts"
        strRow2 = strMyVariable2 & Space(intspacesMyVariable2) & "NEXT column starts"
        strRow3 = strMyVariable3 & Space(intspacesMyVariable3) & "NEXT column starts"
        
        '*
        ' create Column heads:
        ' --------------------
        strColumnName = "column#1"
        strColumn1 = Left(strColumnName,COLUMN) & Replace(space(COLUMN - Len(strColumnName))," ",".")
        
        strColumnName = "column#2"
        strColumn2 = Left(strColumnName,COLUMN) & Replace(space(COLUMN - Len(strColumnName))," ",".")
        
        strColumnName = "column#3"
        strColumn3 = Left(strColumnName,COLUMN) & Replace(space(COLUMN - Len(strColumnName))," ",".")
        
        '*
        ' Output to file, and create some extra lines:
        ' --------------------------------------------
        Set objShell = CreateObject("Wscript.Shell")
        objShell.Run _
            ("%comspec% /c @echo." & strColumn1 & strColumn2 &strColumn3 & " >c:\teststringOut.txt"), _
                1, TRUE
        
        Do until i > 9
        i = i + 1   
        objShell.Run _
            ("%comspec% /c @echo.Row.." & space(30) & strRow1 & " >>c:\teststringOut.txt"), _
                1, TRUE
        objShell.Run _
            ("%comspec% /c @echo.Row.." & space(30) & strRow2 & " >>c:\teststringOut.txt"), _
                1, TRUE
        objShell.Run _
            ("%comspec% /c @echo.Row.." & space(30) & strRow3 & " >>c:\teststringOut.txt"), _
                1, TRUE
        Loop
        wsh.sleep 500
        objShell.Run _
            ("%comspec% /c @notepad.exe c:\teststringOut.txt") ,1, False
        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X