Skip to content

Instantly share code, notes, and snippets.

@danjpadgett
Created June 18, 2014 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjpadgett/8cefb1a7310217338af6 to your computer and use it in GitHub Desktop.
Save danjpadgett/8cefb1a7310217338af6 to your computer and use it in GitHub Desktop.
#############################################################################################
# Add-Ins
#############################################################################################
Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
#############################################################################################
# Constants
# You may need to specify the full path to CSV, format for CSV is : Current-Computer-Name,New-Computer-Name
#############################################################################################
$inputfile="Names.csv"
$logfile="Renamer.log"
#############################################################################################
# Functions
#############################################################################################
# Function to check for logon and return boolean
function Active-Logon(
[string] $strComputer)
{
$Computer =GWMI -Comp $strComputer -CL Win32_ComputerSystem
$strusername = $Computer.UserName
if ($strusername -ne $NULL)
{
#User is logged in
write-host $true
return $TRUE
}
else
{
#No users logged in
write-host $false
return $FALSE
}
}
# POWERSHELL Ping tool to check if a computer/ip/hostname is online
function isComputerOnline( $comp ){
trap [Exception] {
return $false
}
if ( $(new-object system.net.networkinformation.ping).send( $comp ).status -eq "Success" )
{
return $true
}else{
return $false
}
}
# Remote Rename Computer by WMI
function COMPUTER_RENAME([string] $oldComputerName, [string] $newComputerName){
# should implement further checks for the computername. If computername uses illegal characters and if computername is longer than 15 characters
# More information here: http://labmice.techtarget.com/articles/computernaming.htm
$oldComputerName = $oldComputerName.replace(" ","");
$newComputerName = $newComputerName.replace(" ","");
# // Check if the computer is online
if ( isComputerOnline $oldComputerName ){
# // Check the active directory if a computer with your provided newcomputername already exists.
# // If you want to use the script without active directory just comment (use: #) this line and the corresponding "else" loop
if ( ! (Get-QADObject -ldapfilter "(CN=$newComputerName)") ){
# // Handle every upcoming error as a stop failure, so you can trap it
$ErrorActionPreference = "Stop"
try{
# // Get the WMI Object of the remote computer
$ComputerWMIObject = Get-WmiObject Win32_ComputerSystem -ComputerName "$oldComputerName" -Authentication 6
if ( $ComputerWMIObject ){
# // Rename the Computer Object with your or some admin credentials (Yes, Password is the second parameter and username the third)
$result = $ComputerWMIObject.Rename("$newComputerName","PASSWORD,"DOMAIN\USER")
# // Switch Case for the returnvalue of computer renaming function
switch($result.ReturnValue)
{
0 {
if (Active-Logon $oldComputerName)
{
# // A user is logged on, Delay the computer reboot if renaming was successful
$strResult="Computer " + $oldComputerName + " was renamed (" + $newComputerName + ") but was not rebooted due to active user session"
write-host $strresult
write-log $strresult
}
else
{
# // no users logged on, Reboot the computer instantly if renaming was successful
Restart-Computer -Force -ComputerName $oldComputerName
$strResult="Computer " + $oldComputerName + " was renamed (" + $newComputerName + ") and restarted"
write-host $strresult
write-log $strresult
}
}
5 { $strResult="Computer was not renamed. Please check if you have admin permissions (ReturnCode 5)"
write-host $strresult
write-log $strresult; exit; }
default { $strResult="ReturnCode $($result.ReturnValue)"
write-host $strresult
write-log $strresult; exit; }
}
}else{
$strResult="Couldn't create WMI Object on $oldComputerName"
write-host $strResult
write-log $strResult
}
}catch{
$strResult=$_
write-host ($oldComputerName+":"+$strResult)
write-log ($oldComputerName+":"+$strResult)
}
}else{
$strResult="There is already a computerobject with the name $($newComputerName)"
write-host $strResult
write-log $strResult
}
}else{
$strResult="Computer $($oldComputerName) is offline !"
write-host $strResult
write-log $strResult
}
}
# function for easier logging
Function Write-Log(
[string] $strLogMessage)
{
$strLogMessage = (get-date -uformat "%d/%m/%y %T") + " " + $strLogMessage
$strLogMessage | Out-File -FilePath $logfile -Append
}
#############################################################################################
# Start Code
#############################################################################################
if(test-path($inputfile))
{
$csvfile=import-csv $inputfile
foreach ($computer in $csvfile)
{
COMPUTER_RENAME $computer."Current-Computer-Name" $computer."New-Computer-Name"
}
}
else
{
$strResult="CSV File Missing - Ensure 'Names.csv' is in same path as script!"
write-host $strResult
write-log $strResult; exit; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment