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

Sunday, December 20, 2009

How to set quota notification interval in Exchange 2007

The intent of this post is to give some details on how to set quota notification internal in Exchange 2007. During this interval, a wanning message will be sent to users who are exceeding(or near to) their mailbox limits.

Set Quota notification schedule on  a given Mailbox database:


Set-mailboxdatabase -identity "MBX1\SG1\S1" -QuotaNotificationSchedule ("Sun.6:00 AM-Sun.6:15 AM", "Mon.6:00 AM-Mon.6:15 AM", "Tue.6:00 AM-Tue.6:15 AM", "Wed.6:00 AM-Wed.6:15 AM", "Thu.6:00 AM-Thu.6:15 AM", "Fri.6:00 AM-Fri.6:15 AM", "Sat.6:00 AM-Sat.6:15 AM")

Set Quota notification schedule on  all Mailbox databases:

Get-MailboxDatabase | Set-mailboxdatabase -QuotaNotificationSchedule ("Sun.6:00 AM-Sun.6:15 AM", "Mon.6:00 AM-Mon.6:15 AM", "Tue.6:00 AM-Tue.6:15 AM", "Wed.6:00 AM-Wed.6:15 AM", "Thu.6:00 AM-Thu.6:15 AM", "Fri.6:00 AM-Fri.6:15 AM", "Sat.6:00 AM-Sat.6:15 AM")


Set Quota notification schedule on  all Mailbox databases on a single server:

Get-MailboxDatabase -Server MBX1 | Set-mailboxdatabase -QuotaNotificationSchedule ("Sun.6:00 AM-Sun.6:15 AM", "Mon.6:00 AM-Mon.6:15 AM", "Tue.6:00 AM-Tue.6:15 AM", "Wed.6:00 AM-Wed.6:15 AM", "Thu.6:00 AM-Thu.6:15 AM", "Fri.6:00 AM-Fri.6:15 AM", "Sat.6:00 AM-Sat.6:15 AM")
You can change the notification schedule timings as you want.

Happy Learning..,
Sitaram Pamarthi

Bulk User mailbox creation in Exchange 2007

While working on setting up my test Exchange 2007 setup, I got a requirement to create 100 users in my environment. I know how to do it with Vbscript but creating mailbox enabled users increases the complexity of script a bit. Then I explored PowerShell and came up with below piece of code. It might help you too... :-)


[PS] C:\>$password = Read-Host "Enter the password" -AsStringString
[PS] C:\>foreach ($number in 1..100) { new-mailbox -name user$number -Alias user$number -database "MBX1\SG1\S1" -organizationalUnit "SPDOMAIN.COM/INDIA/Users" -UserPrincipalName user$number@spdomain.com -Displayname User$number -password $password }
Please note that, the password will be applied for all 100 test accounts we are creating. Above code creates 100 test user accounts with mailbox enabled on MBX1
\SG1\S1" store.


Happy Learning..,
Sitaram Pamarthi

Wednesday, December 9, 2009

List the files greater than given size using powershell

Below powershell code helps you to list the files which are greater than given size(2GB in this case) and output the file sizes in MB/GB format.

PS C:\local> Get-ChildItem -path c:\mydata\ -recursive | where  { ($_.Length /1GB) -gt 2 } | foreach { ($_.length/1GB).Tostring("0.00") }

Do write in comments section here if you need any charification.

Happy Learning,
Sitaram Pamarthi

Monday, December 7, 2009

Use power shell to get installed patches from windows box

Today, I will take you through some of the PowerShell one-liners which will help you in querying patches installed in your machine.

List All installed patches:

PS C:\>gwmi Win32_QuickFixEngineering | select Description, Hotfixid

List all patches that are installed in last 30 days:

PS C:\> gwmi Win32_QuickFixEngineering | ? {$_.InstalledOn} | where { (Get-date($_.Installedon)) -gt (get-date).adddays(-30) }

List all patches installed on specific date:

PS C:\> gwmi Win32_QuickFixEngineering | ? {$_.InstalledOn} | where { (Get-date($_.Installedon)) -gt (get-date 10/31/2009) }

Query for a specific hotfix by providing hotfix ID(KB12345 format):

PS C:\> gwmi Win32_QuickFixEngineering | ? {$_.HotfixID -match " KB975025" }

List all hotfixes installed by administrator account:

PS C:\> gwmi Win32_QuickFixEngineering | ? {$_.InstalledBy -match "administrator" }

You can adopt the above queries and run against remote machine by passing the computer name shown like below.

