Problem VBScript IF check shenanigans

Discussion in 'Scripting & Programming' started by GSteer, Oct 2, 2010.

  1. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Evenin' all.

    Been knocking up a small bit of VBScript to add to a couple of customers domain logon scripts to do three basic things.

    Note: yes I know it needs tidying up and it's probably inefficient but let me get it working first then I'll sort out my rubbish coding :)

    1) Warn the user of the pending install (if testing shows no real slow down then this will be removed)
    2) Check for the .Net dependancy, if not there then install it
    3) Check for our agent, if not there then install:

    Onto the code:
    Code:
    'Variable Declarations and Sets
    Dim fso,WshShell
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("wscript.shell")
    
    'If Inline PAM Agent is not installed, warn that we are about to install it and this may take some time
    If Not fso.FolderExists("%SystemRoot%\LTSvc\null") Then 
        result = MsgBox("Inline Pro-Active Monitoring and dependencies will now install. Please click OK and wait until your desktop
    appears, this may take a few minutes.",0,"Inline PAM Installation")
    End If
    
    'If Inline PAM Agent is not installed, install it, and check for .Net 3.5 first and install if not there (LabTech)
    If Not fso.FolderExists("%SystemRoot%\LTSvc\null") Then
         
        If Not fso.FolderExists("%SystemRoot%\Microsoft.NET\Framework\v3.5\null") Then    
            result = result = MsgBox(".net not exist framework installing")
            result = WshShell.Run ("dotNetFx35setup.exe /q /norestart",0,True)
        End If
        
        If Not fso.FolderExists("%SystemRoot%\LTSvc\null") Then 
            result = MsgBox("agent not exist installing")
            result = WshShell.Run ("IL_Agent_Install.exe /q",0,True)
        
        End If
    End If
    
    'Cleanup
    set fso = Nothing
    set WshShell = Nothing
    Ok, the additiona MsgBox entries in the last statements are there for testing.

    My problem: If nothing exists on an client machine, then it works (tested on an XP box signing on to the domain). If I then log off / log on this machine, it works again O.o - it shouldn't - it should be seeing folders there and evaluating the [If Not] as false and ergo not doing anything.

    So - any pointers, it's been a looooong while since I did any coding so slap me around the head with a wet fish and tell me my logic is wrong please, and then after I've recovered tell me how it should be.

    Cheers

    Greg
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  2. Fergal1982

    Fergal1982 Petabyte Poster

    4,196
    172
    211
    I dont believe that folderexists understands %SystemRoot%

    Try

    Code:
    strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%") 
    
    MsgBox(fso.folderexists(strWinDir))
     
    Last edited: Oct 2, 2010
    Certifications: ITIL Foundation; MCTS: Visual Studio Team Foundation Server 2010, Administration
    WIP: None at present
  3. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Aah I'll bounce in later and give that a test, cheers.
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  4. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    In VBScript you always have to use the expand function on environment variables, it doesn't understand them like if you were using one in a batch script or something like that.
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
  5. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Just tried with your string Fergal, getting an error:

    Error: Object required: '[string: "C:\Windows"]'
    Code: 800A01A8
    Source: Microsoft VBScript runtime error

    which locates to the strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%") line.

    Haven't had time to look into it myself, thought I'd post it in case someone recognized it.
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  6. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    Can you post your full version please. You may have just omitted to append a "\" somewhere.
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
  7. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Current testing version stands at this:

    Code:
    'Author	|	Greg Steer
    'Date	|	2010-10-01
    'Purpose|	Install Inline LabTech Client Agent and .Net 3.5 dependency (contains 2.0) if required
    
    
    'Variable Declarations and Sets
    Dim fso,WshShell
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("wscript.shell")
    Set strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%") 
    
    MsgBox(fso.folderexists(strWinDir))
    
    'If Inline PAM Agent is not installed, warn that we are about to install it and this may take some time
    If fso.FolderExists("strWinDir\LTSvc\null") Then 
    	result = MsgBox("Inline Pro-Active Monitoring and dependencies will now install. Please click OK and wait until your desktop appears, this may take a few minutes.",0,"Inline PAM Installation")
    End If
    
    'If Inline PAM Agent is not installed, install it, and check for .Net 3.5 first and install if not there (LabTech)
    If fso.FolderExists("strWinDir\LTSvc\null") Then
     	
    	If fso.FolderExists("strWinDir\Microsoft.NET\Framework\v3.5\null") Then    
    		result = result = MsgBox(".net not exist framework installing")
    		result = WshShell.Run ("dotNetFx35setup.exe /q /norestart",0,True)
    	End If
    	
    	If fso.FolderExists("strWinDir\LTSvc\null") Then 
    		result = MsgBox("agent not exist installing")
    		result = WshShell.Run ("IL_Agent_Install.exe /q",0,True)
    	
    	End If
    End If
    
    'Cleanup
    set fso = Nothing
    set WshShell = Nothing
    Set strWinDir = Nothing
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  8. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    Try changing
    Code:
    Set strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%")
    to

    Code:
    strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%")
    You only use set when you are referencing an object. In this case you are using a function to assign the result to a string.
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
  9. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Aha - will try that, cheers.
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  10. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Ok, that clears the error, apologies Fergal for adding in the Set that wasn't in your code.

    Query on the If evaluation then:

    Code:
    'Variable Declarations and Sets
    Dim fso,WshShell
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("wscript.shell")
    strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%") 
    
    MsgBox(fso.folderexists("strWinDir\LTSvc\"))
    Just testing the detection of folders using the above, I'd expect that to evaluate as True, it doesn't, it's coming up as false even though it does exist and I can CD to it using %systemroot%\LTSvc.

    I expect it's a very basic beginners VBS mistake but any help is appreciated :oops:
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  11. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    You are using a variable as a text string.

    Code:
    'Variable Declarations and Sets
    Dim fso,WshShell
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = CreateObject("wscript.shell")
    strWinDir = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%") 
    
    MsgBox(fso.folderexists[COLOR="Red"](strWinDir & "\LTSvc\")[/COLOR])
    Try That or something like that.
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA
  12. GSteer

    GSteer Megabyte Poster

    627
    31
    109
    Well thanks Fergal and Chris - it's up and running now - thanks for the help and repped.
     
    Certifications: BSc. (Comp. Sci.), MBCS, MCP [70-290], Specialist [74-324], Security+, Network+, A+, Tea Lord: Beverage Brewmaster | Courses: LFS101x Introduction to Linux (edX)
    WIP: CCNA Routing & Switching
  13. ChrisH1979

    ChrisH1979 Byte Poster

    225
    9
    37
    Glad you got it sorted :D VB is my favourite scripting language, I know it very well. I need to start learning PowerShell now though :P
     
    Certifications: MCITP:SA, MCSA, MCTS:Win 7, Application Infrastructure
    WIP: MCITP:EA

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.