Dejan Foro dejan.foro@exchangemaster.net Speaker 16years of years of experience with MS technologies 11years and 5generations of experience with Exchange (5.5, 2000, 2003, 2007, 2010) MCP, MCP+I, MCSE NT40, MCSE+I, MCSE 2000, MCSE 2000:Messaging, MCSA 2003, MCSA 2003:Messaging, MCSE 2003, MCSE 2003:Messaging, MCT 1
Speaker Communities Exchange User Group Europe (founder) Swiss IT Pro UserGroup Rewards MCP Hall of Fame (1 of 6 worldwide) Microsoft MVP - Exchange (1 of 120 worldwide, the only 1 in Switzerland) MCP Success Stories Agenda Introduction Doing common tasks in Exchange Users related tasks Testing and troubleshooting Setting up your own PowerShell enviroment Additional resources 3rd party tools, web sites, communities, books, webcasts... Q&A 2
Presentation dowlnoad This presentation will be available for download from: www.exchangemaster.net Introduction How it used to be... Problemsof Scripting in Windows enviroment Manythingscould be done through command line VBScript programming knowledge required, knowledge of VB, WMI, WBEM, ADSI, object models security voulnearable 3
Introduction How it is today... Scriptingwith PowerShell in Exchange Command line interface developed first, than GUI EVERYTHING can be done via command line Exchange managment GUI actually executes PowerShell commands and shows you the syntax Single line commands replace pages of VB code Symple syntax Better security exectution of powershell scripts completely disabled by default, require scripts to be signed, etc. Example 4
Introduction Windows Powershell - 129 commands Get-Command Exchange Powershell additional 394 commands Get-Excommand Don t worry, you will be cool with approx. 20 Why are you going to love PowerShell Task example : Get a list of mailboxes and export into.csv file 5
Why you are going to love PowerShell VBScript Dim SWBemlocator Dim objwmiservice Dim colitems Dim objfso Dim objfile strtitle="mailbox Report" strcomputer = MyServer" UserName= "" Password = "" strlog="report.csv" Set objfso=createobject("scripting.filesystemobject") Set objfile=objfso.createtextfile(strlog,true) strquery="select * from Exchange_Mailbox" Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator") Set objwmiservice= SWBemlocator.ConnectServer(strComputer,"\root\MicrosoftExchangeV2",UserName,Password) Set colitems = objwmiservice.execquery(strquery,,48) For Each objitem In colitems objfile.writelineobjitem.servername& "," &objitem.storagegroupname&_ "," & objitem.storename& "," & Chr(34) & objitem.mailboxdisplayname Next objfile.close Why you are going to love PowerShell PowerShell Get-mailbox export-csv c:\report.csv 6
... And Action!!! Command Syntax New-Mailbox Get-Mailbox Set-Mailbox Move-Mailbox Remove-Mailbox... 7
Getting help List of all available PowerShell commands Get-Command List of onlyexchange commands Get-Excommand Getting help about specific command Get-Help Get-Mailbox Get-Help Get-Mailbox detailed Get-Help Get-Mailbox full Getting info about users/mailboxes List of all mailboxes in organisation Get-Mailbox Get-Mailbox -ResultSize unlimited 8
Getting all available properties Get-Mailbox Format-List Get-Mailbox ResultSize 1 Format-List Getting just a list of properties names Get-Mailbox Get-Member -MemberType *Property Select-Object Name 9
Selecting & Sorting Get-Mailbox Select-Object -Property DisplayName, PrimarySMTPAddress Get-Mailbox Select-Object -Property DisplayName, PrimarySMTPAddress Sort-Object -Property DisplayName Examples List all mailboxes, sort by name, and export into a CSV file Get-Mailbox Sort-Object -Property Name Export-csv c:\mailboxes.csv Get a list of mailboxes from Active Directory OU named Users Get-Mailbox -OrganizationalUnit Users 10
Examples Count mailboxes in organisation (Get-mailbox).count Getting all properties for a specific user Get-Mailbox where {$_.DisplayName -eq "Dejan Foro"} format-list Who is thepostmaster? Get-Mailbox where {$_.EmailAddresses - contains "postmaster@exchangemaster.net"} Examples Who is the user with GUID e65a6ff3-d193-4563-9a8e- 26a22315a686? Get-Mailbox where {$_.guid -eq "e65a6ff3-d193-4563-9a8e-26a22315a686"} Who has UM extention 200? Get-Mailbox where {$_.extensions - contains "200"} 11
Getting info about servers Give me a list of Exchange servers Get-Exchangeserver Get-ExchangeServer Select-Object -Property Name, Edition, AdminDisplayVersion, ServerRole format-list Examples Give me a list of mailbox servers Get-ExchangeServer where {$_.ServerRole -ilike "*Mailbox*"} Do we have servers running on trial version of Exchange and if yes when do they expire? Get-ExchangeServer where {$_.IsExchange2007TrialEdition -eq "True"} Select-Object -Property FQDN, RemainingTrialPeriod 12
Getting membership of a group Get-DistributionGroupMember -identity "Swiss IT Pro User Group Moderators" Managing the user lifecycle Creating users - Importing from a.csv file Modifing users move to another database Removing mailboxes and users 13
Importing users from a.csv file Task Import users from a file c:\users.csv For every user Create user account in AD of form First.Last@exchangemaster.net Put them in Organizational Unit VIP Create a mailbox in database Standard users Enter his first and last name Set all users with password Password123 and require the users to change the password at first logon Importing users from a.csv file Import-CSV c:\users.csv 14
Procesing values from a csv file Processing each row of data from.csv file Import-CSV c:\users.csv ForEach-Object { SOME ACTION} Command for creating Users New-Mailbox Get-Help New-Mailbox full Processing values from.csv file Referencing column names from the.csv file $_.columnname Converting Password text into secure string $Password = ConvertTo-SecureString - String "Password123" -asplaintext -force 15
Importing users from a.csv file Putted all together $Password = ConvertTo-SecureString -String "Password123" - asplaintext -force Import-Csv c:\users.csv ForEach-Object { $Name = $_.First + " " + $_.Last $UPN = $_.First + "." + $_.Last + "@exchangemaster.net" New-Mailbox -Name $Name -UserPrincipalName $UPN -Password $Password -OrganizationalUnit VIP -Database 'standard users' - FirstName $_.First -LastName $_Last -ResetPasswordOnNextLogon $True} Making changes to users Apply policies Assing to groups Enable or disable features Changing attributes Moving mailboxes... 16
Moving mailboxes Moving mailoboxes of users in OU VIP to a new database for VIPs Get-Mailbox -OrganizationalUnit "VIP" Move-Mailbox -TargetDatabase "VIP users" Moving mailboxes Checking for mailbox location after move Get-Mailbox Select-Object Name,Database 17
Removing mailboxes Check before deleting! Get-Mailbox -OrganizationalUnit VIP Remove-Mailbox -WhatIf Remove them Get-Mailbox -OrganizationalUnit VIP Remove-Mailbox Recommendation 3rd party snap-in for better manipulation of ADobjects Quest Software ActiveRoles Management Shell for Active Directoy 18
Managing queues Removing spam messages from the queue Remove-Message -Filter {FromAddress -like "*spammer.com* } -withndr $false Testing Get a list of test commands Get-Command test* 19
Testing Script example Report on Exchange database backups with Powershell http://www.exchangemaster.net/index.php?option=com_conte nt&task=view&id=68&itemid=57 20
Setting up your Exchange PowerShell learning enviroment Prerequisites Supported OS Microsoft Windows Server 2003 R2, or Microsoft Windows Server 2003 with SP1 or SP2 Windows XP with Service Pack 2 Windows Vista Windows 2008 The Microsoft.NET Framework 2.0 (2.0.50727) Powershell Exchange 2007 Setting up your Exchange PowerShell learning enviroment Alternative if you don t want to bother: Microsoft Virtual PC Ready-made Microsoft Virtual Hard drive Exchange 2007 Exchange 2007 SP1 beta 21
PowerShell security and common problems for beginners How do I run PowerShell script? When you try to run a PowerShell script from the Run dialog box or by double clicking it, the script does not execute, but opens in Notepad. Answer: Invoke PowerShell.exe with full path to script PowerShell.exe c:\scripts\myscript.ps1 PowerShell security and common problems for beginners 22
PowerShell security and common problems for beginers Script blocked due to execution policy Policy Types Restricted, AllSigned RemoteSigned Unrestricted Commands Get-ExecutionPolicy Set-ExecutionPolicy Unrestricted Setting to Unrestricted is not recommended in production!!! PowerShell and Common problems for beginers Problem script will not execute if contains Exchange commands Solution -create a PowerShell profile which will load Exchange snap-ins for instructions see: FAQ 000037 -error when executing PowerShell script which contains Exchange PowerShell commands 23
Communicating with user from the script Prompting user Write-Host -ForegroundColor red -BackgroundColor yellow "Formating your drive c:..." Write-Host -ForegroundColor blue -BackgroundColor green "Be cool I am just kidding" Getting user input Read-Host 3rd party editors Primal Script 2009 Autocomplete, Shows you syntax as you type List of switches Color coding Debugging Script signing with a one button click Comparing scripts Source control FTP transfer Code reusing Etc. 24
Primal Script Editors and enhancments Primal Script http://www.primalscript.com/ PowerShell IDE http://powershell.de/ PowerShell Analyzer www.powershellanalyzer.com PowerShell Plus http://www.powershell.com/plus/ 25
I don t like comand line Alternatives to PowerShell Power GUI http://www.powergui.org/index.jspa AD Infinitum http://www.newfawm.com/adi2.htm Scriptlogic http://www.scriptlogic.com/ PowerGUI 26
PowerGUI PowerGUI 27
PowerGUI Additional Exchange resources Microsoft Exchange server web site http://www.microsoft.com/exchange Microsoft Exchange Team Blog (You had me at EHLO) http://msexchangeteam.com Technet Microsoft Exchange Server TechCenter http://www.microsoft.com/technet/prodtechnol/exchange/defa ult.mspx Virtual labs http://www.microsoft.com/technet/prodtechnol/exchange/virtu allab/default.mspx Webcasts http://www.microsoft.com/technet/prodtechnol/exchange/2007 /webcasts.mspx 28
PowerShell books Lite Frank Koch, Microsoft Switzerland German version English version PowerShell books medium PowerShell documentation Pack Manuals that comes with Powershell http://www.microsoft.com/downloads/details.aspx?famil yid=b4720b00-9a66-430f-bd56- ec48bfca154f&displaylang=en 29
Powershell Books Advanced Converting VBScript Commands to Windows PowerShell Commands http://www.microsoft.com/technet/scriptcenter/topics/winpsh/convert/default. mspx 30
Webcasts in German Klickst Du noch oder skriptestdu schon? Frank RöderMVP Windows Server System - Directory Services Teil 1Windows PowerShell Einführung Teil 2 Server-Administration mitwindows PowerShell Teil 3 Exchange Server 2007 Administration mit Windows PowerShell Websites and blogs Windows PowerShell Home http://www.microsoft.com/windowsserver2003/technologies/management/po wershell/default.mspx Blog of Windows Powershellteam http://blogs.msdn.com/powershell The Microsoft Exchange Team blog: http://msexchangeteam.com/default.aspx Vivek Sharma s blog: http://www.viveksharma.com/techlog/category/scripts/ The PowerShell Guy http://thepowershellguy.com/blogs/posh/pages/powertab.aspx 31
Websites and blogs Glen Scales development blog http://gsexdev.blogspot.com/ Webcasts Introduction to Windows PowerShell Scripting in Exchange Server 2007 (Level 200) TechNet Webcast: 24 Hours of Exchange Server 2007 (Part 09 of 24): Using PowerShell for Exchange Management (Level 200) Microsoft Windows PowerShell Scripting for Microsoft Exchange Server 2007 (Level 300) 32
Community PowerShell Anwendergruppe www.powershell-ag.de Community PowerShell Live Get-Community Where {$_.passion -eq "PowerShell } http://powershelllive.com 33
Community Swiss IT Pro user group www.swisitpro.ch Exchange User Group Europe http://www.eugeurope.org Speakers wanted Get-speaker where {$._passion eq commnunity } Contact dejan.foro@eugeurope.org 34
Questions? Questions and Answers Q - Can powershell be used against Exchange 2003 machines? A yes, but not with all functionality and looks ugly http://blog.sapien.com/current/2007/2/21/managingexchange-2003-servers-with-powershell.html 35
Questions and Answers Q CanPowerShellbeusedfrom.NETLanguageslike IronPhyton? A PowerShellis basedon.netframeworkandthereforecanbe embededinother.netlanguagesandvice versa. Hereare some examples: Using IronPythonfrom PowerShell Part 1 : Watch folder for changes without blocking Console http://thepowershellguy.com/blogs/posh/archive/2007/02/10/usingironpython-from-powershell-part-1-watch-folder-for-changeswithout-blocking-console.aspx Embedding PowerShell in IronPythonand vice versa -- two great tastes that taste great together http://stevegilham.blogspot.com/2008/02/embedding-powershellin-ironpython.html Presentation dowlnoad This presentation will be available for download from: www.exchangemaster.net 36
Contact: E-mail: dejan.foro@exchangemaster.net LinkedIn: http://www.linkedin.com/in/dejanforo Xing (OpenBC): http://www.xing.com/profile/dejan_foro 37