Running a command on every machine in your domain from the command line

简介: http://pauldotcom.com/2010/02/running-a-command-on-every-mac.

http://pauldotcom.com/2010/02/running-a-command-on-every-mac.html

 

After listening to Larry's excellent technical segment on dumping the event logs from a large list of computers, I decided to try it out on my own. If you missed the technical segment, you can find the notes here.    To do my own testing I needed to start with a large list of computers.   For my list, I want to have the names of every computer in the domain.   So I turned to "dsquery computer " to get a list of all computers. 

C:/WINDOWS> dsquery computer 

"CN=CONTROLER1,OU=Domain Controllers,DC=subdomain,DC=domain,DC=com"

 

BLA BLA BLA... Truncated

 

"CN=WORKSTATION1,OU=ORGUNIT1,OU=OrgUnit2,OU=OrgUnit3,DC=Subdomain,DC=Domain,DC=com"

 

BLA BLA BLA... Truncated again


The length of the results changes because of the variable number of subdomains, but fortunetly for us the workstation name is always the first part of the string.  It is always between the CN= and the first comma. We can strip out the workstation name with the "DELIMS" and "TOKENS" option of the FOR loop.  Also, by default DSQUERY will only return the first 100 results. This can be changed using the "-LIMIT" option. Setting the LIMIT to 0 returns all result. For now lets check our output looking at only two entries.

 

C:/WINDOWS>for /F "delims=, tokens=1" %i in ('dsquery computer -limit 2') do echo %i

 

C:/WINDOWS>echo "CN=WORKSTATION1

"CN=WORKSTATION1

 

C:/WINDOWS>echo "CN=WORKSTATION2

"CN=WORKSTATION2

 


We are almost there. I need to strip the first 4 characters of the line. For this I stole a page or two from Ed Skoudis' play book. We can strip the first four characters with the SET command using the expression variable = %variable:~4%. But, since we are in a FOR loop we have to turn on delayed variable expansion and use ! instead of %.

 

 

C:/WINDOWS>cmd.exe /v:on /c "for /F "delims=, tokens=1" %i in 

('dsquery computer -limit 2') do set name=%i  & set name=!name:~4! & echo !name!"

 

Dsquery has reached the specified limit on number of results to display; use a different value for the -limit option to display more results.

C:/WINDOWS>set name="CN=WORKSTATION1    & set name=!name:~4!   & echo !name!

WORKSTATION1

C:/WINDOWS>set name="CN=WORKSTATION2    & set name=!name:~4!   & echo !name!

WORKSTATION2


Now we have all the computer names in the domain being printed one at a time. I can dump all the names of the machines in the domain to file to feed Larry's event dumper.

 

C:/WINDOWS>cmd.exe /v:on /c "for /F "delims=, tokens=1" %i in ('dsquery computer -limit 0') do set name=%i & set name=!name:~4! & echo !name! >> c:/temp/machines.txt"

 

Better yet, we could combine it with a WMIC command and other commands to do various useful tasks on our machines.  For example, if I want to query every machine in my network for an instance of a process that starts with CMD I could run the following:  (remember that % is a wild card in wmic)

 

C:/WINDOWS>cmd.exe /v:on /c "for /F "delims=, tokens=1" %i in ('dsquery computer -limit 2') do set name=%i  & set name=!name:~4! & wmic /node:!name! process where "name like 'cmd%'" list brief"

 

Dsquery has reached the specified limit on number of results to display; use a different value for the -limit option to display more results.

 

C:/WINDOWS>set name="CN=WORKSTATION1    & set name=!name:~4!   & wmic /node:!name! process where "name like 'cmd%'" list brief

HandleCount  Name     Priority  ProcessId  ThreadCount  WorkingSetSize

33           cmd.exe  8         2308       1            1654784

 

 

C:/WINDOWS>set name="CN=WORKSTATION2    & set name=!name:~4!   & wmic /node:!name! process where "name like 'cmd%'" list brief

HandleCount  Name     Priority  ProcessId  ThreadCount  WorkingSetSize

33           cmd.exe  8         2368       1            1626112


