Get AD computer account.ps1

下面的脚本实现查询大于90天没有登录的计算机账户,并移动到一个OU中,也可以结合脚本将其disable和删除:

# Gets time stamps for all computers in thedomain that have NOT logged in since after specified date Mod by Tilo2013-08-27

import-module activedirectory 

$domain = "domain.mydom.com" 

$DaysInactive = 90 

$time = (Get-Date).Adddays(-($DaysInactive))

# Get all AD computers with lastLogonTimestamp less than our time

Get-ADComputer –searchBase “ou=computer_OU,dc=devin,dc=com” -Filter {LastLogonTimeStamp -lt $time}-Properties LastLogonTimeStamp | Move-ADObject –TargetPath“OU=test,DC=Devin,DC=com”


下面的几个命令是经常使用的,可以分开使用,包含查询后删除 disable 和 移动等操作

Other Way to resolve the issue:

-----------------------------------------------

# This PowerShell Command will query Active Directory and return thecomputer accounts which  have not loggedfor the past 60 days.  You can easilychange the number of days from 60 to any number of your choosing.  lastLogonDate is a Human Readable conversionof the lastLogonTimeStamp (as far as I am able to discern.  More details about the timestamp can

# be found at technet - http://bit.ly/YpGWXJ  --MWT, 03/12/13

$then = (Get-Date).AddDays(-60)

# The 60 is the number of days from today since the last logon.

Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt$then} | FT

Name,lastLogonDate

# If you would like to Disable these computer accounts,uncomment the following line:

Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt$then} | Set-ADComputer  -Enabled $false

# If you would like to Remove these computer accounts, uncomment the following line:

Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt$then} | Remove-

ADComputer

# If you would like to move these computer accounts to a OU, uncomment the followingline:

Get-ADComputer -Property Name,lastLogonDate -Filter {lastLogonDate -lt$then} | Move-ADObject –TargetPath “OU=test,DC=Devin,DC=com”

## PS. 可以在其中添加search的scope,命令是:

Get-ADComputer –searchBase“ou=computer_OU,dc=devin,dc=com” -Property Name,lastLogonDate -Filter{lastLogonDate -lt $then} | Move-ADObject –TargetPath “OU=test,DC=Devin,DC=com”

Query disabled computer account:

Way 1:

# Only disabled computer accounts

Get-QADComputer -ldapFilter‘(userAccountControl:1.2.840.113556.1.4.803:=2)’

# Only enabled computer accounts

Get-QADComputer -ldapFilter‘(!(userAccountControl:1.2.840.113556.1.4.803:=2))’

Way 2:

dsquery computer –disabled –limit0                                                     

dsquery computer –disabled – limit0 | dsrm –noprompt

另外一种稍微复杂点需要使用get-qad 的方式:

Query the computer and move to one OU:

# set the date to be used as a limit - in this example: 120 daysearlier than the current date ->

$old = (Get-Date).AddDays(-120)

# get the list of computers with the date earlier than this date->

Get-QADComputer -IncludedProperties pwdLastSet -SizeLimit 0 | where {$_.pwdLastSet -le $old }

# get a csv report ->

Get-QADComputer -IncludedProperties pwdLastSet -SizeLimit 0 | where { $_.pwdLastSet-le $old } | select-object Name, ParentContainer, Description, pwdLastSet |export-csv c:\temp\outdated.csv

# move such computers to another OU ->

Get-QADComputer -IncludedProperties pwdLastSet -SizeLimit 0 | where {$_.pwdLastSet -le $old } | Move-QADObject -to my.corp/obsolete

# remove the computer records from AD (since this actually deletesthe records, it would be preferable to run the command with -whatif switchbefore running without it) ->

Get-QADComputer -IncludedProperties pwdLastSet -SizeLimit 0 | where {$_.pwdLastSet -le $old } | Remove-QADObject -to my.corp/obsolete

Comment#1 -> use -SizeLimit0 to remove the default 1000 object retrieval limitation

Comment#2 -> select thecolumns  needed in the report with theSelect-Object cmdlet.

p.s. for the QADComputercommand, please refer to the following article:

http://www.powershelladmin.com/wiki/Quest_activeroles

download the 64-bit or 32-bitversion according to you system, and install it ,after that open the powershellwindows, run Add-PSSnapin Quest.ActiveRoles.ADManagementcommand to import the QADcomputer related module.

仅供参考,如有什么问题,可以发送邮件给,或是留言给我。

谢谢

转载于:https://blog.51cto.com/zhangfang526/1719297

