【问题标题】:Using current directory as output file for Powershell [duplicate]使用当前目录作为 Powershell 的输出文件 [重复]
【发布时间】:2021-06-07 18:00:13
【问题描述】:

我正在尝试让 powershell 将命令中的输出文件吐出到当前文件夹,因为我正在尝试创建可以从 USB 记忆棒运行的打印机迁移。

我的命令很简单:C:\Windows\System32\spool\tools\Printbrm.exe /b /f C:\Temp\Print

但我希望将文件保存到运行脚本的目录中,因为 USB 路径在我们组织中的不同计算机上会发生变化。

有人能帮忙吗?我猜这很简单,但无法弄清楚。

【问题讨论】:

  • $PSScriptRoot,返回脚本保存/运行的路径。

标签: powershell


【解决方案1】:

可以从$MyInvocation获取包含执行脚本的路径:

$scriptFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent

如果你想要调用它的文件夹(因为标题是“当前目录”):

$currentFolder = (Get-Location).Path

运行您的命令并让它转储脚本运行的输出:

$scriptFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent
$printArgs = @("/b", "/f", $scriptFolder)
& "C:\Windows\System32\spool\tools\Printbrm.exe" $printArgs

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
相关资源
最近更新 更多