I didn't say it was flawed. I said YUCK. :-) See below.

I didn't use Wait-Process below because I want the exit value.

The process below also lets the process object handle shell escape and stuff.

I have an entire module I use for process handling, output redirection, etc. 
etc. It's part of my updated CoreConfigurator rewrite in PowerShell. I'll 
release it one day.

function New-NativeBinary
{
        $filename, $arguments = $args

        $process = Start-Process -FilePath $filename -Argument $arguments 
-NoNewWindow `
                -RedirectStandardError $script:stderrFile 
-RedirectStandardOutput $script:stdoutFile -PassThru

        if( $process )
        {
                $process.WaitForExit()
                $exit = $process.ExitCode
                if( $exit -eq $null )
                {
                        $exit = 0
                }

                $process.Close()
                $process.Dispose()
        }
        else
        {
                Write-Error "New-NativeBinary: the process did not start"
        }
}

-----Original Message-----
From: Joseph L. Casale [mailto:[email protected]] 
Sent: Wednesday, May 30, 2012 7:06 PM
To: NT System Admin Issues
Subject: RE: Powershell question

Actually, you are right about the -verbose, I forgot about that and simply 
opened up the first script.
CmdletBinding enables all cmdlets to produce the output if they provide...

I am not following you on the & operator for the Some-Cmdlet example. I did try 
invoke-expression without luck, if its the right way I will revisit it and 
figure out what I did wrong.

As for running a binary, why is my method flawed (brief idea here)?

$Args = New-Object System.Collections.ArrayList
    [void]$Args.Add("--log-file=path")
    [void]$Args.Add("--recursive")
    ...
    If ($PSBoundParameters['Verbose'])
    {
        [void]$Args.Add("--verbose")
        [void]$Args.Add("--stats")
    }
    If ($Checksum)
    {
        [void]$Args.Add("--checksum")
    }

$Process = New-Object System.Diagnostics.Process $Process.StartInfo.FileName = 
"$BinPath\rsync.exe"
$Process.StartInfo.Arguments = $Args


This works pretty well and takes care of a *myriad* of shell escaping as 
System.Diagnostics.Process does all the heavy lifting?

Thanks!
jlc

________________________________________
From: Michael B. Smith [[email protected]]
Sent: Wednesday, May 30, 2012 4:41 PM
To: NT System Admin Issues
Subject: RE: Powershell question

You need to take a look at the & operator and the invoke-command and 
invoke-expression cmdlets.

Insofar as "push the extra arg into the array list" YUCK. Check out 
Start-Process and Wait-Process.

The way you are using Verbose isn't the way it's MEANT to be used (although 
what you are doing certainly does work). All you need to do is set 
CmdletBinding as the master attribute on the parameter list.

-----Original Message-----
From: Joseph L. Casale [mailto:[email protected]]
Sent: Wednesday, May 30, 2012 4:55 PM
To: NT System Admin Issues
Subject: Powershell question

I often have scripts I write that involve passing potentially different sets of 
commands to cmdlets based on switches I pass to the script. Some provide built 
in means to make it
easy:

     -Verbose:($PSBoundParameters['Verbose'] -eq $true)

In the case where I need to invoke a binary, I push the extra arg into the 
array list passed to the System.Diagnostics.Process job as the arglist. Works 
well.

If I am simply running a cmdlet:

     Some-Cmdlet `
       -Param1 xxx `
       -Param2 xxx

If I tack another back tick on param2, what powershell foo allows a conditional 
3rd param to be passed? Would be so much tidier than if/else and duplicating 
the whole thing.

Thanks!
jlc
~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ 
<http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to [email protected]
with the body: unsubscribe ntsysadmin


~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ 
<http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to [email protected]
with the body: unsubscribe ntsysadmin


~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ 
<http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to [email protected]
with the body: unsubscribe ntsysadmin


~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to [email protected]
with the body: unsubscribe ntsysadmin

Reply via email to