PS C:\> gwmi Win32_QuickFixEngineering -Computer remotecomp | ? {$_.InstalledOn} | where { (Get-date($_.Installedon)) -gt (get-date).adddays(-30) }

Happy Learning,
Sitaram Pamarthi

Wednesday, December 2, 2009

PowerShell Commands to list domain controllers in Domain.

My previous article talks about listing domain controllers in a domain in non-powershell world. But with introduction of powershell things changed a lot to give more flexibility to system administrators and developers.

Below piece of powershell code helps to you get it.

List domain Controllers in domain:
$localdomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()

$localdomain | % { $_.DomainControllers } | Select name
 
List all domain controllers in forest


$localdomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()$localdomain.forest.domains | % { $_.DomainControllers } | Select name


You can count the no. of domain controllers also.


($localdomain.forest.domains | % { $_.DomainControllers } | Select name).count
Happy Learning..,
Sitaram Pamarthi

Tuesday, December 1, 2009

Find password last set/reset time using Powershell

This small piece of code helps you to know when a active directory user has changed his password last time. Use your own inventions to make the output appear the way you want.
$user = "user1"
$searcher=New-Object DirectoryServices.DirectorySearcher
$searcher.Filter="(&(samaccountname=$user))"
$results=$searcher.findone()
$changedtime  = [datetime]::fromfiletime($results.properties.pwdlastset[0])
write-host -b blue -f red The user, $user has changed password last time at $changedtime

It will be more easy if you have Quest PowerShell Cmdlets for active roles installed in your PC. Download it from Quest site and install on your machine and execute the below oneliner in Quest powershell window.


Get-QADUser user1 | ft displayname, PasswordLastSet

Happy Learning..,
Sitaram Pamarthi

Friday, November 27, 2009

Log all commands executed in Exchange Management shell to EventViewer


If you want to log all the commands that you are executing in Exchange 2007 management shell to eventviewer, use the below registry key. As a result of this, all the commands you are executing from Exchange Shell will be logged to eventviewer and you can refer them whenever you want. This way we can keep track of what changes we are doing on day-to-day basis.
set-itemproperty HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\Microsoft.Exchange.Management.PowerShell.Admin -Name
 LogpipelineExecutionDetails -value 1
Happy Learning..,
Sitaram Pamarthi

Log the commands your are executing in PowerShell


If you want to log all the commands executing through powershell to a text file, you can use "strat-transcript" cmdlet
Usage:
Start-Transcript c:\your-folder-path\pslogging.txt
This starts logging all the commands you execute from powershell window to text file. This is very help when you are making critical changes.

Friday, November 6, 2009

PowerShell: Query a windows machine Serial Number Remotely


I have seen system administrators who feels bending the CPU box or rebooting the system are only ways to find out serial number of a machine. In situations where a remote systems serial is required, they do nothing but reaching that machine physically.

Enough doing hard work and be smart, guys; here is a small powershell code which helps you to get serial number of local or remote machine without moving your feet.

PS C:\> (Get-WmiObject -computer COMP-A -cl win32_BIOS).SerialNumber
ABCDEFG

Replace COMP-A with the computer name you want to query and needless to say that you need admin rights to execute this command.

Post in comments section here if you have any questions.

Happy Learning,
Sitaram Pamarthi

Wednesday, November 4, 2009

PowerShell: Text to Voice conversion


Today, while playing with PowerShell, I came across a interesting COM object "SAPI.SpVoice". Using this object you can convert given text stream to voice.

Sample script:

[PS] C:\>(new-object -com SAPI.SpVoice).speak("Sitaram Pamarthi")

Usage:

Copy this sample script into powershell window and switch ON your speakers/head phones. You can listen to the voice conversion of text you entered. You can explore more options of this by going through it's properties and methods, (new-object -com SAPI.SpVoice) | get-member.

Happy Learning…,
Sitaram Pamarthi

Tuesday, November 3, 2009

PowerShell: Check if a file is read only or not

This small powershell script checks if a given file is read-only or not.

$status = Get-ChildItem c:\temp\test.txt
If ($status.isreadonly)
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}
Else
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}

If you want to make a file as readonly, use the below code

$status = Get-ChildItem c:\temp\test.txt
$status.set_isreadyonly($true)
If ($status.isreadonly)
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}

Else
{
Write-host -b yellow -f red “The file, $status.fullname is a read-only file”
}

If you want to remove the read-only a ttribute just change the parameter of set_isreadonly method to $false.

Happy Learning,
Sitaram Pamarthi

Get process details using window title:

