Announcement

Collapse
No announcement yet.

change output (For-Do loop in net.exe findstr)

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

  • change output (For-Do loop in net.exe findstr)

    How can I change this code in the best (correct) way to change the "output" from net.exe, I mean (now) by default its results is "*gpo_result" ie. "*Systeembeheer"
    => I'd prefer it to be just "Systeembeheer" (so without the *), I hope I've explained myself well?

    Code:
    @echo off
    
    :create a list of groups (assign each group a unique :label_number)
    Set "Groups="
    call :define_Groups   :01 "Systeembeheer"
    call :define_Groups   :02 "AEC"
    call :define_Groups   :03 "Tekenkamer"
     goto:begin
    
    +----------------------------------------------------------------------+
       When the user is a Member Of,     (sub routines)
    
       :01 "Systeembeheer"
         echo.user is a member of the security group: "%*"
         ::\
             exit /b 0
    
       :02 "AEC"
         echo.user is a member of the security group: "%*"
         ::\
             exit /b 0
    
       :03 "Tekenkamer"
         echo.user is a member of the security group: "%*"
         ::\
             exit /b 0
    +----------------------------------------------------------------------+
    
    :begin
    :: retrieve user's group memberships
    For /f "tokens=* delims=*" %%* in (
       'net.exe user /domain %username% ^| findstr /c:" \*"'
       ) do (
    
       rem # now search in every token for a phrase using each name 
       rem # separately from the list of the provided group names.,
       for %%! in (%Groups%) do call:compare "*%%~!" "%%*"
       )
    
    pause
    :end
    
    
    goto:EOF  + + +   sub routines   + + +
    :compare
       set "testgroup=%~1"
       set "label=%testgroup:~23,2%"
       set "testgroup=%testgroup:~0,20%"
       set "string=%~2"
       echo.%string%|findstr /ic:"%testgroup%" >nul &&call:%label% %testgroup%
       exit /b 0
    
    :define_Groups
       :: the number will be attached to the group. The number will be used in the batch for calling as sub routine when the user is a member of a matching group.
    
       :: because the net user command is used we have to cut
       :: or extend names to a fixed length of 21 characters long
       Set "testgroup=%~2                     " (adding 21 trailing spaces first)
       Set "testgroup=%testgroup:~0,21%%~1"    (then Trim. And additionally, appending the :number)
    
       if not defined groups (
         Set groups="%testgroup%") else (
         Set groups=%groups%, "%testgroup%")
       exit /b 0
    I used this code from Rems @ http://forums.petri.com/showthread.php?t=60518 (closed topic)

  • #2
    Correct?: change output (For-Do loop in net.exe findstr)

    I assume changing:
    set "testgroup=%testgroup:~0,20%"
    into
    set "testgroup=%testgroup:~1,20%"

    Comment


    • #3
      extend the results? (from 20 to 25)

      How can I extend the results to more characters than 20? (i.e. 25)
      Just changing ~1,20% to ~1,25% won't do it...

      With kind regards,
      Tim

      Comment


      • #4
        Re: change output (For-Do loop in net.exe findstr)

        Can you give an example of the output?

        From the script it looks like the code your changing is the input to the net.exe command so you can't change that. We need to grab and parse the output but that will be more difficult in a batch script then a powershell script if you want to change it up.
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment


        • #5
          Re: change output (For-Do loop in net.exe findstr)

          Yep that is true, you cannot change the behaviour of the net user command. There are not much command line tools available by default on client computers that you can use to get a list of his/her group names from AD.

          Code:
          @echo off
          
          :: This is how the output of groups from the net user command looks like,
          For /f "tokens=* delims=*" %%* in (
             'net.exe user /domain %username% ^| findstr /c:" \*"'
             ) do echo.%%*
          
          echo.& PAUSE &echo.
          
          :: And here are the groups now listed in list format,
          For /f "tokens=2,3 delims=*" %%x in (
             'net.exe user /domain %username% ^| findstr /c:" \*"'
             ) do for %%* in ("%%x", "%%y") do @echo.'%%~*'
          
          echo.& PAUSE &echo.
          With the script above you can see the names of the groups and that they are limited to 20 characters. Names that are longer are cut off and names that are shorter are filled with trailing spaces to 20 char.

          The original batch was written to find a match of a given group name and the group names that it retrieved from AD. In order to make that possible the given group name needs to be formated like the retreived names before you can compare them.
          It is very rare that the first 20 characters in a group name are the same as the first 20 characers of an other existing group - but there are cases it could be a problem though. On the otherhand the trailing spaces for shorter group names are very usefull. So you don't need to use a regular expression to avoid making any wrong matches: i.e. you provided the group name 'engineering' - it gets 9 trailing spaces now you can be sure that it does not match with similar group names like 'engineering management'.

          If you want to see a list of retreived group names I assume you like to change the purpose of the script?
          Like Jeremy suggested also, you better can use an other script language for this, powershell or vbs.

          It would also be possible to write a batch that invokes vbs code that sets an environment variabele that you can expand in your batch. (see sample below), it it fun but it is better to do it solely with vbs or in powershell.

          Batch sample:
          Code:
          @goto:begin of batch ----------------------------------------
          
          :Sub_1
            Setlocal & set "$var=%~1"
            set "$=On Error Resume Next:For Each objGroup in"
            set "$=%$% GetObject("LDAP://"&CreateObject("
            set "$=%$% "ADSystemInfo").userName).Groups:If"
            set "$=%$% len(sGroups)<1 then sGroups=Chr(34)"
            set "$=%$% &objGroup.CN&Chr(34) Else sGroups="
            set "$=%$% sGroups&Chr(44)&Chr(34)&objGroup.CN&"
            set "$=%$% Chr(34)End If:Next : %$var%=sGroups"
            call:vbscript "%$var%" "$"
            endlocal & set "%$var%=%vbscript%"
          exit /b 0
          
          :vbscript launcher
            set "[=about:<script language=VBS>resizeTo 0,0"
            set "FSO=CreateObject("Scripting.FileSystemObject")"
            set "FSO=%FSO%.GetStandardStream(1).WriteLine"
            Set "]=%FSO% %~1:close</script>"
            (call Set script=mshta.exe "%[%:%%%~2%%:%]%")
            for /f "tokens=*" %%! in ('%script%') do set "vbscript=%%!"
          exit /b 0
          
          :begin of batch ---------------------------------------------
          @echo off 
          
          call:Sub_1 MyGroups
          
          for %%* in (%MyGroups%) do echo.%%~*
          
          pause
          /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


          • #6
            Re: change output (For-Do loop in net.exe findstr)

            Current example:
            Code:
            @echo off & cls && echo.
            
            :create a list of groups (assign each group a unique :label_number)
            Set "Groups="
            call :define_Groups   :01 "Administratie"
            call :define_Groups   :02 "BEPERKT"
            call :define_Groups   :03 "Citrix Portal Gebruikers"
            call :define_Groups   :04 "Citrix XD Gebruikers"
            call :define_Groups   :05 "Directie"
            call :define_Groups   :06 "Ontwerpers"
            call :define_Groups   :07 "Opzichters"
            call :define_Groups   :08 "Presentatie"
            call :define_Groups   :09 "Projectleiders"
            call :define_Groups   :10 "REViT"
            call :define_Groups   :11 "Systeembeheer"
            call :define_Groups   :12 "Tekenkamer"
             goto:begin
            
            +----------------------------------------------------------------------+
               When the user is a Member Of,     (sub routines)
            
               :01 "Administratie"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :02 "BEPERKT"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :03 "Citrix Portal Gebruikers"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :04 "Citrix XD Gebruikers"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :05 "Directie"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :06 "Ontwerpers"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :07 "Opzichters"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :08 "Presentatie"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :09 "Projectleiders"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :10 "REViT"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :11 "Systeembeheer"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            
               :12 "Tekenkamer"
                 echo.user is a member of the security group: "%*"
                 ::\
                     exit /b 0
            +----------------------------------------------------------------------+
            
            :begin
            :: retrieve user's group memberships
            For /f "tokens=* delims=*" %%* in (
               'net.exe user /domain %username% ^| findstr /c:" \*"'
               ) do (
            
               rem # now search in every token for a phrase using each name 
               rem # separately from the list of the provided group names.,
               for %%! in (%Groups%) do call:compare "*%%~!" "%%*"
               )
            
            echo.
            PAUSE
            :end
            
            
            goto:EOF  + + +   sub routines   + + +
            :compare
               set "testgroup=%~1"
               set "label=%testgroup:~23,2%"
               set "testgroup=%testgroup:~1,20%"
               REM set "testgroup=%testgroup:~0,20%" [this keeps the original asterisk]
               set "string=%~2"
               echo.%string%|findstr /ic:"%testgroup%" >nul &&call:%label% %testgroup%
               exit /b 0
            
            :define_Groups
               :: the number will be attached to the group. The number will be used in the batch for calling as sub routine when the user is a member of a matching group.
            
               :: because the net user command is used we have to cut
               :: or extend names to a fixed length of 21 characters long
               Set "testgroup=%~2                     " (adding 21 trailing spaces first)
               Set "testgroup=%testgroup:~0,21%%~1"    (then Trim. And additionally, appending the :number)
            
               if not defined groups (
                 Set groups="%testgroup%") else (
                 Set groups=%groups%, "%testgroup%")
               exit /b 0
            => "Citrix Portal Gebruikers" is now echoed as "Citrix Portal Gebrui" in the current output

            PS. aaah so it's purely net.exe behaviour, okay thank you (and I'll give your vbs example a try)
            Last edited by APOC; 19th November 2013, 14:41. Reason: PS.

            Comment

            Working...
            X