【发布时间】:2011-06-22 20:27:28
【问题描述】:
我有一组包含“通用”脚本的 PowerShell 脚本,位于同一文件夹中,如下所示:
# some-script.ps1
$scriptDir = Split-Path -Parent $myinvocation.mycommand.path
. "$scriptDir\script-utils.ps1"
如果直接调用脚本就可以了,例如
.\some-script.ps1
但是,如果使用 Invoke-Command 调用脚本,这将不起作用:
Invoke-Command -ComputerName server01 -FilePath "X:\some-script.ps1"
在这种情况下,实际上$myinvocation.mycommand 包含脚本的内容,而$myinvocation.mycommand.path 为空。
当使用 Invoke-Command 调用脚本时,如何确定脚本的目录?
注意
最后,这是我实际使用的解决方案:
Invoke-Command -ComputerName server01 `
{param($scriptArg); & X:\some-script.ps1 $scriptArg } `
-ArgumentList $something
这也允许将参数传递给脚本。
【问题讨论】:
-
任何有完整源代码的解决方案?