Windows 8.1 batch checking if computer is part of a workgroup and changing if necessary

0

I've got SYSTEMINFO | FIND /I "DOMAIN:" which displays if it's part of a Domain or Workgroup. But I'm trying to figure out how to take the result of that and if it's anything other than Company.LLC to goto :JoinWorkgroup. I would imagine it should look something like this:

IF SYSTEMINFO | FIND /I "DOMAIN:" NOT = "Company.LLC" THEN GOTO :JoinWorkgroup
IF SYSTEMINFO | FIND /I "DOMAIN:" = "Company.LLC" THEN GOTO :NextVerification

I can get IF NOT EXIST to work with directories, but can't figure out how to tie it into results of prompt utilities.

Sandfrog

Posted 2015-02-09T17:50:45.573

Reputation: 137

Are you confined to batch files? If not, I'd recommend going with PowerShell instead. You'll end up with cleaner files and it'll be easier to follow the logic. – Thor – 2015-02-09T18:40:28.553

Yeah, sort of am. I have a "sophisticated" batch that does a number of things in order. To call a powershell script might introduce some problems. Thanks for the response! – Sandfrog – 2015-02-10T16:06:52.190

Answers

0

For the first look, launch next command from command line:

for /F "tokens=1*" %G in ('SYSTEMINFO ^| FIND /I "DOMAIN:"') do @echo %G %H

For use in a batch script double the percent sign %: change %G to %%G and %H to %%H. Then an applicable code snippet in your batch script could look as follows (retain line spacing, please):

set "sDomain=WORKGROUP"
for /F "tokens=1*" %%G in ('SYSTEMINFO ^| FIND /I "DOMAIN:"') do set "sDomain=%%~H"
IF "%sDomain%"=="Company.LLC" (
    GOTO :NextVerification 
) ELSE (
    GOTO :JoinWorkgroup
)

or, if you would prefer a solution without any auxiliary variable:

for /F "tokens=1*" %%G in ('SYSTEMINFO ^| FIND /I "DOMAIN:"') do ( 
  IF "%%~H"=="Company.LLC" (
      GOTO :NextVerification 
  ) ELSE (
      GOTO :JoinWorkgroup
  )
)

Edit (although accepted): use %%~H with the ~ argument modifier to remove surrounding quotes (") if any.

Resource:

JosefZ

Posted 2015-02-09T17:50:45.573

Reputation: 9 121

Worked great! And thank you for the thought out response and Resource! – Sandfrog – 2015-02-11T02:14:12.830