如何让PowerShell函数接收多变量对应的多组参数
解决PowerShell函数批量接收对应参数的问题
问题说明
无法让Get-AdvAppReport函数正确匹配CSV导入的多组参数:单组参数调用正常,但CSV多行时,每个ComputerName会匹配所有ServType,而非CSV中对应的一组参数。
函数参数定义
[CmdletBinding()] Param ( [parameter(Mandatory = $false, ValueFromPipeline = $True)] [string[]]$ComputerName = $env:COMPUTERNAME.ToUpper(), [parameter(Mandatory = $false, ValueFromPipeline = $True)] [string[]]$ServType = ("COM").ToUpper(), [parameter(Mandatory = $false, ValueFromPipeline = $True)] [string[]]$SiteSize = ("STANDARD").ToUpper(), [parameter(Mandatory = $false, ValueFromPipeline = $True)] [string[]]$User = (Read-Host -prompt "Input User Name: ") )
调用场景差异
- 单组参数调用:
Get-AdvAppReport $Computer $ServType $SiteSize可正常运行 - CSV批量调用:导入
$PROD_Serv = Import-CSV ".\Servers_PROD.csv"后,执行Get-AdvAppReport $($PROD_Serv).ComputerName $($PROD_Serv).ServType $($PROD_Serv).SiteSize,多行CSV时参数不对应,每个电脑会匹配所有服务类型
函数核心遍历逻辑
New-HTMLMain -BackgroundColor "#241e69" { foreach ($Computer in $ComputerName) { $Computer = $Computer.ToUpper() Write-Color "`n`nApp Report for ", "$Computer ", "as Type ", "$ServType ", "Initiated...`n`n" -Color Magenta, Cyan, Magenta, Green, Magenta New-HTMLTab -TabName ($Computer + "_" + $ServType) -IconBrands microsoft { New-HTMLTabOptions -SelectorColor "#707cf1" -SelectorColorTarget "#383392" -Transition -LinearGradient -BorderRadius "10px" -FontSize "15" -FontWeight Bolder -FontFamily "Arial" New-HTMLTableStyle -TextAlign center If (Test-Connection -ComputerName $computer -Count 1) { Write-Color "`n $Computer ", "ONLINE. `n" -Color Cyan, Green Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer ONLINE" try { Write-Color "`n $Computer ", "App Report Data Collection ", "In Progress... `n" -Color Cyan, White, Green Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer App Report Data Collection Initiated" Get-AdvAppPreReport $spacer $spacer $spacer New-HTMLText -Text "End of Report" -Color "#f56e78" -fontsize 20 -Alignment Center -FontWeight Bolder -FontFamily Arial } catch { Write-Warning "`n$($Timestamp.Invoke()) - $Computer Ping Status error encountered. - $($_.Exception.Message)" Write-Color "`n $Computer ", "Offline. `n" -Color Cyan, Red New-HTMLText -Text "$Computer Offline." Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer Offline." } } Else { Write-Warning "`n$($Timestamp.Invoke()) - $Computer Ping Status error encountered. - $($_.Exception.Message)" Write-Color "`n $Computer ", "OFFLINE. `n" -Color Cyan, Red Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer Offline." OfflineLog } } } }
解决方案
方案1:修改函数支持管道对象输入(推荐)
调整参数绑定逻辑,让函数直接接收CSV的行对象,保证参数一一对应:
- 修改参数定义
[CmdletBinding()] Param ( [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $True)] [string]$ComputerName = $env:COMPUTERNAME.ToUpper(), [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $True)] [string]$ServType = ("COM").ToUpper(), [parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $True)] [string]$SiteSize = ("STANDARD").ToUpper(), [parameter(Mandatory = $false)] [string]$User = (Read-Host -prompt "Input User Name: ") )
修改点:
- 将参数类型从
string[]改为string,适配单条记录的单个值 - 使用
ValueFromPipelineByPropertyName = $True,自动匹配CSV列名与参数名 - 移除
User参数的管道绑定,避免不必要的输入干扰
- 简化函数内部遍历逻辑
因为管道会逐行调用函数,无需再遍历ComputerName数组,直接使用单个参数值:
New-HTMLMain -BackgroundColor "#241e69" { $Computer = $ComputerName.ToUpper() Write-Color "`n`nApp Report for ", "$Computer ", "as Type ", "$ServType ", "Initiated...`n`n" -Color Magenta, Cyan, Magenta, Green, Magenta New-HTMLTab -TabName ($Computer + "_" + $ServType) -IconBrands microsoft { New-HTMLTabOptions -SelectorColor "#707cf1" -SelectorColorTarget "#383392" -Transition -LinearGradient -BorderRadius "10px" -FontSize "15" -FontWeight Bolder -FontFamily "Arial" New-HTMLTableStyle -TextAlign center If (Test-Connection -ComputerName $computer -Count 1) { Write-Color "`n $Computer ", "ONLINE. `n" -Color Cyan, Green Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer ONLINE" try { Write-Color "`n $Computer ", "App Report Data Collection ", "In Progress... `n" -Color Cyan, White, Green Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer App Report Data Collection Initiated" Get-AdvAppPreReport $spacer $spacer $spacer New-HTMLText -Text "End of Report" -Color "#f56e78" -fontsize 20 -Alignment Center -FontWeight Bolder -FontFamily Arial } catch { Write-Warning "`n$($Timestamp.Invoke()) - $Computer Ping Status error encountered. - $($_.Exception.Message)" Write-Color "`n $Computer ", "Offline. `n" -Color Cyan, Red New-HTMLText -Text "$Computer Offline." Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer Offline." } } Else { Write-Warning "`n$($Timestamp.Invoke()) - $Computer Ping Status error encountered. - $($_.Exception.Message)" Write-Color "`n $Computer ", "OFFLINE. `n" -Color Cyan, Red Add-Content $LogFile -Value "`n$($Timestamp.Invoke()) - $Computer Offline." OfflineLog } } }
- 管道方式调用函数
$PROD_Serv = Import-CSV ".\Servers_PROD.csv" $PROD_Serv | Get-AdvAppReport
方案2:不修改函数,循环调用
遍历CSV的每一行,单独传递对应参数给函数:
$PROD_Serv = Import-CSV ".\Servers_PROD.csv" foreach ($item in $PROD_Serv) { Get-AdvAppReport -ComputerName $item.ComputerName -ServType $item.ServType -SiteSize $item.SiteSize }
原调用出错原因
原调用Get-AdvAppReport $($PROD_Serv).ComputerName $($PROD_Serv).ServType $($PROD_Serv).SiteSize会将所有电脑名合并为一个数组传给ComputerName,所有服务类型合并为一个数组传给ServType。函数内部遍历ComputerName时,每次都会使用完整的ServType数组,导致每个电脑匹配所有服务类型,而非对应行的服务类型。
内容的提问来源于stack exchange,提问作者ComputerRecon
相关产品推荐
相关产品推荐

