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.
You can try the following with your exchange commands:
# iex is the alias for Invoke-Expression
function Invoke-GenericCommand { iex "$args" }
For instance:
PS > Invoke-GenericCommand dir -filter *.txt
Name
----
file1.txt
file2.txt
file3.txt
Or even with piping:
PS > Invoke-GenericCommand Get-Process a* | select name,id | ft -auto
Name Id
---- --
agent 7444
alg 2276
BTW, there is a great ActiveX free tool (from Sapien technologies) that you
can utilize, it's called 'ActiveX PowerShell (ActiveXPosh)' and you can get
it here:
http://www.primalscript.com/Free_Tools/index.asp
---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
N> I'm trying to create a wrapper script in powershell that will execute
N> any commands I pass to it as arguments after loading all of the
N> appropriate Exchange based plugins. This is so that I can execute
N> powershell commands from other programming and scripting languages
N> like VBScript and simply parse the results that powershell pukes
N> back.
N>
N> Creating the script seemed super straight forward at first. I simply
N> called the "&" symbol to execute the $args array and it seemed to
N> work the very first time. I was psyched that it worked the very first
N> time:
N>
N> c:\scripts\lib_exchange_generic.ps1
N>
N> & $args
N>
N> Well that worked fine as long as I didn't pass any command line
N> switches. For example this works just fine:
N>
N> get-MailboxStatistics
N>
N> however this did not:
N>
N> get-MailboxStatistics test.user
N>
N> It returned the error:
N>
N> Get-MailboxStatistics test.user
N> 'Get-MailboxStatistics test.user' is not recognized as a cmdlet,
N> function,
N> operable program, or script file. Verify the term and try again.
N> At c:\scripts\powershell.ps1:26 cha
N> r:2
N> Bummer. So thinking that it's trying to parse the entire string as a
N> command name I setup a for loop to try to create a string for the
N> command, and then one for the command line switches. Hey this works
N> great:
N>
N> c:\scripts\lib_exchange_generic.ps1
N>
N> $checkfirst = 0
N> $argsstring = ""
N> foreach ($additionalarg in $args) {
N> if ( $checkfirst -gt 0 ) {
N> if ( $checkfirst -eq 1) {
N> $argsstring = $additionalarg
N> }
N> else {
N> $argstring = $argsstring + " " + $additionalarg
N> }
N> #write-host "inside loop"
N> }
N> #write-host "1:" + $additionalarg
N> #write-host "2:" + $argsstring
N> #write-host "3:" + $checkfirst
N> $checkfirst++
N> }
N> #write-host "Final Command:"
N> #write-host $args[0] $argsstring
N> & $args[0] $argsstring
N>
N> Except for when I try to pass a switch to the command:
N>
N> Get-MailboxStatistics -Identity test.user
N>
N> The specified mailbox "-Identity" does not exist.
N> At c:\scripts\powershell.ps1:24 cha
N> r:2
N> So does anyone have any advice to get this thing working as expected?
N> Powershell really has some weird behavior and I'm not really sure how
N> to proceed. Am I just approaching this problem wrong? Is there a
N> better way?
N>
N> Thanks in advance.
N>
N> -Nate
N> http://www.naterice.com
^^ Holy crap this worked. ^^
You sir are a genius.
Thank you very much.
On Sep 4, 4:34 am, Shay Levy [MVP] <n...@addre.ss> wrote:
> Hello Nathan,
>
> You can try the following with your exchange commands:
>
> # iex is the alias for Invoke-Expression
> function Invoke-GenericCommand { iex "$args" }
>
> For instance:
>
> PS > Invoke-GenericCommand dir -filter *.txt
>
> Name
> ----
> file1.txt
> file2.txt
> file3.txt
>
> Or even with piping:
>
> PS > Invoke-GenericCommand Get-Process a* | select name,id | ft -auto
>
> Name Id
> ---- --
> agent 7444
> alg 2276
>
> BTW, there is a great ActiveX free tool (from Sapien technologies) that you
> can utilize, it's called 'ActiveX PowerShell (ActiveXPosh)' and you can get
> it here:
>
> http://www.primalscript.com/Free_Tools/index.asp
>
> ---
> Shay Levy
> Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic