Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1204
  • Last Modified:

Help with PowerShell script/batch file

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
JElster
Asked:
JElster
1 Solution
 
athomsfereCommented:
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
 
sunnyc7Commented:
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
 
JElsterAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
JElsterAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
JElsterAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
JElsterAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
JElsterAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
JElsterAuthor Commented:
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
 
JElsterAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

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