Monday, September 23, 2013

Load Powershell Addins in your scripts


It is often that you want to write a script which uses Powershell cmdlets that are not the basic ones found in Powershell v2.0.

For example, you might want to perform actions on Active Directory using Windows 2008 built in AD commands, Exchange 2010 commands or even Quest Active Roles.

Option 1 - Importing the cmdlets from the batch
One option to run a script with Exchange 2010 commands (on a sever running Exchange 2010 or a server with the Exchange management tools installed) is to call the script from from a batch file which loads the Exchange powershell extentions from the batch file. For Example you .cmd or .bat file will look like this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\Scripts\YourScriptName.PS1"


Option 2 - Importing the  cmdlets  from the script
This option - also running on an Exchange server or a server with the Exchange management tools installed, use the batch to load the script using the "Normal" Powershell with this command:

powershell -command "& 'C:/scripts/YourScriptName.ps1'"

and

In the script file, use the following commands to load the needed PSSnapIn:

For Exchange 2010 PSSnapIn
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010

For Microsoft Active Directory commands (2008)
Import-Module activedirectory

For Microsoft Microsoft Lync 2010
Import-Module Lync

For Quest Active-Roles commands (Free download from Quest software Click Here )
Add-PSSnapin Quest.ActiveRoles.Admanagement


Example:

createmailboxlist.cmd
powershell -command "& 'C:/scripts/Mailboxlistgenerator.ps1'" 


Mailboxlistgenerator.ps1
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010 
$list = get-mailbox -resultsize unlimited | select name,database,primarysmtpaddress
$list | export-csv c:\scripts\Mailboxlist.csv


Option 3 - Remoting commands to Exchange server
This option allows you to run Exchange Powershell commands from a computer running Powershell with
no Exchange snap-ins by creating a remote session to the Exchange server.
This requires an http connection between the the server or workstation and the Exchange server.
Use the following commands:

$Session = new-pssession -configurationname microsoft.exchange -connectionUri http://ExchangeServerName/powershell
Import-PSSession $Session

From this step you can run Exchange Powershell commands.


Now all you need is to schedule the command by running the batch or run it manually





1 comment:

  1. Casino de Monte-Carlo Casino (Italy) | JM Hub
    Casino 안성 출장샵 de Monte-Carlo 여주 출장샵 Casino (Italy) | JTM Hub | The best place 군산 출장샵 to see and get updates, 정읍 출장샵 videos, 익산 출장안마 photos, directions, videos, more.

    ReplyDelete