Skip navigation

JSI Tip 2910. Building compound commands by piping.


The Windows NT 4.0 and Windows 2000 CMD processor allows you to pipe the output of a command as the input to another command.

You can use this ability to create compound commands.

For example, you can pipe the output of a net group /domain command into a Find command, in order to determine if a user is a member of a group. I have created IsInGrp.bat to demonstrate this ability. The syntax is:

IsInGrp DomainGroup UserName

IsInGrp.bat contains:

@echo off
setlocal
set grp=%1
set usr=%2
set grp=%grp:"=%
set usr=%usr:"=%
Net Group /Domain "%grp%"|Find /i "%usr%">null
If ErrorLevel 1 (
    Echo %usr% is NOT a member of the %grp% group.
) Else (
    Echo %usr% is a member of the %grp% group.
)
endlocal
NOTE: See tip 0758 for the IF / ELSE syntax.

If you wish return a fnd variable, instead of the Echo:

@echo off
setlocal
set grp=%1
set usr=%2
set grp=%grp:"=%
set usr=%usr:"=%
Net Group /Domain "%grp%"|Find /i "%usr%">null
If ErrorLevel 1 (
    set fnd=N
) Else (
    set fnd=Y
)
endlocal&set fnd=%fnd%
Example of use:
IsInGrp "Domain Admins" "%UserName%"
if "%fnd%"=="Y" goto ismbr
@echo %UserName% is not a member of the Domain Admin group
goto end
:ismbr
@echo %UserName% is a member of the Domain Admin group
:end

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish