Solved

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

Posted on 2010-09-22
2
810 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 71

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

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 …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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