Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with PowerShell script/batch file

Posted on 2010-09-03
15
Medium Priority
?
1,177 Views
Last Modified: 2012-05-10
I need to write a DOS batch file that can 'invoke' Powershell and then pass a cmd to the Powershell command line...

I can write the batch file that calls  'Powershell' and opens the cmd line in PS. But then how can I pass in - into  the PS command window ,  the commands I need to excute a PS command?

TEST.BAT  - looks like this.. but then I need to invoke the new-mailbox command.. how do I pass that in too?  


powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command "

0
Comment
Question by:JElster
15 Comments
 
LVL 14

Expert Comment

by:athomsfere
ID: 33601738
You should be able to eliminate the command prompt, as power shell can handle running a batch file just fine, or any scripting you need. Is there a specific reason?
0
 
LVL 28

Expert Comment

by:sunnyc7
ID: 33601766
Check this one
http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/

powershell.exe -command "1..10 | foreach-object { "Current output:"; $_; }
0
 
LVL 1

Author Comment

by:JElster
ID: 33602625
Sorry...
I don't understand... I don't know much or anything! about powershell.  I need to call powershell with the above params from code - probably using a DOS batch file a C# program would be best - and pass in the Powershell command - I need to run on the next line.


So I first call "powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command " to get the POWERSHELL CMD.  Then I need to pass in the New-MailBox command.

thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 71

Expert Comment

by:Qlemo
ID: 33602734
If you look at the command line which invokes the MSX Management Shell, you should get an idea how to use it for calling a script or a single command:
powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command "New-MailBox -name Example -alias Example -UserPrincipalName Example@Example.com -Database "Storage Group 1\Mailbox Database 1" /* ... */

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 33602898
Hi..
I have to call 1 command first.. then call the New Mail box.. the New Mail box is the one that I need to pass / change the paramaters..

The first command is

 proc.StartInfo.FileName = "powershell.exe -PSConsoleFile 'C:\\Program Files\\Microsoft\\Exchange Server\\bin\\exshell.psc1' -noexit -command '. 'C:\\Program Files\\Microsoft\\Exchange Server\\bin\\Exchange.ps1'";
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 33602926
You are wrong. Exchange.ps1 does only provide additions useful for the interactive management session. You can issue the new-mailbox command without using that start script; all necessary stuff is included in the psc1 file.
0
 
LVL 1

Author Comment

by:JElster
ID: 33602938
Ok... I don't know anything about Exchange or Powershell..... so you say that I shoudl be abLE
to call

proc.StartInfo.FileName = "powershell.exe -PSConsoleFile 'C:\\Program Files\\Microsoft\\Exchange Server\\bin\\exshell.psc1' -noexit -command NEW-MAILBOX

???
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 33602964
Yes, but if you do, you have to omit the -noexit, and provide ALL parameters needed for new-mailbox, as shown (partitially) in my example.
0
 
LVL 1

Author Comment

by:JElster
ID: 33603144
Ok..
So my batch file looks like this

powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"  -command  %1

How do I pass in the "new-mailbox' with all the parameters enclosed in double quotes?
thanks
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 33603169
I don't know why you want to use a batch file, and in particular I don't know how you generate the necessary parameters for new-mailbox. You need to describe that a little more. Are you calling the batch file from a program, supplying the parameters? If you just can generate a commandline as shown by me above, you do not need a batch file. BTW, that line should not have a double quote before new-mailbox, I put it in in error. You should be fine with

powershell ... -command new-mailbox /* ... */ -Database "Storage Group 1\Mailbox Database 1" /* ... */

(just fill out the ellipsis).
0
 
LVL 1

Author Comment

by:JElster
ID: 33603224
I tried to run the WHOLE  cmd from C# but it doesn't work. I can only get it to work if I invoke it by putting the cmd in batch file.   The batch file works if the new-mailbox is surrounded by double quotes...
I don't understand why I need the double quotes in the batch... The new mail box command was given me by a system admin.. it's  over 300 chararcters long with double quotes and all kinds of crap I have no idea what it does.. has paths etc....
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 33603754
Double quotes are needed if the string contains spaces (and some other chars), because space is a parameter separator. That is a requirement of batch files (both cmd and PowerShell).

C# needs double quotes around the complete command string - that is a requirement of C#.

Both combined makes it more difficult. First, you need to execute the command in a cmd prompt for testing. If that works, put it into a C# string, making each double quote *double*. The above example would then look like:

string cmd = @"powershell ... -command new-mailbox /* ... */ -Database ""Storage Group 1\Mailbox Database 1"" /* ... */";

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 33603798
Is there something I need to do for a colon in the string?  I get the following.. think I'm close.. thx

New-Mailbox : Cannot bind parameter 'LinkedCredential'. Cannot convert the ":" value of type "System.String" to type "System.Management.Automation.PSCredentia l". At line:1 char:189 + new-mailbox -
0
 
LVL 1

Author Comment

by:JElster
ID: 33603901
Also get this... when I enclose the -organizationalunit  in double quotes

New-Mailbox : Cannot convert 'System.Object[]' to the type 'Microsoft.Exchange. Configuration.Tasks.OrganizationalUnitIdParameter' required by parameter 'Organ izationalUnit'. Specified method is not supported.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 33604600
I cannot tell without seeing the part of code you use. There is some issues with named parameters, obviously, as parameters are passed as objects (they should not). Please obfuscate any security related info, but maintain colons, commas and the like.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
If you have come across a situation where you need to find some EDB mailbox recovery techniques, then here you will find the same. In this article, we will take you through three techniques using which you will be able to perform EDB recovery. You …
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
Loops Section Overview

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question