【问题标题】:How to use Argument to get path file as an input data to other script in PowerShell?如何使用 Argument 获取路径文件作为 PowerShell 中其他脚本的输入数据?
【发布时间】:2025-11-27 01:50:02
【问题描述】:

我想读取 2 个文本文件(Byte2.txt 和 FB.txt),但我不想将路径放在脚本上,我想在从命令行来解析我的脚本的路径。我的期望,当我从命令行运行脚本时:

 $PowerShell.exe Script.ps1 arg1 arg2

arg1代表Byte.txt的路径,arg2代表FB.txt的路径

这就是我所做的。

$file = $Args[0]
$Data = $Args [1]
$file_2 = Get-Content " "
$file_2
$Data_2 = Get-content " "
$Data_2

我从 cmd 执行该脚本:

$PS.exe Script.ps1 C:\User\Byte.txt C:\User\FB.txt

【问题讨论】:

  • 请提供更好的解释。我很难理解你在追求什么。你的意思是你想要一行代码来显示它从哪里运行? (Get-PSCallStack)[0].InvocationInfo.MyCommand.Source 会给你脚本的完整路径

标签: powershell arguments


【解决方案1】:

效果很好

Param(
      [parameter(mandatory=$true)][string]$arg1, $arg2
    )

    $file= Get-Content $arg1
    $file

    $Data= Get-Content $arg2
    $Data

从 cmd 执行:

$PS.exe Script.ps1 -arg1 C:\User\Byte.txt -arg2 C:\User\FB.txt

【讨论】: