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

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.

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

谢谢

时间: 2024-10-10 01:53:59

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

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

遇到一个情况,想通过表1的id找到表2,删除表2中barcode关联的库存数据,然后一直不能失败,如下: delete from 库存表 where BARCODE in( select BARCODE from 表1 where fmoveid= (select id from 表2 where PCID='SMX2014082604494930') ); 解决方式,加别名 delete from 库存表 where BARCODE in( select aa.BARCODE from(   

排错Exchange Server自动查询的AD服务器

在实际生成环境中经常会遇到某台DC出现问题(存在多台DC服务器),而此DC还是GC全局编录服务器.我们知道Exchange会自动随机查找可用的GC服务器,不管此GC是否可用.当查找到不可用的GC时就会出现报错.然后十五分钟后Exchange再次查找其他GC.为了避免Exchange查找不可用的GC服务器,我们可以通过设置Exchange来排错有问题的GC服务器.具体设置如下: get-exchangeserver | set-exchangeserver -StaticExcludedDomai

delete archivelog all 无法彻底删除归档日志?

最近在因归档日志暴增,使用delete archivelog all貌似无法清除所有的归档日志,到底是什么原因呢? [python] view plain copy print? 1.演示环境 SQL> select * from v$version where rownum<2; BANNER ---------------------------------------------------------------- Oracle Database 10g Release 10.2.0.

450. Delete Node in a BST 删除bst中的一个节点

[抄题]: Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If

Move Find into Model 将查询方法从控制器上移至模型

class TaskController < ApplicationController def index @tasks = Task.find_all_by_complete(:false, :order => "created_at DESC") end end 这段代码的意思是查询所有未完成的任务并按照创建的时间先后排序.如果控制器中有好多个地方要用到,那么我们可以将这个方法抽出来放到模型中,用到的时候 @tasks = Task.find_incomplete来调

list&lt;String,object&gt;的元素判空(用于判断查询数据库返回值)

一般人可能会使用list.size或者list==null来做判断.当没有返回值时返回的类型为"[ ]"它并不是空也没有元素,所以使用==null以及if(list.size()>0){//业务逻辑}是不成功的. 所以这需要去判断元素的存在与否,应使用list.isEntity()函数来做判断.if(!list.isEntity()){//返回值不为0的业务逻辑}. 原文地址:https://www.cnblogs.com/daqq/p/9506098.html

Slmgr.vbs参数使用方法[转自windows10操作系统]

Slmgr.vbs参数使用方法: --------------------------- Windows Script Host --------------------------- Windows 软件授权管理工具 用法: slmgr.vbs [MachineName [User Password]] [<Option>] MachineName: 远程计算机的名称(默认为本地计算机) User: 远程计算机上具有所需特权的帐户 Password: 前面帐号的密码 全局选项: /ipk &

django之模型层(待补充)

模型层 1. ORM查询 所有代码都是在test.py文件中运行的 注意:我如果想在test.py文件中测试相关代码,那么必须要进行配置,不然会报以下的错误 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable D

MySQL 5.6对已有Mysql单实例的机器,再添加mysql数据库,实现单机多实例

一.需求: 对已有Mysql单实例的机器,再添加两个mysql数据库,实现单机多实例. 一个绑定在端口3306,另外两个绑定在端口3307,3308: 数据分别存放在/data/mysqldata./data/mysqldata2./data/mysqldata3 三个实例均采用InnoDB作为默认的存储引擎,字符编码采用UTF-8: 三个实例均采用相同的性能优化配置参数: MySQL的源码安装请看我的另一篇博客http://yylinux.blog.51cto.com/8831641/1677