从文件导入 Scriptblock
Importing Scriptblock from file
我有一个有效的 Powershell 脚本,我想从外部文件中提取脚本块。
工作:
$scriptblock = { ... }
invoke-command -ComputerName $server -ScriptBlock $Scriptblock -ArgumentList $server,$team -Credential $credential -asjob -JobName Dashboard_$server -SessionOption (New-PSSessionOption -NoMachineProfile)
"get-job -id | receive-job" 的输出很好
不工作:
# Generate scriptblock from file
$file = Get-Content E:\Dashboard\Windows\winrm_scriptblock.txt
$Scriptblock = $executioncontext.invokecommand.NewScriptBlock($file)
invoke-command -ComputerName $server -ScriptBlock $Scriptblock -ArgumentList $server,$team -Credential $credential -asjob -JobName Dashboard_$server -SessionOption (New-PSSessionOption -NoMachineProfile)
"get-job -id | receive-job" 的输出为空
winrm_scriptblock.txt的内容正是工作版本中定义的脚本块变量中大括号中包含的内容。
如有任何帮助,我们将不胜感激。
与How do I pass a scriptblock as one of the parameters in start-job
的答案非常相关
如果您将字符串 "Get-ChildItem C:\temp" 存储在文件 "E:\Dashboard\Windows\winrm_scriptblock.txt" 中,则此代码应输出本地计算机上文件夹 "C:\temp" 的内容。
Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content "E:\Dashboard\Windows\winrm_scriptblock.txt")))
参数
就传递参数而言 Pass arguments to a scriptblock in powershell covers that answer as well. As Keith Hill 指出:脚本块只是一个匿名函数
考虑以下文件内容
param(
$number
)
$number..2 | ForEach-Object{
Write-Host "$_ lines of code in the file."
}
和命令
Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content "E:\Dashboard\Windows\winrm_scriptblock.txt"))) -ArgumentList "99"
会给你
烦人的输出
99 lines of code in the file.
98 lines of code in the file.
97 lines of code in the file.
....
有什么理由不只使用 Invoke-Command 的 -FilePath 参数吗?
我知道您已经有了答案,但是从脚本文件获取脚本块的另一种方法是使用 get-command
cmdlet:
$sb=get-command C:\temp\add-numbers.ps1 | select -ExpandProperty ScriptBlock
$sb 现在是脚本的脚本块。
您必须从 E:\Dashboard\Windows\winrm_scriptblock.txt
中提取 {}
我有一个有效的 Powershell 脚本,我想从外部文件中提取脚本块。
工作:
$scriptblock = { ... }
invoke-command -ComputerName $server -ScriptBlock $Scriptblock -ArgumentList $server,$team -Credential $credential -asjob -JobName Dashboard_$server -SessionOption (New-PSSessionOption -NoMachineProfile)
"get-job -id | receive-job" 的输出很好
不工作:
# Generate scriptblock from file
$file = Get-Content E:\Dashboard\Windows\winrm_scriptblock.txt
$Scriptblock = $executioncontext.invokecommand.NewScriptBlock($file)
invoke-command -ComputerName $server -ScriptBlock $Scriptblock -ArgumentList $server,$team -Credential $credential -asjob -JobName Dashboard_$server -SessionOption (New-PSSessionOption -NoMachineProfile)
"get-job -id | receive-job" 的输出为空
winrm_scriptblock.txt的内容正是工作版本中定义的脚本块变量中大括号中包含的内容。
如有任何帮助,我们将不胜感激。
与How do I pass a scriptblock as one of the parameters in start-job
的答案非常相关如果您将字符串 "Get-ChildItem C:\temp" 存储在文件 "E:\Dashboard\Windows\winrm_scriptblock.txt" 中,则此代码应输出本地计算机上文件夹 "C:\temp" 的内容。
Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content "E:\Dashboard\Windows\winrm_scriptblock.txt")))
参数
就传递参数而言 Pass arguments to a scriptblock in powershell covers that answer as well. As Keith Hill 指出:脚本块只是一个匿名函数
考虑以下文件内容
param(
$number
)
$number..2 | ForEach-Object{
Write-Host "$_ lines of code in the file."
}
和命令
Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content "E:\Dashboard\Windows\winrm_scriptblock.txt"))) -ArgumentList "99"
会给你
烦人的输出99 lines of code in the file.
98 lines of code in the file.
97 lines of code in the file.
....
有什么理由不只使用 Invoke-Command 的 -FilePath 参数吗?
我知道您已经有了答案,但是从脚本文件获取脚本块的另一种方法是使用 get-command
cmdlet:
$sb=get-command C:\temp\add-numbers.ps1 | select -ExpandProperty ScriptBlock
$sb 现在是脚本的脚本块。
您必须从 E:\Dashboard\Windows\winrm_scriptblock.txt
中提取 {}