Powershell使用-NoMachineProfile调用命令如何获得传递的参数

Powershell使用-NoMachineProfile调用命令如何获得传递的参数,powershell,invoke-command,Powershell,Invoke Command,问题 $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 = 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
有人知道一种方法可以获得与$MyInvocation相同的数据吗?强烈的偏好是只使用内置的东西,而不是第三方附加软件来实现这一点

问题

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

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

我不想依赖$args,因为这是后处理。也就是说,引号、变量和其他传递的参数不再与传递时相同

背景

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

e、 g。 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
这是另一种方法

至于传递论点,我相信

$using:variable
这就是你要找的

e、 g

我希望这是有道理的

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

最后一个注意事项是,您在引号中的引号可能会弄乱您的脚本,我只是将
split(“,”)
替换为
split(“,”)

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