Showing posts with label Active Directory. Show all posts
Showing posts with label Active Directory. Show all posts

Friday, 16 October 2009

Update Active Directory from WMI

The Need:
I needed to update the computer description field for servers in AD with the site information where the computer was located. This value was already present for the WIN2k3 servers in WMI.

The Script:
#Update-AD-From-WMI.ps1

#Get the required value from WMI and place in a variable.
function Update-Servers-Site
{
$Serv = gwmi win32_NTDomain -ComputerName $Server | where { $_.caption -match "COMPANYNAME"}
$site = $Serv.ClientSiteName

#Set the description with the value from the variable
Get-qadComputer $server | Set-QADObject -Description $Site | Out-Null
Get-QADComputer $server | Select Name,Description
}

#Loop through a list of server running the function against each one.
$Servers = gc C:\liveServers.txt
Foreach ($server in $Servers) {Update-Servers-Site}

Friday, 15 May 2009

Check-ActiveServers

The Need:
Today I needed to go through Active Directory and produce a list of all servers, then check which were active. I needed to output this to file this time but more often I will just need to output to the screen, so i decided to make use of the switch statement.


The Script:
cls
write-Host "This script will list all the servers in AD and then ping them."
$output = (Read-Host "Press 1 to output to the screen or 2 to output to file")

Switch ($output) {

1 {
Write-Host "Please Wait....Checking AD now!" -fore Yellow
#Get Servers from AD
$ServerList = @(get-qadcomputer -OSName "Windows Server*")
$Servers = $ServerList | foreach {$_.Name}


#Ping Server
function Ping-Server{
$ping = gwmi -q "SELECT * FROM Win32_Pingstatus WHERE Address = '$serv'"
if($ping.statusCode -eq 0) { write-Host "$serv is up" }
else { Write-Host "$serv is not responding" -fore red }
}

foreach ($serv in ($servers)){Ping-Server}
write-host "Done!"
}

2 {
Write-Host "When complete the files will be saved in C:\Temp"
Write-Host "Please Wait....Checking AD now!" -fore Yellow
#Create 3 blank files
New-Item C:\Temp\Servers-in-AD.txt -Type File -force | out-null
New-Item C:\Temp\Servers-Responding.txt -Type File -force | out-null
New-Item C:\Temp\Servers-Not-Responding.txt -Type File -force | out-null

#Get Servers from AD
$ServerList = @(get-qadcomputer -OSName "Windows Server*"); $Servers = $ServerList | foreach {$_.Name}

$Servers >> C:\Temp\Servers-in-AD.txt

#Ping Server
function Ping-Server{
$ping = gwmi -q "SELECT * FROM Win32_Pingstatus WHERE Address = '$serv'"
if($ping.statusCode -eq 0) { Write-output "$serv is up" >>C:\Temp\Servers-Responding.txt}
else { Write-output "$serv is not responding" >>C:\Temp\Servers-Not-Responding.txt}
}
foreach ($serv in ($servers))
{
Ping-Server
}
write-host "Done!"

#Open output files
Invoke-Item C:\Temp\Servers-in-AD.txt
Invoke-Item C:\Temp\Servers-Not-Responding.txt
Invoke-Item C:\Temp\Servers-Responding.txt
}
Default {Write-Host "You didn't select option 1 or 2" -fore Red}
}

Saturday, 28 March 2009

Get-NewUsers

The Need:
I have a need to know what accounts have been created within the past week/month.



The Script:
#Get-NewUsers
#This script will list new accounts and then count them.

$days = (read-host "How many days back shall I go?")
Write-host "This script will display all accounts created in the past $days days. Please wait a few minutes" -fore yellow
$date = get-date
$date = $date.AddDays(-$days)
write-host " "
$TotalAccountsCreated = Get-QADUser -SizeLimit 0 | where {$_.whenCreated -ge $date} | select Name,Company
$TotalAccountsCreated
write-host " "
Write-host "There have been" $totalaccountscreated.count "accounts created in the past $days days" -fore red