Help with a VBS

Home Forums Scripting Windows Script Host Help with a VBS

This topic contains 4 replies, has 3 voices, and was last updated by Avatar James Haigh 6 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Avatar
    dfortier
    Member
    #161430

    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.

    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:scriptsresult.txt

    PATH_TOOLS = “\servernamesoftwaredistribution$Toolsaccesschk.exe”
    strCommand = “WshShell.run %comspec% /K ” & ” ” & PATH_TOOLS & ” ” &groupe & ” ” & ” -s ” & ” ” &partage & ” ” &” >> c:scriptsresult.txt 2>&1″

    If partage <> “” Then
    WshShell.run strCommand,1,FALSE
    End if[/CODE][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:scriptsresult.txt

    PATH_TOOLS = “\servernamesoftwaredistribution$Toolsaccesschk.exe”
    strCommand = “WshShell.run %comspec% /K ” & ” ” & PATH_TOOLS & ” ” &groupe & ” ” & ” -s ” & ” ” &partage & ” ” &” >> c:scriptsresult.txt 2>&1″

    If partage <> “” Then
    WshShell.run strCommand,1,FALSE
    End if[/CODE]

    Avatar
    Rems
    Moderator
    #228282

    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 filesfile.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 = “\servernamesoftwaredistribution$Toolsaccesschk.exe”

    resultat = “c:scriptsresult.txt”
    ‘ [color=red](Make sure that the folder c:scripts exist before running the script!)[/color]

    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

    Avatar
    James Haigh
    Member
    #387955

    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 “\someserversoftwaredistribution$Toolsaccesschk.exe” “c’folder” -s “group” “c:scriptsresult.txt”2>&1

    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 = “\someserversoftwaredistribution$Toolsaccesschk.exe”

    resultat = “c:scriptsresult.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[/Code][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 = “\someserversoftwaredistribution$Toolsaccesschk.exe”

    resultat = “c:scriptsresult.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[/Code]

    Avatar
    Rems
    Moderator
    #228284

    Re: Help with a VBS

    dfortier;272140 wrote:
    […]

    the result of this command wscript.echo strCommand is %comspec% /C “\someserversoftwaredistribution$Toolsaccesschk.exe” “c’folder” -s “group” “c:scriptsresult.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 “”\someserversoftwaredistribution$Toolsaccesschk.exe” “groupname” -s “C:Folder” >> “c:scriptsresult.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:windowssystem32notepad.exe”” “”c:my text filesfile.txt”””””
    wscript.echo strCmd

    This is the modified code:

    Code:
    partage = “C:folder”
    groupe = “administrators”

    resultat = “c:scriptsresult.txt”

    PATH_TOOLS = “\someserversoftwaredistribution$Toolsaccesschk.exe”

    [color=red]strCommand = “%comspec% /C “”””” & PATH_TOOLS & “”” “”” & _
    groupe & “”” -s “”” & partage & “”” >> “”” & resultat & “”” 2>&1″””[/color]

    rem wscript.echo strCommand

    If partage “” Then
    Set WshShell = WScript.CreateObject(“WScript.Shell”)
    WshShell.run strCommand,1,FALSE
    End if

    /Rems

    Avatar
    James Haigh
    Member
    #387956

    Re: Help with a VBS

    Thank you. It works now.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.