Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

21 Experts available now in Live!

Get 1:1 Help Now