I came across a incident where I have to kill one of the running MMC(active directory users and computers) as it is not responding properly. I went to task manager and tried killing the process but in vain. I am sure this happens for many of the system administrations and they had to go to the processes tab and find-out the process and kill it. I thought of doing that but I have few more other important MMCs running which I don’t want to disturb.  I looked around for a quick way to find out which MMC process belongs to my “Active Directory Users and Computers” MMC and resulted in this small and power{full | shell} script.


PS C:\> Get-Process | where {$_.mainwindowtitle -match "active directory users and computers"}


And to stop/kill that process I used powershell only as it is straight forward from here.

PS C:\> Get-Process | where {$_.mainwindowtitle -match "active directory users and computers"} | stop-process

I am done with my task :-)

Happy Learning,

Sitaram Pamarthi

Get Uptime of Microsoft Windows Operating System using PowerShell

There are variety of ways and tools available for generating uptime of a windows XP/2003 machine. This post describes the procedure for generating uptime with powershell code. You can use this code in your powershell scripts without depending on third-party tools/utilities.


$os = Get-WmiObject -cl win32_operatingsystem

write-host ((Get-date) - $os.converttodatetime($os.lastbootuptime))"(DD.HH:MM:SS)" | fl


Copy and paste this to your powershell window and format the output like you want.


Happy Learning,

Sitaram Pamarthi

Thursday, August 20, 2009

Find the filesize using PowerShell

Below piece of code helps to find the size of a file that you mention. This code can find the size of a local file, or a file on remote system. You need to give the full path of file to make it work.

$filepath="YOUR FILE PATH"
Get-ChildItem $filepath | ForEach-Object { Write-Host $_.name,==> ($_.Length/1MB).tostring("0.00")MB

Findout Operating System of Remote Machine using PowerShell

Below piece of code helps you.

PS C:\>Get-WmiObject -Computer MyRemoteHost Win32_OperatingSystem | select CSname, Caption, CSDVersion | fl

Wednesday, August 19, 2009

Configure Exchange 2007 OWA using PowerShell

In this post, I am giving the powershell commands to configure OWA authentication mechanism as per your needs usign powershell cmdlets. Please write to me (in comments sections) if you need any clarifications.

Configure Exchange 2007 OWA to take default domain name:

Set-owavirtualdirectory -identity "owa (default web site)" -LogonFormat UserName -DefaultDomain mydomain.com

Configure Exchange 2007 OWA to accept only "domain name\User Name" format:


Set-owavirtualdirectory -identity "owa (default web site)" -LogonFormat fulldomain

Configure Exchange 2007 OWA to use form-based authentication:

Set-owavirtualdirectory -identity "owa (default web site)" -FormsAuthentication:$true

Configure Exchange 2007 OWA to use integrated authentication:

Set-OwaVirtualDirectory -Identity "owa (Default Web Site)" -WindowsAuthentication $true

Happy Learning,
Sitaram Pamarthi

Enable Diagnostic Logging in Exchange 2007


In Exchange 2003, you need to go to "Diagnostic logging tab" in Exchange server properties to enable required logging. This has been simplified in Exchange 2007 and now you can do it through Exchange Shell.

List Logging Levels (Get-EventLogLevel)

To get the current logging levels of a Exchange 2007 server, run the below command. You put your server name after "-server". If you don't specify any it will display the logging levels of local server. The logging level list varies depending on the role of the Exchange 2007 server (MBX, CAS, HTS).

Get-EventLogLevel -Server MYSERVER

Set Logging Level (Set-EventLogLevel)

You can increase the logging level of any listed identify to one of the below values. Below levels are self explanatory and I am hoping no need of further descriptions of them.
  • Lowest
  • Low
  • Medium
  • High
For example, if I want to change "MSExchange ADAceess\Validation" Logging level to low. I will use the below command.

Set-EventLogLevel "MSExchange ADAceess\Validation" -Level High

You can do it remaining for identities as well in similar way. Good Luck and Happy Learning..

Sitaram Pamarthi

Tuesday, August 4, 2009

Find disk space of remote machine with powershell script

Subject says it all. This code helps you to findout the disk space of remote machine.

Code(save it into a file with ps1 extension):
$hostname=Read-host "Enter the computer name"
get-wmiobject -computer $hostname win32_logicaldisk -filter "drivetype=3" | ForEach-Object { Write-Host  Device name : $_.deviceid\; write-host Total space : ($_.size/1GB).tostring("0.00")GB; write-host Free Spce : ($_.freespace/1GB).tostring("0.00")GB }

Output
PS C:\temp> .\CheckSpace.ps1
Enter the computer name: MyRemotePC
Device name : C: \
Total space : 232.75 GB
Free Spce : 130.51 GB
PS C:\temp\>