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?
-
Do you want the output of the log file or the output of the script?zdan– zdan2013-10-04 01:04:10 +00:00Commented Oct 4, 2013 at 1:04
-
I want the output of command add-windowsfeature piping to this script, which writes to the logfileNaz– Naz2013-10-04 16:18:15 +00:00Commented Oct 4, 2013 at 16:18
Add a comment
|
1 Answer
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.
2 Comments
Naz
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
Mike Shepard
Does this work: $stuff.ToString() | out-file c:\logs\logfile.txt