powershell - 从 Invoke-Command 返回变量
问题描述
我正在尝试将 Invoke-Command 中的结果数据输出到 .csv 文件,但运气不佳。这是我所拥有的:
$output= @()
ForEach ($server in $servers) {
Invoke-Command -ComputerName $server -ScriptBlock {
param($server_int, $output_int)
If((Start-Process "c:\temp\installer.exe" -ArgumentList "/S" -Wait -Verb RunAs).ExitCode -ne 0) {
$output_int += "$server_int, installed successfully"
} else {
$output_int += "$server_int, install failed"
}
} -ArgumentList $server, $output
}
$output | Out-file -Append "results.csv
"
据我了解, $output_int 仅在 Invoke-Command 会话中可用。如何检索此 $output_int 变量并将其值添加到我的 .csv 文件中?
非常感谢!
解决方案
使用Write-Output
cmdlet,并将调用保存到$output
数组中...
试试这个:
$output = @()
ForEach ($server in $servers) {
$Output += Invoke-Command -ComputerName $server -ScriptBlock {
param($server_int)
If((Start-Process "c:\temp\installer.exe" -ArgumentList "/S" -Wait -Verb RunAs).ExitCode -ne 0) {
Write-Output "$server_int, installed successfully"
} else {
Write-Output "$server_int, install failed"
}
} -ArgumentList $server
}
$output | Out-file -Append "results.csv"
推荐阅读
- python - 在 python 或 tableau 的帮助下合并数据
- c# - 使用 Moq 模拟 ReturnsAsync 以返回 2 个值
- java - 如何在普通 Spring 微服务应用程序(非 Spring 启动)中跟踪请求?
- jira - JIRA java API - 如何获取自定义字段的值列表
- python - 如何通过链接两个列表来运行循环?
- javascript - 将活动类数据存储在浏览器的 localStorage 中
- node.js - Node.js:mongoose.once('open') 不执行回调函数
- azure - 适用于应用服务计划和 Application Insights 的 Azure 灾难恢复
- excel-formula - 基于 snother 单元格的条件格式
- android - 每当有人在 Android Studio firebase 中发送消息时,如何防止我的聊天内容重复