Home > OS >  Search name from an array using a text present inside the name
Search name from an array using a text present inside the name

Time:11-27

I am not pro in PowerShell,

Context: I have a array of database name and I want to search on the basis of part of a string and assign it to a variable.

For example:

$databaseList = ("master","test-awe-nfr-lcl-data-sqldb","test-awe-nfr-lcl-data-sqldb_VIII","test-awe-nfr-lcl-data-sqldb_IX","test-awe-nfr-lcl-data-sqldb-RBAK")

databaseList contain all the database name and has a common string in all name i.e. test-awe-nfr-lcl-data-sqldb

I want to assign on my following search pattern to various variable like db1,db2,db3,db4

DB Name has only test-awe-nfr-lcl-data-sqldb (search pattern) need to assign db1

DB Name has _VIII in the last of test-awe-nfr-lcl-data-sqldb need to assign db2

DB Name has _IX in the last of test-awe-nfr-lcl-data-sqldb need to assign db3

DB Name has -RBAK in the last of test-awe-nfr-lcl-data-sqldb need to assign db4

CodePudding user response:

Instead of creating 4 different variables $db1 .. $db4, I think it would make more sense to create a Hashtable where you can store the database name under a key name.

Something like

$databaseList = "master","test-awe-nfr-lcl-data-sqldb","test-awe-nfr-lcl-data-sqldb_VIII",
                "test-awe-nfr-lcl-data-sqldb_IX","test-awe-nfr-lcl-data-sqldb-RBAK"

$dbHash = [ordered]@{}
$count  = 1
$databaseList | Where-Object { $_ -match '^test-awe-nfr-lcl-data-sqldb' } | ForEach-Object {
    $dbHash[('db{0}' -f $count  )] = $_
}

Variable $dbHash now contains:

Name                           Value
----                           -----
db1                            test-awe-nfr-lcl-data-sqldb
db2                            test-awe-nfr-lcl-data-sqldb_VIII
db3                            test-awe-nfr-lcl-data-sqldb_IX
db4                            test-awe-nfr-lcl-data-sqldb-RBAK

To use one of these items, simply refer to it as $dbHash['db3'] to return test-awe-nfr-lcl-data-sqldb_IX etc.

CodePudding user response:

This is more a question about general program flow rather than powershell, I think. Anyway, here's some code that works. You should think about what happens when the conditions you're expecting aren't met, though.

$databaseList = ("master","test-awe-nfr-lcl-data-sqldb","test-awe-nfr-lcl-data-sqldb_VIII","test-awe-nfr-lcl-data-sqldb_IX","test-awe-nfr-lcl-data-sqldb-RBAK")

function get_db($dbListItem) {
    if (!($dbListItem.StartsWith("test-awe-nfr-lcl-data-sqldb"))) {
        return $dbListItem # Or do some error handling.
    }

    if ($dbListItem -eq "test-awe-nfr-lcl-data-sqldb") {
        return "db1"
    }
    elseif ($dbListItem.EndsWith("_VIII")) {
        return "db2"
    }
    elseif ($dbListItem.EndsWith("_IX")) {
        return "db3"
    }
    elseif ($dbListItem.EndsWith("_RBAK")) {
        return "db4"
    }
    else {
        return $dbListItem # Or do some error handling.
    }
}

Output:

get_db("master")                                          # master
get_db("test-awe-nfr-lcl-data-sqldb")                     # db1
get_db("test-awe-nfr-lcl-data-sqldb_VIII")                # db2
get_db("unsupported-prefix_VIII")                         # unsupported-prefix_VIII
get_db("test-awe-nfr-lcl-data-sqldb_unsupported-postfix") # same thing comes out
  • Related