1

问题

有谁知道获取与 $MyInvocation 相同数据的方法?强烈的偏好是只使用内置的东西而不是第三方添加软件来实现这一点。

问题

当您使用 -NoMachineProfile 创建会话时,$MyInvocation 不会被填充。我希望有其他方法来获取信息,就像 [System.Environment]::Username 用于 $env:USERNAME 一样。

我无法改变调用事物的方式。请不要建议删除 -NoMachineProfile 作为解决方案,因为这不是一个选项。

我不想依赖 $args 因为那是后处理。即引号、变量和其他传递的参数不再与它们传递时的相同。

背景

$MyInvocation.line 包含提供 0 解析的确切命令。

例如 SomeScript.ps1

write-host ("Invoked line: "+$MyInvocation.line)

.\somescript.ps1 foo bar "foo and bar" 产生以下输出

Invoked line: .\somescript.ps1 foo bar "foo and bar"

引号和所有,这意味着它能够被剪切和粘贴,并且在 0 用户翻译的情况下完全以相同的方式运行。

-NoMachineProfile 调用

$SE_ADMIN_CREDENTIALS = Get-Credential
$PS_SESSION_OPTIONS = New-PSSessionOption -NoMachineProfile
$arrayRemoteTarget = "thehostname"
$strScriptToRun = ".\somescript.ps1"
$StrArguments = "arg1,arg2,arg3"

$icmCommand = "Invoke-Command -Credential `$SE_ADMIN_CREDENTIALS -SessionOption `$PS_SESSION_OPTIONS -ComputerName `$arrayRemoteTarget -FilePath `$strScriptToRun -ArgumentList (`$strArguments).Split("","")"

Invoke-Expression $icmCommand
4

1 回答 1

0
-sessionoption (new-pssessionoption -nomachineprofile)

是另一种方法。

至于传递论点,我相信

$using:variable

是你要找的

例如

$listing = "*"
invoke-command -computername $computer -scriptblock {cd /; ls $using:listing}

我希望这是有道理的。

否则你可以在文件中使用“arg[0]”或“args[0]”来运行脚本......应该可以......

最后一点,你的引号,可能会弄乱你的脚本,我只是替换split("","")split(',')

于 2018-09-09T21:26:23.610 回答