Monday, May 2, 2011

Exchange 2007 Powershell

Mailbox Size Report - Individual Database

cls
Get-MailboxStatistics -Database "ecgcmail01\webmail users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 145MB} | ft Displayname , @{expression={$_.TotalItemSize.Value.ToMB()}}, StorageLimitStatus

Get-MailboxStatistics -Database "ecgcmail01\general users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 65MB} | ft Displayname , @{expression={$_.TotalItemSize.Value.ToMB()}}, StorageLimitStatus

*******************************

update 10K Photo in AD

Import-RecipientDataProperty -Identity "Bharat Suneja" -Picture -FileData ([Byte[]]$(Get-Content -Path "C:\pictures\BharatSuneja.jpg" -Encoding Byte -ReadCount 0))

**************************************

Enable OWA for list of users

$path = “d:\Andy\Owa.txt”
foreach ($line in Get-Content $path) { Set-CASMailbox –Identity $line –OWAEnabled:$true }

**********************************************
Logon Statastics Report

Get-LogonStatistics -Server "ecgcmail01" | Sort-Object Logontime –Descending | ft userName , Logontime

*******************************
List of Databases

Get-Mailbox -Database "ecgcmail01\general users\mailboxes" |Sort-Object Name –Descending

*******************************
Move all Mailboxes from database

Get-Mailbox -Database "ecgcmail01\ecgc users\general user" |Move-Mailbox -TargetDatabase "ecgcmail01\general users\mailboxes" -PreserveMailboxSizeLimit

*******************************
List Group Members

Get-QADGroupMember -Identity ITCoordinators

*******************************
For Logon Details

$last30d = (Get-Date).Subtract((New-TimeSpan -Days 15))
get-mailboxstatistics -Server ecgcmail01 | where {$_.lastlogontime -lt $last30d} | Ft Displayname , lastlogontime

*******************************
Add User to Group

Add-QADMemberOf -Group "Internet users" -Identity "ecgcindia\sukhan.singh"

*******************************
Get all disconnected mailboxes

Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid

*******************************
Remove a single disconnected mailbox

Remove-Mailbox -Database <Database-Name> -StoreMailboxIdentity <MailboxGuid> -confirm:$false

*******************************
Remove all disconnected mailboxes

$users = Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid,Database

*******************************
Update GAL

Update-GlobalAddressList  -identity "Default Global Address List"

*******************************
Certificate Request

New-ExchangeCertificate -GenerateRequest -Path c:\webmail_ecgc_in.csr -KeySize 1024 -SubjectName "c=IN, s=Maharashtra, l=Mumbai, o=Export Credit Guarantee Corporation of India, ou=MAIL SERVER, cn=webmail.ecgc.in" -DomainName ecgcindia.com, ecgcowa01.ecgcindia.com, ecgcmail01.ecgcindia.com, webmail.ecgc.in, autodiscover.ecgcindia.com, autodiscover.ecgc.in, ecgc.in -PrivateKeyExportable $True

*******************************
Enable Disable Outlook Anywhere Access
set-casmailbox -mapiblockoutlookrpcthhp:$true

you can do:
get-user | set-casmailbox -mapiblockoutlookrpcthhp:$true
which will kill all access, and then go back and to a $false for the two in question to allow them to use it.

*******************************

To Create CAS Virtual directories using Exchange PowerShell

Get-OwaVirtualDirectory -server ecgcowa01 | Remove-OwaVirtualDirectory
New-OwaVirtualDirectory -Name "Exchange" -owaversion Exchange2003or2000 -VirtualDirectoryType Mailboxes -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "exadmin" -owaversion Exchange2003or2000 -VirtualDirectoryType exadmin -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "public" -owaversion Exchange2003or2000 -VirtualDirectoryType PublicFolders -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "owa" -owaversion Exchange2007 -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "exchweb" -owaversion Exchange2003or2000 -VirtualDirectoryType exchweb -WebSiteName "Default Web Site"

*******************************
Move Mailbox

Move-Mailbox -TargetDatabase "ecgcmail01\ECGC Users\webmail user" -Identity "ED" -preservemailboxsizelimit
Y
*******************************
Export Mailbox in PST

Export-Mailbox -pstfolderpath d:\andy\pst\ -identity "Ashok Dichwalkar"
Y
*******************************
cls
Get-MailboxStatistics -Database "ecgcmail01\general users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 65MB} | ft Displayname , TotalItemSize
Get-MailboxStatistics -Database "ecgcmail01\webmail users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 145MB} | ft Displayname , TotalItemSize

