Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Thursday, March 11, 2010

Exchange Server 2010 - Create New Mailbox Database

.......... wassssupppppppppppppppppppp..??????
Well, am back, Early thursday morning and I aint sleeping, lets learn something, today i will show you how to create a new mailbox database using powershell.

It is a very simple one-liner again, below is the actual command:
new-mailboxdatabase -name 'MBX DB2' -server 'w2k8r2' -edbfilepath 'C:\MailboxDB\DB2\MBX_DB2.edb' -logFolderPath 'C:\MailboxDB\DB2_Logs'

Press Enter, vola, you have your new mailbox database created :)
When you create this new DB, you can note that it is not mounted, you have to mount it urself, well again you can use this below command to mount a database:
mount-database -identity 'mbx db1'

YOu can now see in EMC, that the new DB is created:
And below is the screenshot of the mount command, pretty straight forward though.

Exchange Server 2010 - Move Database

Hello Everyone,

Today, we will see how to move database files from one location to another location.
there is a one-liner for this, the cmd-let is move-databasepath.

Below is the complete command.
move-DatabasePath -Identity 'MBX DB1' -EdbFilePath 'C:\MailboxDB\DB1\MBX_DB1.edb' -LogFolderPath 'C:\MailboxDB\DB1_Logs\'

Note, there are three parameters, 
         1.-identity -> this is where you give the name of the db
         2.-edbfilepath -> give the path of the new edb file along with the name of the new edb file.
         3.-logfolderpath -> as the name, you just need to give the folder path.

Did you notice something odd ?? , yes, in Exchange 2010, logs are individual for each DB, one DB has its own set of log files, not like in Exchange 2003, where you had log files shared among Storage Groups, this gives the admin more flexibility on working on a individual store and doesnt impact any other store at all.

You can see the screen shot below, once you key in the command, it will ask whether you want to proceed, becos moving database path may dismount the DB and users will not be able to access their mailbox during this period.
Confirm by typing Y and press enter and wait for the command line progress (see screenshot) , to complete.
Then you can see that the files are moved to the new location.
PS: note that in Exchange 2010, log file sise is reduced to 1MB from the typical 5 MB in Exchange 2003 :).
Now in the actual EMC (Exchange Management Console)  not the EMC company :) ... well you can see that the new paths are getting reflected.
Note that Database are now a ORG wide thing and it is moved under the ORG level, back in Exchange 2003, this was under Servers container.
Pretty sleek huh? , well this can be easily done using GUI as well, but who wants GUI ? :) ...

Thursday, March 4, 2010

PowerShell - Disk Space HTML Email Report


Hi Guys and fellow powershell scripters :),
Well, i have been busy for the past few days and now time to write my next blog.
As you already know from the title, yes am going to write a powershell script which generate a html report of the disk spaces details on a given list of servers and then email that report to the administrator or any email address.

What is needed?
A file named sl.txt on the directory where you have this script copied, this file will have the list of computers, one per line.

Before i start to go into details, you can simply get the diskspace info for any computer by the following powershell cmd-let

Get-WmiObject win32_logicaldisk  | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt

Well many in the world dont like some black&white text report, they just dont like it, well lets create a HTML report of this diskspace info and then email it.

So, lets jump in and get his badboy started.
My Powershell script contains 5 important functions, which do their own job.
Below are the details of those 5 functions:
1.writeHtmlHeader: This function write the HTML tags to create the html file and then kick start the process.
   This function also write some CSS styling info, so dont worry about any of this, i have already taken the load of going through them and creating it, so this is just for your info.
Below is the code for this function:
2.writeHtmlFooter - This function simply updates the file in the end to add the closing html tags , the same old </body> and the </html> tags.
3. sendEmail - This function sends email to the given address, it takes in from,to,subject,smtphost,htmlfile as the input and then composes the message and then emails the file.

4.writeTableHeader - This function simply writes the html tags for a table, my idea was for each and every computer to have a seperate table header, for eg., drive letter C:\ and the the lable of the drive "OS", and then total space ,free space etc., these info will be the table header for each computer. 


5. writeDiskInfo -  This is the actual function which write the disk details to the html file, it also calculates the free space percentage etc.,
On top of the script, i have setup some variable like the sl.txt file which will contain the server names, you can change it to whatever name of the file you want or you take in parameter from the user and then process it, it is left to you.

I have two more variables named lke $warning and $critical, i have set them to 85 and 70, these values will be compared against the free disk space percentage and then if a particular drive is above the critical or warning threshold, that particular drive will marked in RED and Yellow respectively.
You will get an idea of what i am talking about when you see the screen shot below:
Here is the complete Script:

