Link to home
Start Free TrialLog in
Avatar of Number5ix
Number5ixFlag for Australia

asked on

How do you write a batch/cmd file that calls Exchange 2007 cmdlets?

Hi all,

I've written a small batch file that accepts a parameter which is then used to create directories, setup an Exchange storage group, create an Exchange mailbox then mount that new mailbox.   The directory stuff works ok but the Exchange cmdlet bits fail with the following error:

'new-StorageGroup' is not recognized as an internal or external command, operable program or batch file.

Is there a special way I need to call these cmdlets from batch/command scripts?   Do I need to write my own Powershell cmdlet to get these working?   I know the cmdlets work individually because I use them all the time - they just don't work from this batch file.

Any hints?   I'm sure I'm missing something key here.   :)

Thanks!
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

I think your missing the fact that those are not batch commands but Windows Powershell commands.

Try renaming the file to .ps1 and running in powershell.
Avatar of Number5ix

ASKER

Fair answer but I've got a whole load of batch-specific stuff in there (if statements etc).   I'd assume I need to change all these to the Powershell equivalents first?

Renaming to .ps1 doesn't work because of this.
that is correct... you will need a powershell script.. I can help convert it if you would like.
I've already converted most of it but stil have 1 problem.   The first command I'm running from within my PowerShell script is New-StorageGroup for Exchange 2007.   Like this :

$GroupName = $args[0];
write-host "Creating $GroupName storage group ...";
new-StorageGroup -Server 'TMMAIL0' -Name '$GroupName Storage Group' -LogFolderPath 'D:\Exchange\Mailbox\$GroupName Storage Group' -SystemFolderPath 'D:\Exchange\Mailbox\$GroupName Storage Group';

When I do that it makes a storage group called $GroupName ... how do I insert the argument password on the command line ($args[0]) instead of $GroupName?
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi there,

Thanks a lot for your help so far.   Sorry to keep asking but can you please clarify the Param($GroupName) line?   Is that line supposed to get the first parameter that is passed to the PS script?   When I do that I get the following :

The term 'param' is not recognized as a cmdlet, function, operable program, or
script file. Verify the term and try again.
At C:\scripts\CreateTeam.ps1:15 char:7
+     Param( <<<< $GroupName)
No bother at all.. its why I am here.

Param MUST be the first element of a script or function (the very first line.)

When you pass arguments or parameters to a script or function you have two options to access that information.
1) is using the Param statement.
    - This allows you to name the variable
    - allows you to use named parameters or positional
    - Allows you to set a default value or throw an error if missing
    - Must be the first line of the script or function
2) using the $args variable.
    - variables are ABSOLUTELY positional
Hi again,

Thanks again for your help!   I didn't end up using the Param() function as $args does what I want for now (param didn't work for me anyway).   I assure you BSonPosh you'll be getting the points but I have another question for you that's kinda strange.

I'm using the following Exchang cmdlets in my PowerShell script :

New-StorageGroup - works fine
New-MailboxDatabase - works fine
Set-MailboxDatabase - works fine
Mount-Database - doesn't work.   When I run this from within my script I get the following error:

Mount-Database : Exchange is unable to mount the database that you specified. S
pecified database: TMMAIL0\Commercial Storage Group\Commercial Mailbox Database
; Error code: MapiExceptionADNotFound: Unable to mount database. (hr=0x80004005
, ec=2417)
.
At line:1 char:15
+ mount-database  <<<< -Identity "CN=Commercial Mailbox Database,CN=Commercial
Storage Group,CN=InformationStore,CN=TMMAIL0,CN=Servers,CN=Exchange Administrat
ive Group (FYDIBOHF23SPDLT),CN=Administrative Groups,CN=xxx,CN=Microsoft Ex
change,CN=Services,CN=Configuration,DC=xxx,DC=local"

If I then try to run Mount-Database from the PowerShell prompt I get the same error.

If, however, I create the storage group and database from within the console and also mount it from the console, everything works fine.

What could be causing this?   I've gone over my script and I'm pretty sure there aren't any silly typos or anything like that.   :)
First, no need to reassure me about the points. Points are not why I do this :)

As for the problem... I don't really know exchange that well (I'm learning,) but I have seen references to this being a timing issue. Try to put a start-sleep before the mount. Also.. do a get-help on New-MailboxDatabase and see if it has a mount option.

Start-Sleep worked perfectly!   Thanks BSonPosh for all your help - I really appreciate it!   I'll keep you in mind for future stuff like this (if you don't mind).   :)
dont mind at all :)