• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3239
  • Last Modified:

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!
0
Number5ix
Asked:
Number5ix
  • 6
  • 5
1 Solution
 
BSonPoshCommented:
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.
0
 
Number5ixAuthor Commented:
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.
0
 
BSonPoshCommented:
that is correct... you will need a powershell script.. I can help convert it if you would like.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
Number5ixAuthor Commented:
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?
0
 
BSonPoshCommented:
two things

1) At the top of the script instead of using $args use Param statement like
Param($GroupName)

2) When you use single quotes ' variable expansion does NOT happen... use Double qoutes if you want variable expansion

########################
Param($GroupName)
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"
0
 
Number5ixAuthor Commented:
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)
0
 
BSonPoshCommented:
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
0
 
Number5ixAuthor Commented:
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.   :)
0
 
BSonPoshCommented:
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.

0
 
Number5ixAuthor Commented:
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).   :)
0
 
BSonPoshCommented:
dont mind at all :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now