要点: 我试图用更少的冗余来构建我的脚本。问题是我必须为每个交换机重复一个功能,尽管它们要到同一个目的地并完成相同的任务。
问题: 是否可以采用开关es age,名称和专业并将其转换为单一功能?
旁注: 我正在使用一个psobject并存储它的URI,将其枚举并将$ Search变量实例化为为psobject中的每个成员指定的URI。
function Search-PersonnelData {
param(
[switch]$Name,
[switch]$Age,
[switch]$Profession,
[switch]$All,
[switch]$Less,
[switch]$Count,
[string]$Search,
[string]$Limit
)
if ($Limit.Length -eq 0 -or ($Limit -match $invalidnumber))
{
$Limit = 15
Write-Warning -Message 'Invalid limit specified, setting to 15 by default'
}
# HERE IS WHERE I AM REPEATING THE SAME FUNCTION OVER
# Name Query
if($Name)
{
$searchTerms = Get-QueryType -userinput Name
$Results = Invoke-RestMethod -Method Get -Uri $apiUrl$searchTerms"?term=$Search&limit=$Limit" -WebSession $currentsession
}
# Age Query
if($Age)
{
$searchTerms = Get-QueryType -userinput Age
$Results = Invoke-RestMethod -Method Get -Uri $apiUrl$searchTerms"?term=$Search&limit=$Limit" -WebSession $currentsession
}
# Profession Query
if($Profession)
{
$searchTerms = Get-QueryType -userinput 'Profession'
$Results = Invoke-RestMethod -Method Get -Uri $apiUrl$searchTerms"?term=$Search&limit=$Limit" -WebSession $currentsession
}
##########################Here is where there repeition ends###############################
if($Results.DataBase.UUID.Count -eq 0)
{
Write-Warning -Message 'No results found'
break
}
if($SignatureCount -eq $true)
{
$Results.DataBase| Group-Object | Select-Object -Property count,name -ExcludeProperty RunspaceId
}
if($Less -eq $true)
{
$Results.DataBase | Select-Object -Property $TableFormat -ExcludeProperty RunspaceId
}
if($All -eq $true)
{
$Results.DataBase | Select-Object -Property $Gridtable -ExcludeProperty RunspaceId
}
if($Count -eq $false -and ($less -eq $false)-and ($all -eq $false))
{
$Results.DataBase| Select-Object -Property $TableFormat | Format-Table -AutoSize
$Results.DataBase | Group-Object | Select-Object -Property count,name -ExcludeProperty RunspaceId | Format-Table -AutoSize
}
}
$EndPoints = New-Object psobject
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name 'Profession' -Value "by_profession"
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name 'Age' -Value "by_age"
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name Name -Value "by_Name"
function Get-QueryType {
param(
$userinput
)
$EndPoints.psobject.Members |
Where-Object {$_.Name -eq $userinput } |
? {$_.Membertype -eq "noteproperty"} |
%{ $_.Value }
}
答案 0 :(得分:3)
所以我建议你使用哈希表进行查找:
function Search-PersonnelData {
param(
[ValidateSet('Name', 'Age', "Profession")]
[string]$queryType,
...
)
...
$hashtable = @{
"Name" = "by_name"
"Age" = "by_age"
"Profession" = "by_profession"
}
$searchTerms = $hashtable[$queryType]
$Results = Invoke-RestMethod -Method Get -Uri $apiUrl$searchTerms"?term=$Search&limit=$Limit" -WebSession $currentsession
...
}
编辑,忘记添加,我完全删除了所有这些代码:
$EndPoints = New-Object psobject
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name 'Profession' -Value "by_profession"
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name 'Age' -Value "by_age"
Add-Member -InputObject $EndPoints -MemberType NoteProperty -Name Name -Value "by_Name"
function Get-QueryType {
param(
$userinput
)
$EndPoints.psobject.Members |
Where-Object {$_.Name -eq $userinput } |
? {$_.Membertype -eq "noteproperty"} |
%{ $_.Value }
}
答案 1 :(得分:0)
这有点长,但我认为你可以做到以下几点:
($Name,'Name'), ($Age,'Age'), ($Profession,'Profession') | % { if ($_[0]) { $searchTerms = Get-QueryType -userinput $_[1], $Results = Invoke-RestMethod -Method Get -Uri $apiUrl$searchTerms"?term=$Search&limit=$Limit" -WebSession $currentsession} }
顺便说一下,你大部分时间都使用了布尔变量的if,如果你改变代码中的其他部分就可以节省很多行:
if($Limit.Length -eq 0), if($SignatureCount -eq $true)
为:
if(-not $limit), if($SignatureCount)