Solved

Can I use "Get-QADComputer -Name $computers" and pull from a csv file for the var $computers

Posted on 2010-09-22
2
808 Views
Last Modified: 2012-05-10
Im looking to modify a list of Servers that are in AD.  Like to be able to use a csv or txt file to pull the name from and then have it modify them based on what I need done.  I can do this using a var for one machine and it will prompt me, but want to make sure I can do this for multible machines as well.   I am using the Quest Software "ActiveRoles Management Shell for Active Directory x64".

I have attached a txt file of what I have so far.  I think Imay need to use foreach but not sure where to put it.  I am still a NOOB working with powershell so be nice to me  :-).
decomm-ps-script.txt
0
Comment
Question by:gzugg
2 Comments
 

Author Comment

by:gzugg
ID: 33736023
Just adding a note, when I runt he script without the part for the csv file it works great, so I know the sytax is correct fro that part.  This will give you an option to run for one machine or use a csv file for multible machines to modify.
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 33741751

Something like this...

You'll need PowerShell 2 if you don't have it already.

Chris
# NAME:decommAD.ps1
#
# AUTHOR: George Zugg
# DATE  : 9/22/2010
# 
# COMMENT: This is for the ### domain only.  This script moves a 
# computer object to the retired server OU, changes the server
# object description to say "Remove After (Date)". The date is
# filled in automatically based on todays date plus 30 days out.  
# The final step is to disabled the computer account.
# 
#
# Ver 1.2.7
#
#
#################################################################
## Create necessary variables to use throughout the script for logging

<#
  .Synopsis
    Moves the specified computers to the specified OU
  .Description
    Moves the specified computers to the specified OU
  .Parameter Computer
    A single computer, or list of computers
  .Parameter CSVFile
    Read computer names from a CSV File. File must exist.
  .Parameter ColumnName
    By default  the script will look for a Name column in the CSV file.
  .Parameter Credential
    Credentials used to perform this action
  .Parameter LogFolder
    A folder for log files. Files will be appended with todays date. By default the Log Folder path is C:\scripts\decommAD\logs\.
#>

[CmdLetBinding(DefaultParameterSetName = "ManualList")]
Param(
  [Parameter(ParameterSetName = "ManualList")]
  [String[]]$Computer,
  [Parameter(ParameterSetName = "CSVList")]
  [ValidateScript( { Test-Path $_ } )]
  [String]$CSVFile,
  [Parameter(ParameterSetName = "CSVList")]
  [String]$ColumnName = "Name",
  [Parameter(ParameterSetName = "")]
  [Management.Automation.PsCredential]$Credential,
  [Parameter(ParameterSetName = "")]
  [String]$LogFolder = "C:\Scripts\decommAD\Logs\"
)


$DateTime = Get-Date -Format "dd-MM-yyyy"
$ActivityLog = $logFolder + "decommAD_$DateTime.log"

If (!(Test-Path $logFolder)) { New-Item -Path $logFolder -Type Directory -Force }

$Date = (Get-Date).AddDays(30).ToString("d")

# If explicit credentials are passed
If ($Credential) { Connect-QADService -Credential $Credential }

If ($PsCmdLet.ParameterSetName = "CSVList") {
  $Computer = Import-Csv $CSVFile | ForEach-Object { $_.$ColumnName }
}

Get-Date | Add-Content -Path $ActivityLog 

$Computer | ForEach-Object {
  "Moving Server name to RetiredServer OU" >> $ActivityLog

  $ComputerAccount = Get-QADComputer $_

  Move-QADObject $ComputerAccount.DN -NewParentContainer 'domain.com/Servers/BackOffice/RetiredServers' >> $ActivityLog

  "Adding Description Remove after ($Date)" >> Add-Content -Path $ActivityLog
  Set-QADObject $ComputerAccount.DN -Description "Remove After $Date" >> $ActivityLog

}

Disconnect-QADService

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question