Wednesday, March 26, 2014

Everyday Powershell - Part 18 - Set Address Details in Active Directory

Here's a request from one of our readers. He's tidying up a negelected AD he inherited and needed a way to apply consistent addresses to his AD User Objects.

Powershell is great at this stuff! We've decided to use group membership as a way to decide who should get what data, but this could be easily applied via OU.
<# For this to work setup a CSV in C:\Scripts call it Groups.csv
And fill it with data like this; 
SecurityGroup,Address,City,Zip,State,Country,Telephone
Group1,1 Some St,SomeTown,1122,SOMESTATE,AU,615551234
Group2,2 Some Other St,SomeOtherTown,2211,SOMESTATE,AU,615554321 #>

$csvdata =import-csv C:\Scripts\Groups.csv
foreach($row in $csvdata)
{
    $users= Get-ADGroupMember $row.securitygroup
    foreach($user in $users)
    {
        Set-ADUser $user -StreetAddress $row.Address -city $row.city -State $row.State -Postalcode $row.Zip -Country $row.Country -OfficePhone $row.Telephone
    }
}
Just a simple script with two loops, first one iterates through the rows in the CSV the second group steps through each user in the security group identified in the initial loop.

Nothing fancy but it fixed our readers problem. Such is the power of the shell!

Wednesday, March 19, 2014

Everyday Powershell - Part 17 - Using new-mailboxexportrequest with NON-US dates

Here's a crazy making problem!

The new-mailboxexportrequest commandlet will only accept dates in US formats. Roland Paix has made this post about the issue. The conclusion is switch your culture to EN-US before loading powershell. Sooo frustrating! But hey, something has to be the default datetime and Microsoft is an American company. No point getting emotional, let's just fix it.

Here's a neat trick you can use to set the Culture of the "Current Thread" you are running.

[System.Reflection.Assembly]::LoadWithPartialName("System.Threading")
[System.Reflection.Assembly]::LoadWithPartialName("System.Globalization")
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-us")

Add-PSSnapin *exchange*

$start = (get-date).adddays(-28)
$end = (get-date).adddays(-14)

$filter = "(Received -gt '"+$start+"') -and (Received -lt '"+$end+"')"
new-Mailboxexportrequest -mailbox SOMEMAILBOX -ContentFilter $filter -filepath C:\SOME.pst

This should trick new-mailboxexportrequest into believing that your dates are in the "correct" format!

Wednesday, March 12, 2014

Everyday Powershell - Part 16 - Monitoring Disk Performance in Powershell

Welcome back to another Everyday Powershell post. It's not powershell we POST every day, it's powershell you could USE everyday.

Today we look at get-counter which you should have a fiddle with if you get the chance. It does a lot more than what we've used it for here. But this may help you get the gist of running performance counters in powershell.

$disks = Get-WmiObject win32_logicaldisk | Where-Object {$_.DriveType -eq 3| select DeviceID
$report = @()
$i=1
foreach ($disk in $disks)
{
    Write-Progress -Activity "Mesuring disk performance" -Status $disk.DeviceID -PercentComplete (($i/$disks.Count)*100)
    $temp = "" | select Disk, Read, Write
    $temp.disk = $disk.deviceid
    $counter = "\LogicalDisk(" + ($disk.deviceid) + ")\Avg. Disk sec/Read"
    $temp.read = ((Get-Counter -Counter $counter).CounterSamples.cookedvalue)*1000
    $counter = "\LogicalDisk(" + ($disk.deviceid) + ")\Avg. Disk sec/Write"
    $temp.write = ((Get-Counter -Counter $counter).CounterSamples.cookedvalue)*1000
    $report += $temp
    $i++
}
$report

We've included the now ubiquitous write-progess to give a useful progress indicator. As well as the old $temp="" | select which sets up a hash table for us to dump data into.