0

I have a script that writes output to a log file and also the console. I am running the command Add-WindowsFeatures... I want to take output of this command and pipe it to my script. Is it possible?

2
  • Do you want the output of the log file or the output of the script? Commented Oct 4, 2013 at 1:04
  • I want the output of command add-windowsfeature piping to this script, which writes to the logfile Commented Oct 4, 2013 at 16:18

1 Answer 1

4

Absolutely. You just need to include the CmdletBinding attribute on your param statement. Then, add an attribute to one of your parameters which details the way the pipeline input binds to the parameter. For instance put this in c:\temp\get-extension.ps1:

[CmdletBinding()]
Param(
[parameter(Mandatory=$true,
            ValueFromPipeline=$true)][System.IO.FileInfo[]]$file
)

process {
  $file.Extension
}

Then, you can do this:

dir -File| C:\temp\get-extension.ps1

updating to address the latest comment: I'm guessing that setting the parameter type to [object[]]$stuff rather than [fileinfo[]] and putting

$stuff | out-file c:\logs\logfile.txt  #or wherever you want

in the process block will get you close.

Sign up to request clarification or add additional context in comments.

2 Comments

but when I do add-windowsfeatures| c:\....ps1, I just see microsoft.Windows.ServerManager.Commands.FeatureOperationResult in my log file. I am not seeing output from this command
Does this work: $stuff.ToString() | out-file c:\logs\logfile.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.