Solved

Help with PowerShell script/batch file

Posted on 2010-09-03
15
1,124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 70

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 70

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 70

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 70

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 70

Accepted Solution

by:
Qlemo earned 500 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 70

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

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This video discusses moving either the default database or any database to a new volume.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

737 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