Get AD Object and disable move delete AD account script 查询删除AD账户计算机相关推荐

  1. mysql中delete from in子查询删除失败

    遇到一个情况,想通过表1的id找到表2,删除表2中barcode关联的库存数据,然后一直不能失败,如下: delete from 库存表 where BARCODE in ( select BARCO ...

  2. 如何手工删除AD RMS SCP?

    目前微软的最新的windows server 2012已经推出,在windows server 2012的域环境安装IRM时, 我们发现SCP不能注册 成 功,原因就是之前windows server ...

  3. adlds文件服务器,window_Windows Server 2008:AD LDS应用攻略,本文我们共同了解一下AD LDS的 - phpStudy...

    Windows Server 2008:AD LDS应用攻略 本文我们共同了解一下AD LDS的具体安装以及简单应用操作. 一.安装 AD LDS 服务器角色 1. 单击"开始", ...

  4. 服务器性能计数器驱动没装上,安装和删除 AD DS 的已知问题

    安装 Active Directory 域服务 (AD DS) 之前,请查看下列已知问题: Adprep.exe 问题 Adprep.exe 在 Windows Server 2008 和 Windo ...

  5. AD域建设管理(一)| 安装windows server2019、AD域、AD域证书服务

    AD域建设管理(一)| 安装windows server2019.AD域.AD域证书服务 1.环境介绍及前言 2.安装配置win server 2019虚拟机 3.安装AD域服务.AD证书服务 3.1 ...

  6. AD20怎么铺铜?AD怎么铺铜?Altium designer怎么铺铜?AD明明已经铺铜怎么只有框?AD怎么铺不了铜皮?

    AD20怎么铺铜?AD怎么铺铜?Altium designer怎么铺铜?AD明明已经铺铜怎么只有框? ..可能有人刚学AD会有些疑问,如下图,为什么我已经铺铜可是只有一个红色的边框,其实是你还有些设置 ...

  7. 关于js中delete 操作符的秘密(如何删除对象的属性,清空对象)

    开发十年,就只剩下这套架构体系了! >>>    概念 再mdn中detele的概念如下. delete 操作符用于删除对象的某个属性:如果没有指向这个属性的引用,那它最终会被释放. ...

  8. SQL优化之一则MySQL中的DELETE、UPDATE 子查询的锁机制失效案例

    关注"数据和云",精彩不容错过 前言 开发与维护人员避免不了与 in/exists.not in/not exists 子查询打交道,接触过的人可能知道 in/exists.not ...

  9. Mysql定义DELETE操作触发器,将删除数据存入历史表

    Mysql定义DELETE操作触发器,将删除数据存入历史表 SQL如下: // An highlighted blockDELIMITER $$ CREATE TRIGGER <触发器名称> ...

最新文章

  1. 集群理论讲解(续三)
  2. app配置智能硬件的解决方案
  3. 旋转矩阵求旋转角度_(加餐)欧拉角及矩阵旋转
  4. iOS手势操作简介(六)
  5. packetbeat oracle,packetbeat 无法启动
  6. .net 数字转汉字_收藏!小学生汉字拼音学习工具。
  7. Windows 7 任务栏开发 之 进度条(Progress Bar)
  8. 创建IPSEC连接安全
  9. gethostbyname与sockaddr_in的完美组合
  10. Android -- 闹钟服务的使用(单次闹钟)
  11. c语言课程设计参考,c语言课程设计参考
  12. 支付宝支付出现 openssl_sign(): supplied key param cannot be coerced into a private key
  13. 【测开方法论】测开平台pk心得-抉择
  14. TCP四次挥手的2MSL详解
  15. 亚马逊、虾皮、来赞达、速卖通、ebay等跨境电商平台如何搭建一个稳定的自养号补单系统?
  16. MCS-51单片机内部结构——CPU结构 单片机原理学习笔记(二)
  17. 6.Python之函数
  18. Bmob用户功能详解(三)
  19. vue 精简教程(四) vuerouter 路由
  20. 未来最赚钱的四个行业,看看你的行业在其中吗?

热门文章

  1. CentOS7 搭建Ambari-Server,安装Hadoop集群(一)
  2. 你必须『收藏』的Github技巧
  3. AutoCompleteTextView 和 TextWatcher 详解
  4. linux下修改当前目录下图像文件并删除
  5. 对刚接触oracle的人比较有用的一些工具 zt
  6. 硬盘全新安装windows 7
  7. AD RMS 问题解决 事件ID:139
  8. 二叉树最近公共祖先 LCA
  9. 如何监控 Elasticsearch 集群状态?
  10. RabbitMQ(六) Routing路由模式