PS 5.1.22621.4391(我被它困住了)
有时我需要运行这个:
plink -t -load "$Session" -batch -m $ShellFile
但有时我需要添加另一个选项:
plink -t -load "$Session" -batch -m $ShellFile -pw $SshPw
这看起来太黑客了,但效果不好:
if ($SshPw)
{ plink -t -load "$Session" -batch -m $ShellFile -pw $SshPw }
else
{ plink -t -load "$Session" -batch -m $ShellFile }
我尝试将命令构建为字符串,然后通过执行&
,但失败了,因为显然&
处理的只是程序名称。
PS C:\Users> $X="plink -t -load `"$Session`" -batch -m $ShellFile"
PS C:\Users> $X
plink -t -load "FIS-Q-LBX-PGS-101-a" -batch -m C:\<somedir>\daily.txt
PS C:\Users> &$X
& : The term 'plink -t -load "FIS-Q-LBX-PGS-101-a" -batch -m C:\<somedir>\daily.txt' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:2
+ &$X
+ ~~
+ CategoryInfo : ObjectNotFound: (plink -t -load ...\Temp\daily.txt:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
有解决方法吗?我的 Google 能力不够强。
使用splatting通过字典传递参数
查看示例和about_Splatting
编辑: 使用字典进行 Splatting 不适用于本机应用程序,您需要使用数组进行 Splatting:
或者您可以使用也接收数组
-ArgumentList
的选项:Start-Process
我会使用启动过程来更好地控制。类似
并且 plink 的文字路径会更好,因此如果它在脚本目录中 .\plink.exe
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.5
编辑:由于我仍然认为这是正确的,OP 根本没有从各个角度考虑,解决上述错误在于如何使用它,使用已经提供的示例(使用记事本,因为我没有在这个系统上使用 plink)调用仍然可以发生。