Solved

Help with PowerShell script/batch file

Posted on 2010-09-03
15
1,113 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 69

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 69

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 69

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 69

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 69

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 69

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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