Friday, March 4, 2011

Some Exchange 2007 CMDlets

Mailbox Size Report - Individual Database

cls
Get-MailboxStatistics -Database "ecgcmail01\webmail users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 145MB} | ft Displayname , @{expression={$_.TotalItemSize.Value.ToMB()}}, StorageLimitStatus

Get-MailboxStatistics -Database "ecgcmail01\general users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 65MB} | ft Displayname , @{expression={$_.TotalItemSize.Value.ToMB()}}, StorageLimitStatus

*******************************

update 10K Photo in AD

Import-RecipientDataProperty -Identity "Bharat Suneja" -Picture -FileData ([Byte[]]$(Get-Content -Path "C:\pictures\BharatSuneja.jpg" -Encoding Byte -ReadCount 0))

**************************************

Enable OWA for list of users

$path = "d:\Andy\Owa.txt"
foreach ($line in Get-Content $path) { Set-CASMailbox –Identity $line –OWAEnabled:$true }

**********************************************
Logon Statastics Report

Get-LogonStatistics -Server "ecgcmail01" | Sort-Object Logontime –Descending | ft userName , Logontime

*******************************
List of Databases

Get-Mailbox -Database "ecgcmail01\general users\mailboxes" |Sort-Object Name –Descending

*******************************
Move all Mailboxes from database

Get-Mailbox -Database "ecgcmail01\ecgc users\general user" |Move-Mailbox -TargetDatabase "ecgcmail01\general users\mailboxes" -PreserveMailboxSizeLimit

*******************************
List Group Members

Get-QADGroupMember -Identity ITCoordinators

*******************************
For Logon Details

$last30d = (Get-Date).Subtract((New-TimeSpan -Days 15))
get-mailboxstatistics -Server ecgcmail01 | where {$_.lastlogontime -lt $last30d} | Ft Displayname , lastlogontime

*******************************
Add User to Group

Add-QADMemberOf -Group "Internet users" -Identity "ecgcindia\sukhan.singh"

*******************************
Get all disconnected mailboxes

Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid

*******************************
Remove a single disconnected mailbox

Remove-Mailbox -Database <Database-Name> -StoreMailboxIdentity <MailboxGuid> -confirm:$false
 
*******************************
Remove all disconnected mailboxes

$users = Get-MailboxStatistics | where-object { $_.DisconnectDate -ne $null } | Select DisplayName,MailboxGuid,Database

*******************************
Update GAL

Update-GlobalAddressList  -identity "Default Global Address List"

*******************************
Certificate Request

New-ExchangeCertificate -GenerateRequest -Path c:\webmail_ecgc_in.csr -KeySize 1024 -SubjectName "c=IN, s=Maharashtra, l=Mumbai, o=Export Credit Guarantee Corporation of India, ou=MAIL SERVER, cn=webmail.ecgc.in" -DomainName ecgcindia.com, ecgcowa01.ecgcindia.com, ecgcmail01.ecgcindia.com, webmail.ecgc.in, autodiscover.ecgcindia.com, autodiscover.ecgc.in, ecgc.in -PrivateKeyExportable $True

*******************************
Enable Disable Outlook Anywhere Access
set-casmailbox -mapiblockoutlookrpcthhp:$true

you can do:
get-user | set-casmailbox -mapiblockoutlookrpcthhp:$true
which will kill all access, and then go back and to a $false for the two in question to allow them to use it.

*******************************

To Create CAS Virtual directories using Exchange PowerShell

Get-OwaVirtualDirectory -server ecgcowa01 | Remove-OwaVirtualDirectory
New-OwaVirtualDirectory -Name "Exchange" -owaversion Exchange2003or2000 -VirtualDirectoryType Mailboxes -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "exadmin" -owaversion Exchange2003or2000 -VirtualDirectoryType exadmin -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "public" -owaversion Exchange2003or2000 -VirtualDirectoryType PublicFolders -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "owa" -owaversion Exchange2007 -WebSiteName "Default Web Site"
New-OwaVirtualDirectory -Name "exchweb" -owaversion Exchange2003or2000 -VirtualDirectoryType exchweb -WebSiteName "Default Web Site"

*******************************
Move Mailbox

Move-Mailbox -TargetDatabase "ecgcmail01\ECGC Users\webmail user" -Identity "ED" -preservemailboxsizelimit
Y
*******************************
Export Mailbox in PST

