Solved

Help with PowerShell script/batch file

Posted on 2010-09-03
15
1,106 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
 
LVL 68

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 68

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

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 68

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 68

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 68

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

Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

Join & Write a Comment

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
This video discusses moving either the default database or any database to a new volume.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now