gpt4 book ai didi

powershell - 从文件导入脚本 block

转载 作者:行者123 更新时间:2023-12-03 22:52:33 26 4
gpt4 key购买 nike

我有一个有效的 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 的内容正是包含在工作版本中定义的 scriptblock 变量中的大括号之间的内容。

任何帮助表示赞赏。

最佳答案

我知道您已经有了答案,但是从脚本文件中获取脚本块的另一种方法是使用 get-command小命令:

$sb=get-command C:\temp\add-numbers.ps1 | select -ExpandProperty ScriptBlock 

$sb 现在是脚本的脚本块。

关于powershell - 从文件导入脚本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com