0

I am running validations based on details about the machine the script is running on. I currently have an API that will return 1 of the following

Name1
Name1, Name2
Name1, Name3
Name1, Name2, Name3
Name2, Name3
Name2
Name3

What would be the most efficient way to run a different set of validations (functions) based upon one of those 7 results?

EDIT: Here is some pseudo code to represent what I am trying to accomplish. I am hoping to make the switch and values in the where-object of each switch cleaner

function returnRelevantBlankValues {
    $instance = $args[0] #could be any 7 of the strings above
    $inputFile = "C:\path\input.txt"
    $fileResults = "C:\path\output.txt"
    switch ($instance){
        "name1" {
            Get-Content $inputFile | where-object {
                $_ -like "*name1*" -and $_ -notlike "*name2*" -and $_ -notlike "*name3"} > $fileResults
        }
        "name1, name2" {
            Get-Content $inputFile | where-object {
                $_ -like "*name1*" -and $_ -like "*name1*" -and $_ -notlike "*name3"} > $fileResults
        }
    }
}
4

1 回答 1

1

我根据服务器的角色进行验证,一个服务器可以有多个。

首先我创建服务器对象:

$servers = @()
$servers += New-Object -Type PSObject -Property @{
   Name = "Server1"
   Role = "DB"
}
$servers += New-Object -Type PSObject -Property @{
   Name = "Server2"
   Role = "DB","Application"
}

(实际上我将它们存储在 XML 中,但这有效)

然后,您可以使用$_.Role -contains "Application"来确定是否要运行检查。这也使得最终添加类似角色的额外服务器变得更加容易。YMMV 取决于您要检查的详细信息。

于 2013-08-26T19:19:30.040 回答