How to specify input and output paths from cmd.exe

2019-07-12 18:53发布

问题:

I'm a beginner with PowerShell, and a total newb with the command line. I have a PowerShell script that I want others to run via cmd.exe. It looks like this:

 Get-Content $i | (Do stuff with strings) | Out-file $o

Where "i" and "o" are string variables for input and output I want to be chosen by users. I made a batch file and it all works as intended, running from cmd. My problem is that I want users to be able to specify their input and output paths from the command line, without opening PowerShell. How can I do this?

回答1:

Maybe in batch do...

set /p IN=[Enter an input path]
set /p OUT=[Enter an output path]

then call your PS script with the 2 variables on the end

yourscript.ps1 %IN% %OUT%

Then in your PS script set it up to take some parameters...

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
  [string]$INpath,

  [Parameter(Mandatory=$True,Position=2)]
  [string]$OutPath
)

Get-Content $INpath | (Do stuff with strings) | Out-file $OutPath

That might help you