Export-Mailbox -pstfolderpath d:\andy\pst\ -identity "Ashok Dichwalkar"
Y
*******************************
cls
Get-MailboxStatistics -Database "ecgcmail01\general users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 65MB} | ft Displayname , TotalItemSize
Get-MailboxStatistics -Database "ecgcmail01\webmail users\mailboxes" | Sort-Object TotalItemsize -Descending | Where {$_.TotalItemSize -gt 145MB} | ft Displayname , TotalItemSize

Thursday, September 24, 2009

LDAP Queries

This LDAP query should show you all users in your enviroment with exchange mailboxes that have OWA enabled.

(&(objectCategory=user)(!protocolsettings=HTTP*0*)(msExchUserAccountControl=0))

This LDAP query should show you all users in your enviroment with exchange mailboxes that have OWA disabled.

(&(objectCategory=user)(protocolsettings=HTTP*0*)(msExchUserAccountControl=0))


Thursday, April 16, 2009

Delete Media Files

Script to delete media files
Script Code:
------------------------------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Extension = 'mp3' OR Extension = 'wma' OR Extension = 'flv' OR Extension = 'jpg' OR Extension = 'mov' OR Extension = 'avi' OR Extension = '3gp' OR Extension = 'flv' OR Extension = 'vob' OR Extension = 'mpeg'")
For Each objFile in colFiles
objFile.Delete
Next

Cleanup Old Files

Script to Delete files older than X days
Usage: cscript c:\scripts\deloldfiles.vbs c:\windows\system32\logfiles 30 >> c:\scripts\iis.log
Scrpit Code:
-----------------------------------------------------------------------------------------
' Objective: To delete old files from a given folder and all subfolders below
'
' Created by: MAK June 21, 2005
'
' Format: cscript deloldfiles.vbs {DriveLetter:\FolderName} {#ofDays}
' or: cscript deloldfiles.vbs {\\servername\FolderName} {#ofDays}
' Example: cscript deloldfiles.vbs c:\dba\log 3
' (deletes files older than 3 days from the \dba\log file on drive C:)
Set objArgs = WScript.Arguments
FolderName =objArgs(0)
Days=objArgs(1)

set fso = createobject("scripting.filesystemobject")
set folders = fso.getfolder(FolderName)
datetoday = now()
newdate = dateadd("d", Days*-1, datetoday)
wscript.echo "Today:" & now()
wscript.echo "Started deleting files older than :" & newdate
wscript.echo "________________________________________________"
wscript.echo ""
recurse folders
wscript.echo ""
wscript.echo "Completed deleting files older than :" & newdate
wscript.echo "________________________________________________"

sub recurse( byref folders)
set subfolders = folders.subfolders
set files = folders.files
wscript.echo ""
wscript.echo "Deleting Files under the Folder:" & folders.path
wscript.echo "__________________________________________________________________________"
for each file in files
if file.datelastmodified < newdate then
wscript.echo "Deleting " & folders.path & "\" & file.name & " last modified: " & file.datelastmodified
on error resume next
' === to test this script but not actually delete files, comment out the next line ===
file.delete
end if

next

for each folder in subfolders
recurse folder
next

set subfolders = nothing
set files = nothing

end sub

Wednesday, October 22, 2008

Batch command to change DNS

cd\
netsh int ip set dns "local area connection" static 10।120.135.100 primary 'Add primary DNS
netsh int ip add dns "local area connection" 10.4.148.103 'Add secondary DNS
netsh int ip add dns "local area connection" 10.120.135.101 'Add additional DNS

Friday, June 20, 2008

Script to remove domain user from local administrator group

' (This script can run as logon script without problems because it first check if the user is a member.
' If the user is a local administrator s/he has privileges to remove him/her self from the local group.
' Do Keep in mind however that the user at this stage already is logged-on as an Administrator,
' s/he will keep all the access rights until the next logon).

Set objNetwork = CreateObject("Wscript.Network")
strUser = objNetwork.UserName
strDomain = objNetwork.UserDomain
strComputer = objNetwork.ComputerName

strLocalGroup = "Administrators"

On Error Resume Next

' Bind to local group object.
Set objGroup = GetObject("WinNT://" _
& strComputer & "/" & strLocalGroup & ",group")
If (Err.Number <> 0) Then wscript.Quit

' Bind to the user object on the Domain.
' (use also the WinNT provider).
Set objUser = GetObject("WinNT://" _
& strDomain & "/" & strUser & ",user")
If (Err.Number <> 0) Then wscript.Quit

On Error GoTo 0

' Check If user is a member, then remove him/her self! from local group
If (objGroup.IsMember(objUser.AdsPath) = True) Then
objGroup.Remove(objUser.AdsPath)

End If

Wscript.Quit