无法过滤并仅获取 .ps1 脚本执行的返回变量

问题描述 投票:0回答:1
$scriptPath = "${{ github.action_path }}\get_details.ps1"
$ServerName = "remoteserver777"

$returnArray = & "$scriptPath" -mypass ${{ inputs.mypass }} -myuser ${{ inputs.myuser }} -ServerName $ServerName

Write-Host "Print return data- $returnArray" 

.ps1 返回

cmdb_ci_linux_server,2,8.9,Test,10.130.68.70
,这就是我想要的;但是当我打印
$returnArray
时,它有 $ServerName
remoteserver777
作为参数传递给 .ps1 脚本。

电流输出:

Print return data- remoteserver777 cmdb_ci_linux_server,2,8.9,Test,10.130.68.75

所需输出:

Print return data- cmdb_ci_linux_server,2,8.9,Test,10.130.68.75

然后,我尝试仅过滤

Select-Object -ExpandProperty Output
并通过尝试以下操作来获取 .ps1 脚本
return $returnValues
返回的输出:

Invoke-Expression -Command & 'D:\Git-Runners\s1_work_actions\newfeature\getdetails\get_details.ps1' -mypass *** -myuser svc_AMS_AppReliabilityEng -ServerName remoteserver777 | Select-Object -ExpandProperty Output
Select-Object : Property "Output" cannot be found.

请推荐。

windows powershell filter return
1个回答
0
投票

一种方法是使用

$ServerName
函数从返回值中提取
split

$scriptPath = "${{ github.action_path }}\get_details.ps1"
$ServerName = "remoteserver777"

$returnArray = & "$scriptPath" -mypass ${{ inputs.mypass }} -myuser ${{ inputs.myuser }} -ServerName $ServerName

# Extracts $ServerName from $returnData
$returnData = $returnArray.Split(' ')[1..($returnArray.Split(' ').Count - 1)] -join ' '

Write-Host "Print return data- $returnData"

这会产生以下输出:

Print return data- cmdb_ci_linux_server,2,8.9,Test,10.130.68.75
© www.soinside.com 2019 - 2024. All rights reserved.