Solved

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

Posted on 2010-09-22
2
805 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now