Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Powershell Wrapper Script Problems - Trying to Call PowershellExchange 2007 Commands from Secondary Language (Like VBScript)

Reply
 
Old 09-03-2008   #1 (permalink)
Nathan


 
 

Powershell Wrapper Script Problems - Trying to Call PowershellExchange 2007 Commands from Secondary Language (Like VBScript)

I'm trying to create a wrapper script in powershell that will execute
any commands I pass to it as arguments after loading all of the
appropriate Exchange based plugins. This is so that I can execute
powershell commands from other programming and scripting languages
like VBScript and simply parse the results that powershell pukes back.

Creating the script seemed super straight forward at first. I simply
called the "&" symbol to execute the $args array and it seemed to work
the very first time. I was psyched that it worked the very first time:

c:\scripts\lib_exchange_generic.ps1

& $args

Well that worked fine as long as I didn't pass any command line
switches. For example this works just fine:

get-MailboxStatistics

however this did not:

get-MailboxStatistics test.user

It returned the error:

Get-MailboxStatistics test.user
'Get-MailboxStatistics test.user' is not recognized as a cmdlet,
function,
operable program, or script file. Verify the term and try again.
At c:\scripts\powershell.ps1:26 cha
r:2

Bummer. So thinking that it's trying to parse the entire string as a
command name I setup a for loop to try to create a string for the
command, and then one for the command line switches. Hey this works
great:


c:\scripts\lib_exchange_generic.ps1

$checkfirst = 0
$argsstring = ""
foreach ($additionalarg in $args) {
if ( $checkfirst -gt 0 ) {
if ( $checkfirst -eq 1) {
$argsstring = $additionalarg
}
else {
$argstring = $argsstring + " " + $additionalarg
}
#write-host "inside loop"
}
#write-host "1:" + $additionalarg
#write-host "2:" + $argsstring
#write-host "3:" + $checkfirst
$checkfirst++
}

#write-host "Final Command:"
#write-host $args[0] $argsstring

& $args[0] $argsstring



Except for when I try to pass a switch to the command:

Get-MailboxStatistics -Identity test.user

The specified mailbox "-Identity" does not exist.
At c:\scripts\powershell.ps1:24 cha
r:2


So does anyone have any advice to get this thing working as expected?
Powershell really has some weird behavior and I'm not really sure how
to proceed. Am I just approaching this problem wrong? Is there a
better way?

Thanks in advance.

-Nate
http://www.naterice.com

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Call binary to return value in Powershell script PowerShell
How to call PowerShell script from C# PowerShell
function call from within powershell script PowerShell
Call powershell script via Batch-File PowerShell
Call Powershell script from ASP.NET page? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46