So there you go.  Now using this basic syntax...

 

cmd.exe /v:on /c "for /F "delims=, tokens=1" %i in ('dsquery computer -limit 0') do set name=%i  & set name=!name:~4! & Any command here based on !name!

 

... you can run any command you want on every machine in your domain.   

 

Join me for SANS 504 June 21-26

 

 

目录
相关文章
|
机器学习/深度学习 人工智能 自然语言处理
当语言遇见智慧火花:GPT家族历代模型大起底,带你见证从平凡到卓越的AI进化奇迹!
【10月更文挑战第6天】随着自然语言处理技术的进步,GPT系列模型(Generative Pre-trained Transformers)成为该领域的明星。从GPT-1的开创性工作,到GPT-2在规模与性能上的突破,再到拥有1750亿参数的GPT-3及其无需微调即可执行多种NLP任务的能力,以及社区驱动的GPT-NeoX,这些模型不断进化。虽然它们展现出强大的语言理解和生成能力,但也存在如生成错误信息或偏见等问题。本文将对比分析各代GPT模型的特点,并通过示例代码展示其部分功能。
406 2
|
人工智能 算法 前端开发
阿里通义灵码的最佳实践
上周首次尝试了阿里巴巴的通义灵码AI插件,体验良好。该插件体积适中,约5.8M,适合项目开发使用。其@workspace和@terminal功能强大,能快速帮助开发者熟悉新项目结构,提供智能代码导航、搜索、优化及错误提示等服务,显著提升开发效率与代码质量。实践证明,通义灵码在加速项目理解和新需求实现方面表现出色,是开发者的得力助手。
496 1
阿里通义灵码的最佳实践
|
编译器 C++ 计算机视觉
include包含文件查找的顺序 .
从microsoft网站上找到关于#include Directive (C/C++)的相关问题解释如下: The #include directive tells the preprocessor to treat the contents of a specified file as if t...
1500 0
|
监控 人机交互 芯片
西门子S7-200 SMART的功能和特点有哪些?STEP7-Micro/WIN SMART编程软件简介
西门子S7-200 SMART是西门子公司针对中国小型自动化市场客户需求设计研发的一款高性价比小型PLC产品。S7-200 SMART CPU将微处理器、集成电源、输入输出电路组合到一个设计紧凑的外壳中,已形成功能强大的小型plc。面板包含电源接线端子、直流24V电源输出端子、数字量输入输出接线端子、CPU状态指示灯、IO状态指示灯、存储卡插槽、以太网接口、RS485接口等。
西门子S7-200 SMART的功能和特点有哪些?STEP7-Micro/WIN SMART编程软件简介
阿里云账号企业实名认证图文教程
如果是阿里云账号是实际使用主体为企业,那么选择企业认证,企业认证支持的方法比较多,可以选择企业支付宝授权认证、企业法人支付宝授权认证、企业钉钉授权认证、企业法人扫脸认证、企业银行打款方式认证或1688企业用户一键同步。
5821 0
阿里云账号企业实名认证图文教程
|
机器学习/深度学习 人工智能 缓存
|
编解码 光互联 虚拟化
虚幻引擎 5 来了!不止 Lumen、Nanite 新技术,性能及 UI 均迎来大升级
虚幻引擎 5 来了!不止 Lumen、Nanite 新技术,性能及 UI 均迎来大升级
563 0
虚幻引擎 5 来了!不止 Lumen、Nanite 新技术,性能及 UI 均迎来大升级
|
云安全 人工智能 Cloud Native
关于阿里云aca和acp哪个好?阿里云认证证书有含金量吗?
由于网络的变迁,为网络行业的兴起提供了很好的发展。在其中出现了更多的新职位,从而为许多人创造了更大的就业机会,与此同时也增加了就业竞争。 那么接下来跟随着认证大使的小编一起了解下关于阿里云aca和acp哪个好?
809 0
关于阿里云aca和acp哪个好?阿里云认证证书有含金量吗?
|
存储 物联网 Shell
SQLite 删除语句和匹配条件 | 学习笔记
快速学习 SQLite 删除语句和匹配条件