Link to home
Start Free TrialLog in
Avatar of trcadmin
trcadmin

asked on

how to exclude the script not to run on Certain servers

i have a batch script that i have running as a logon script via AD user. i need a way to exclude this command from running on certain servers. is there a way of doing this. Please see below

---Script commands-----
mkdir "%USERPROFILE%\OneDrive - Tenant"
mkdir "%USERPROFILE%\OneDrive - Tenant\Favorites"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Pictures"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Videos"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Music"
subst h: "%USERPROFILE%\OneDrive - Tenant"

exit
-------------
2019-05-08_17-02-32.jpg
Avatar of oBdA
oBdA

This should do the trick:
set exclude=SERVER1 SERVER2 SERVER3
for %%a in (%exclude%) do if /i "%%a"=="%ComputerName%" exit /b

mkdir "%USERPROFILE%\OneDrive - Tenant"
mkdir "%USERPROFILE%\OneDrive - Tenant\Favorites"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Pictures"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Videos"
mkdir "%USERPROFILE%\OneDrive - Tenant\My Music"
subst h: "%USERPROFILE%\OneDrive - Tenant"

exit

Open in new window

To modify ODBA's comment, I prefer this because its quicker to check all terms at one go instead of in a for loop:

SET"_Excluded=SERVER1 SERVER2 SERVER3"

ECHO. %_Excluded% | FIND /I /V "%ComputerName%" 2>&1 >Nul && (
     mkdir "%USERPROFILE%\OneDrive - Tenant"
     mkdir "%USERPROFILE%\OneDrive - Tenant\Favorites"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Pictures"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Videos"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Music"
     subst h: "%USERPROFILE%\OneDrive - Tenant"
)
EXIT /B

Open in new window

Alternatively you could Check AD for membership in a particular Group or OU you can do so such as this:

Exclude by Computer being an a certain OU:
SET "_Excluded_By_OU=OU=Excluded,OU=Computers,OU=Example,DC=Domain,DC=Local"

dsquery COMPUTER -name %computername% | FIND /I /V "%_Excluded_By_OU%" 2>&1 >Nul && (
     mkdir "%USERPROFILE%\OneDrive - Tenant"
     mkdir "%USERPROFILE%\OneDrive - Tenant\Favorites"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Pictures"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Videos"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Music"
     subst h: "%USERPROFILE%\OneDrive - Tenant"
)
EXIT /B

Open in new window

Exclude by Computer being a member of a certain group:





SET "_Excluded_By_Group_Name=Excluded_From_Script"

dsquery group -name  %_Excluded_By_Group_Name% | dsget group -members | FIND /I "%ComputerName%" 2>&1 >Nul && (
     Exit /B
) || (
     mkdir "%USERPROFILE%\OneDrive - Tenant"
     mkdir "%USERPROFILE%\OneDrive - Tenant\Favorites"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Pictures"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Videos"
     mkdir "%USERPROFILE%\OneDrive - Tenant\My Music"
     subst h: "%USERPROFILE%\OneDrive - Tenant"
)
EXIT /B

Open in new window

added examples of excluding Computers by OU and group membership using the same methodology

Note I chose to query the computer and check for the excluded OU path, instead of getting a list of Computers in the OU, because querying one result is a faster proposition than checking one result against many possible answers by using the Scope option, but you can also do that method if you prefer.
Avatar of trcadmin

ASKER

I have tested this using the excluding Computers By OU method. i get error "'dsquery' is not recognized as an internal or external command,
operable program or batch file.". Please see attachments.

i do not have AD install on this server. We are using windows server 2008 R2 and windows server 2019. Please advise. Thanks !!!
2019-05-09_15-00-13.jpg
ASKER CERTIFIED SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ben,

This resolved my issue, Thank you so much for your help!!!
Ben,

This resolved my issue, Thank you so much for your help!!!