Announcement

Collapse
No announcement yet.

Help with a VBS

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

  • Help with a VBS

    I have trouble with my script. I want to output the result from the command accesschk to a text file. When i run it from the command prompt it is ok. It create and populated my text file. But when i run the vbs script i see noting.

    Code:
    On Error Resume Next
    
    Dim partage,groupe,resultat,strCommand
    
    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    partage = inputbox("Entrer le chemin du partage  valider:")
    groupe = inputbox("Entrer le nom du groupe  valider:")
    'resultat = c:\scripts\result.txt
    
    PATH_TOOLS = "\\servername\softwaredistribution$\Tools\accesschk.exe"
    strCommand = "WshShell.run %comspec% /K " & " " & PATH_TOOLS & " " &groupe  & " " & " -s "  & " " &partage & " " &" >> c:\scripts\result.txt 2>&1"
    
    If partage <> "" Then 
    	WshShell.run strCommand,1,FALSE
    End if
    Last edited by Rems; 26th April 2013, 18:23.

  • #2
    Re: Help with a VBS

    Strings in vbscript should always be wrapped between double quotes.
    If if there are double quote signs within a string these should be doubled.

    i.e.:
    strCmd = "notepad.exe ""c:\my text files\file.txt"""
    wscript.echo strCmd



    Your code would then look like:
    Code:
    rem On Error Resume Next
    
    Dim partage, groupe, resultat, strCommand
    
    partage = trim(inputbox("Entrer le chemin du partage  valider:"))
    groupe = trim(inputbox("Entrer le nom du groupe  valider:"))
    
    PATH_TOOLS = "\\servername\softwaredistribution$\Tools\accesschk.exe"
    
    resultat = "c:\scripts\result.txt"
    ' (Make sure that the folder c:\scripts exist before running the script!)
    
    strCommand = "%comspec% /C """ & PATH_TOOLS & """ """ & groupe  & """ -s """ & partage & """ >> """ & resultat & """ 2>&1"
    
    If partage <> "" Then
       Set WshShell = WScript.CreateObject("WScript.Shell")
       WshShell.run strCommand,1,FALSE
    End if
    /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


    • #3
      Re: Help with a VBS

      thank you for the answer. But it still does not create the text file result.txt
      I have added a wscript.echo to see what is the output of the variable strCommand.

      the result of this command wscript.echo strCommand is %comspec% /C "\\someserver\softwaredistribution$\Tools\accessch k.exe" "c'\folder" -s "group" "c:\scripts\result.txt"2>&1

      Code:
      Dim partage, groupe, resultat, strCommand
      
      partage = trim(inputbox("Entrer le chemin du partage  valider:"))
      groupe = trim(inputbox("Entrer le nom du groupe  valider:"))
      
      PATH_TOOLS = "\\someserver\softwaredistribution$\Tools\accesschk.exe"
      
      resultat = "c:\scripts\result.txt"
      ' (Make sure that the folder c:\scripts exist before running the script!)
      
      strCommand = "%comspec% /C """ & PATH_TOOLS & """ """ & groupe  & """ -s """ & partage & """ >> """ & resultat & """ 2>&1"
      
      If partage <> "" Then
         Set WshShell = WScript.CreateObject("WScript.Shell")
         wscript.echo strCommand
         WshShell.run strCommand,1,FALSE
      End if
      Last edited by Rems; 26th April 2013, 21:40.

      Comment


      • #4
        Re: Help with a VBS

        Originally posted by dfortier View Post
        [...]

        the result of this command wscript.echo strCommand is %comspec% /C "\\someserver\softwaredistribution$\Tools\accessch k.exe" "c'\folder" -s "group" "c:\scripts\result.txt"2>&1

        [...]
        Firstly, how come the name of the group and path are exchanged? it will of course not work this way.
        And also note the single quote in the path that should not be there, maybe typos? like there is also a missing space after the outputfile.

        wscript.echo should actually show the following:
        Code:
        %comspec% /C ""\\someserver\softwaredistribution$\Tools\accesschk.exe" "groupname" -s "C:\Folder" >> "c:\scripts\result.txt" 2>&1"
        So a pair of extra double-quote signs needed to be in there. Because of the behavior of the cmd.exe /c statement. The commands that comes after cmd /c should not start with quotes and it cannot contain any special signs (like >> ). If it does then the whole that comes after cmd /c must be wrapped in double qoutes. And for the string in the vbscript that means wrapped in doubled double-quote signs because it are quotes inside a string:
        i.e.:
        strCmd = "cmd /c """"c:\windows\system32\notepad.exe"" ""c:\my text files\file.txt"""""
        wscript.echo strCmd


        This is the modified code:
        Code:
        partage = "C:\folder"
        groupe = "administrators"
        
        resultat = "c:\scripts\result.txt"
        
        PATH_TOOLS = "\\someserver\softwaredistribution$\Tools\accesschk.exe"
        
        strCommand = "%comspec% /C """"" & PATH_TOOLS & """ """ & _
         groupe & """ -s """ & partage & """ >> """ & resultat & """ 2>&1"""
        
        rem wscript.echo strCommand
        
        If partage <> "" Then
           Set WshShell = WScript.CreateObject("WScript.Shell")
           WshShell.run strCommand,1,FALSE
        End if
        /Rems
        Last edited by Rems; 27th April 2013, 09:36.

        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


        • #5
          Re: Help with a VBS

          Thank you. It works now.

          Comment

          Working...
          X