Friday, February 26, 2010

Exchange Server 2010 - OWA Themes


Today, lets see how the OWA has changed from the age old E2K3 days.

My test workstation is running Windows Server 2008 R2 + Exchange Server 2010.

In previous blogs, i have setup few users using power shell, lets check one of those user's mailbox through OWA.

Below is the screen shot of the login screen:

 




After you key in the credentials, it will show up with this screen below:
Here you can select the time sone and click OK.

Then you will have the OWA open up your actual mailbox.

 


There are many key changes in the interface.
Notice there is a search field about the mail items, you can easily search for items from here, it is really fast.

When you click the options link on ur top right corner, you will get displayed with many options,
like, you can change your password, create rules, set auto replies, set oof etc.,

You can create groups, like distribution lists.
You can setup, delivery reports.
You can also manage your Junk-Email settings from here.

There is no options for the user to change themes :( . you have get stuck with this yellow theme ,  i like the theme anyway, but for those don't like yellow ?? :)
Well, With Exchange 2010, users will loose the capability to change the themes :( .
But , the admins can change the themes,  but it will reflect the same to all users.
Lets test a couple of them and see how it will look.
First of all, there are 4 themes, including the Yellow one, which is shown above. that is theme no: 4.
Lets, take a look at the first 3 themes, well you have to know how to change them, well the command below is how you set it..
Well, as you can see, you can change the number at the end of the command to change to different themes.
1. City Theme - Which is mostly black, you can see the screen shot for urself.
2. X-Box 360 Theme - Well i like this as well, screenshot below:
3. Zune Theme - This one is neat as well, check it out:
Well the fourth one is the one i showed above, the default one.

Wednesday, February 24, 2010

Powershell - Provisioning Users

Hello Everyone,

Time for my second blog, i was working on and off for the last few days on the powershell script, using which you can provision new users.

Script?
For what? - Create new mailbox enabled AD users
Input Method? - a CSV file

First of all thanks to "Don Jones" for writing such an eloborate blog on technet on how to setup this provisioning, you can read Don's blog from the below link.


http://technet.microsoft.com/en-us/magazine/2009.03.windowspowershell.aspx?pr=blog

For those newbies like me to powershell, POWERGUI is a cool little editor for writing powershell scripts, you can download it from the below link.

http://www.powergui.org/downloads.jspa

Now lets get to the tasks ahead.

First lets take a look at the CSV file, see the screenshot below:

Note the Header of each field, those don't look like standard AD attribute name, well we will tackle them when we actually write the powershell script.

Script will consist of 2 blocks , the work of the first block is just to read the CSV file and for each and every line of the CSV will be converted into an HASHTABLE and this hashtable will be fed in to the second script block.

Below screenshot of the complete code:



Lets take a look the script blocks closely.

Function ReadCSV {
Param([string]$fileName)

$users = Import-Csv $fileName

foreach ($user in $users){

$ht = @{ 'givenName'=$user.fn
'sn'=$user.ln

'displayName'=$user.dispname
'alias'=$user.alias

'samAccountName'=$user.alias

'userPrincipalName' = $user.upn

'database' = $user.Database
'
organizationalUnit' = $user.ou

'name' = ($user.fn + $user.sn)

}

Write-Output $ht
}
}


I guess, you all came to know the purpose of the function from the function name itself, well you are right, this function takes a csv file path as the input and then imports that csv file.
Using the command
Import-Csv $fileName.

Output of this command is stored on to the $users variable, in the next line you can see that using a for each loop, each line of the csv is fed into the $ht hashtable

For example, as you have in the screenshot of the csv file, displayNAme value of the hastable will be picked from the CSV file and stored onto the $ht hashtable, in this scenario it will be "Bhat, Shridhar"

Once all the fields mentioned here are loaded to the the $ht hashtable, then using the write-output command it is passed on to the next pipeline

Just by the name you may wonder, that the purpose of "Write-Output" is simply to write out some output, but that is not the case, it will feed each hashtable entry to the proceeding pipeline, in our case the second script block which will actually use this info and create the mailboxes.

You can read about "Write-Output" here.

Lets jump to the next script block.

Function CreateUser{
Param($userInfo)
$secureString = ConvertTo-SecureString "K1ll31ll$1234" -AsPlainText –Force New-Mailbox -Name $userInfo['name' ]`
-Alias $userInfo['alias'] -UserPrincipalName $userInfo['userPrincipalName'] `
-SamAccountName $userInfo['alias'] -Database $userInfo['Database']`
-FirstName $userInfo['givenName'] -LastName $userInfo['sn'] `
-DisplayName $userInfo['DISPLAYNAME'] `
-Password $secureString -ResetPasswordOnNextLogon $true
}

Notice this function takes in a parameter $userinfo , meaning the hashtable from the previous script block, you will understand better, when we look at the last step of this script, for now, it takes in a parameter.

Then, all it does is that , it uses the "New-Mailbox" cmdlet and then feeds in the parameter which it recieved from the hastable and then uses them as a parameter for the "New-Mailbox" cmdlet.

Notice this line -
$secureString = ConvertTo-SecureString "K1ll31ll$1234" -AsPlainText –Force
This for me not wanting to enter a password for each and every user, so i am forcing the script to use this plain text as a password.

Function CreateMailbox{ PROCESS { CreateUser $_ } }

This is just a blanket function from which we call the CreateUser function, this is basically to modularise the script.
Lets look at the final bit, which is the actual flow of the script.

ReadCSV c:\Scripts\UserList_10.csv | CreateMailbox

Notice, first we are calling the ReadCSV function and give it a csv file path as a parameter and then it is piped to the CreateMailbox function.
So when the first line of the csv file is read, it take all the user details into a hashtable and then feeds that info to the CreateMailbox function, which in turn , using the new-mailbox cmdlet creates the mailbox enabled AD user.

Output of the Script:



Actually i used mutiple names in the CSV files, which i didnt show in the first screenshot.

Below is the screenshot from "Active Directory Users and Computers" showing the user objects.
I user Windows Server 2008 R2 + Exchange 2010"



This one is from the Exchange 2010 Management Console.




Time to sleep, it is 6AM, i better sleep now :).

~F0x


Friday, February 19, 2010

PowerShell - Searching Eventlog

Back in Windows 2003, you need to open up event viewer and then setup a filter and the search for a specific event.

But now, welcome to the world of powershell, you can get the same thing done in just a command.

Let me walk you through the command.

I know, this very novice and guys who know powershell already might think, what, this is just a piece of cake, well, i am going to mention it anyway.

Get-EventLog -LogName system | Where-Object { $_.eventID -eq 50036}

Looks a bit big ? and confusin ?? :) , well it isn't that complicated, as you will soon see.
Side note: In powershell, the execution of the statements happen from "Left" to "Right" side of the command.

On the first block of the command, we have the "Get-Eventlog" statment, this is a powershell commandlet which lets you to get information from the eventlog, it could be from the application eventlog or the system eventlogs.

Well, you may go try just that by typing "Get-Eventlog", but you can see that it asks you which eventlog it wants to access, then when you key in either "System" of "Application", it will then show you all the eventlogs in the entire server for that particular section.

We don't want that? do we ? , we need to search the eventlog for a specific event id , in our eg., event id : 50036

Our first part of the command gets us the entire eventlog and displays that information on to the powershell console
"
Get-EventLog -LogName system" - Here the System Eventlog

Our last segment of the command , includes the below statment.

| Where-Object { $_.eventID -eq 50036}


You see the first sign in the beginning, well, that is not a l or 1,, it is a pipe sign, well the blogger site doesnt have another font which will show the sign clearly, well you get my point ?

So what we are doing here with this sign ? we are taking the results of the first command which is to get the System eventlog and then pass it to the next segment .

Once the data is passed on to the next segment, you can modify the data as you wish, may be do a little sorting and formatting, but there it is, it is all yours to play around, but for our little command, we just need to filter a particular event.

Where-Object - this command let is similar to a SQL query or if you are familiar with scripting, something like if then statement.

The Where-Object checks the all the items passed to it for the conditions listed within those curly braces, here we have specified, that "$_.eventid -eq 50036" , interesting little symbol there? did you notice that ?? i meant the "$_" , well you can read all about that variable in the below link:

http://www.computerperformance.co.uk/powershell/powershell_variables.htm

To say bluntly it is the current object variable, in our case the eventlog collection of events from the system eventlog.

so our command in complete says that, for each and every event, check if the eventid property of that particular event matches 50036.

So, that is it, let me try this on my test workstation, and see how the output comes out to be :)


Looks like it did the trick,, well it is really getting late for me, and I am going to sleep now, it is 7AM in the morning and my eyes are shutting off, so is my machine.

Please comment, if you have any questions or have something to say about this blog.

~f0x


தெய்வத்தால் ஆகாதெனினும் முயர்ச்சி தன்
மெய்வருத்த கூலி தரும்.