locked
Clean IF variable RRS feed

  • Question

  • Hi i need help with the following.

    i have multiple customers (only put 2 in this example)
    now i have a variable block for each customer and the an if statement for each customer to reset the variable to the one i will use in my script.

    #customer A variable
    $custa = 'Companyblablabla' 
    $upnsuffixcustf = '@Companyblablabla.com'
    $emailsuffixcustf = '@Companyblablabla.com'
    $shortnamecustf = 'DS'
    $localexchangecustf = 'no'
    $mailformatcustf = 'standaard'
    $adfscustf = $adfs01
    $fileserverdiskcustf = 'J'
    
    #customer B variable
    $custg = '@Companyblablabla2.com' 
    $upnsuffixcustg = '@Companyblablabla2.com'
    $emailsuffixcustg = '@Companyblablabla2.com'
    $shortnamecustg = '2S'
    $localexchangecustg = 'no'
    $mailformatcustg = 'standaard'
    $adfscustg = $adfs01
    $fileserverdiskcustg = 'K'
    
    #reset variable customer specific
    if ($custa -eq $customer){ 
    $upnsuffix = $upnsuffixcusta
    $emailsuffix = $emailsuffixcusta
    $usernameprefix = $shortnamecusta+"_"
    $localexchange = $localexchangecusta
    $mailformat = $mailformatcusta
    $adfsserver = $adfscusta
    $fileserverdisk = $fileserverdiskcusta
    $shortnamecust = $shortnamecusta
    }
    if ($custb -eq $customer){ 
    $upnsuffix = $upnsuffixcustb
    $emailsuffix = $emailsuffixcustb
    $usernameprefix = $shortnamecustb+"_"
    $localexchange = $localexchangecustb
    $mailformat = $mailformatcustb
    $adfsserver = $adfscustb
    $fileserverdisk = $fileserverdiskcustb
    $shortnamecust = $shortnamecustb
    }

    It works but i need to have an IF statement for each customer, is there a way to do with one IF statement?

    if customer -eq cust* then change all * variable 
    * beeing a wildcard

    thanks


    Monday, November 26, 2018 8:46 AM

Answers

  • I'm not completely sure if I got it but if yes you could start with something like this:
    $ADuser = New-Object -TypeName psobject -Property @{
        firstname      = "$firstname"
        lastname       = "$lastname"
        name           = "$name"
        username       = "$username"
        upn            = "$upn"
        customer       = $customer
    }
    
    If ($ADuser.customer -eq "custa" ) {
        $ADuser.email = "$email_custa"
        $ADuser.samAccountName = "$username_custa"
        $ADuser.password = "$password_custa"
        $ADuser.phone = "$phone_custa"
        $ADuser.ou = "$ou_custa"
    }
    elseif ($ADuser.customer -eq "custb") {
        $ADuser.email = "$email_custb"
        $ADuser.samAccountName = "$username_custb"
        $ADuser.password = "$password_custb"
        $ADuser.phone = "$phone_custb"
        $ADuser.ou = "$ou_custb"
    }
    else {
         '..... and so on ...'
    }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by ArnevdHoeven Monday, November 26, 2018 3:17 PM
    Monday, November 26, 2018 12:48 PM

All replies

  • I think there are much better ways to do this but why not a simple if/else statement instead of two separate if statement?

    I think you should invest more time into learning PowerShell. Something like arrays or hash tables would be a good place to start :)

    Monday, November 26, 2018 9:16 AM
  • ... looks pretty weird to me. You might explain what the problem is what you're trying to solve and not the way you think you can solve the problem with.  ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, November 26, 2018 9:16 AM
  • i can create an if else statement but then i have the same issue. 

    If custa blablabla 
    else is custb blablabla
    else if custc blablabla 
    and so on ......

    and i am investing time and this is part of it.

    ... looks pretty weird to me :-)

    Ok have different variables with every customer, some like fistname lastname are things that the user that is starting the runbook needs to fill in, but there are also variables that are customer specific. 

    i need all of these to create an object that i will use in my next steps in the runbook.

    $ADuser = New-Object -TypeName psobject -Property @{
    firstname = "$firstname"
    lastname = "$lastname"
    name = "$name"
    username = "$username"
    upn = "$upn"
    customer = $customer
    email = "$email"
    samAccountName = "$username"
    password = "$password"
    phone = "$phone"
    ou = "$ou"
    ip = "$ip"
    scriptuser = "$scriptuser"
    scriptpassword = "$scriptpassword"
    fileserver = "$fileserver"
    fileserverdisk = "$fileserverdisk"
    fileserverFQDN = $fileserverFQDN
    localexchange = "$localexchange"
    mailformat = "$mailformat"
    adfsserver = "$adfsserver"
    usertype = "$usertype"
    shortnamecust = "$shortnamecust"
    }

    So i have created a set of variables for each customer that will not change. 

    #customer A variable
    $custa = 'name company' 
    $upnsuffixcusta = '@company.cloud'
    $emailsuffixcusta = '@company.nl'
    $shortnamecusta = 'SW'
    $localexchangecusta = 'yes'
    $mailformatcusta = 'optie1'
    $adfscusta = $adfs01
    $fileserverdiskcusta = 'D'

    Now i have a dropdown menu when you start the runbook that fills the name of the customer and when the name of the customer variable is the same as $custN it should use the Customer N set of variables. 

    so now i do this : 

    if ($custn -eq $customer){ 
    $upnsuffix = $upnsuffixcustn
    $emailsuffix = $emailsuffixcustn
    $usernameprefix = $shortnamecustn+"_"
    $localexchange = $localexchangecustn
    $mailformat = $mailformatcustn
    $adfsserver = $adfscustn
    $fileserverdisk = $fileserverdiskcustan
    $shortnamecust = $shortnamecustn
    }
    
    #Create Object
    $ADuser = New-Object -TypeName psobject -Property @{
    firstname = "$firstname"
    lastname = "$lastname"
    name = "$name"
    username = "$username"
    upn = "$upn"
    customer = $customer
    email = "$email"
    samAccountName = "$username"
    password = "$password"
    phone = "$phone"
    ou = "$ou"
    ip = "$ip"
    scriptuser = "$scriptuser"
    scriptpassword = "$scriptpassword"
    fileserver = "$fileserver"
    fileserverdisk = "$fileserverdisk"
    fileserverFQDN = $fileserverFQDN
    localexchange = "$localexchange"
    mailformat = "$mailformat"
    adfsserver = "$adfsserver"
    usertype = "$usertype"
    shortnamecust = "$shortnamecust"
    }



    Monday, November 26, 2018 12:31 PM
  • I'm not completely sure if I got it but if yes you could start with something like this:
    $ADuser = New-Object -TypeName psobject -Property @{
        firstname      = "$firstname"
        lastname       = "$lastname"
        name           = "$name"
        username       = "$username"
        upn            = "$upn"
        customer       = $customer
    }
    
    If ($ADuser.customer -eq "custa" ) {
        $ADuser.email = "$email_custa"
        $ADuser.samAccountName = "$username_custa"
        $ADuser.password = "$password_custa"
        $ADuser.phone = "$phone_custa"
        $ADuser.ou = "$ou_custa"
    }
    elseif ($ADuser.customer -eq "custb") {
        $ADuser.email = "$email_custb"
        $ADuser.samAccountName = "$username_custb"
        $ADuser.password = "$password_custb"
        $ADuser.phone = "$phone_custb"
        $ADuser.ou = "$ou_custb"
    }
    else {
         '..... and so on ...'
    }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by ArnevdHoeven Monday, November 26, 2018 3:17 PM
    Monday, November 26, 2018 12:48 PM
  • I think you would do best by learning PowerShell before trying to write a script.  What you are doing has nothing to do with programming.  YOU want to place you customers in objects or use a CSV file.

    Without some  very fundamental understanding of PowerShell you will not be able to understand any answers that you have been give,

    A forum is not a good place to get personal training. There are many books and videos that will help you get started.

    1. Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    \_(ツ)_/

    Monday, November 26, 2018 12:48 PM
  • thanks for all your nice and not so nice answers :-)

    i did it like this now, any suggestions on how to make it even cleaner will be welcome.

    $arraycust = @()
    if ($customer -eq $custa){
    $arraycust = 'customername1','@customername1.cloud','@customername1.nl','C1','yes','optie1','$adfs01','D'
    } elseif ($customer -eq $custb){
    $arraycust = 'customername2','@customername2.nl','@customername2.nl','LG','yes','standaard','$print01','E'
    } elseif ($customer -eq $custc){ 
    $arraycust = 'customername3','@customername3.demo','@customername3.demo','ID','yes','standaard','$adfs01','F'
    } 
    
    $upnsuffix = $arraycust[1]
    $emailsuffix = $arraycust[2]
    $shortnamecust = $arraycust[3]
    $localexchange = $arraycust[4]
    $mailformat = $arraycust[5]
    $adfsserver = $arraycust[6]
    $fileserverdisk = $arraycust[7]
    $usernameprefix = $shortnamecust+"_"

     
    Monday, November 26, 2018 3:16 PM
  • did it like this in the end : 

    switch ( $Cust ) {
        company1 {
            $arraycust = New-Object -TypeName psobject -Property @{
                upnsuffix = 'customername1'
                emailsuffix = '@customername1.cloud'
                shortnamecust = 'C1'
                localexchange = 'Yes'
                mailformat = 'optie1'
                adfsserver = 'adfs01'
                fileserverdisk = 'fileserer'
                usernameprefix = '@customername1.cloud'
            }
        }
        company2  {
        $arraycust = New-Object -TypeName psobject -Property @{
                upnsuffix = 'customername2'
                emailsuffix = '@customername2.cloud'
                shortnamecust = 'C2'
                localexchange = 'Yes'
                mailformat = 'optie2'
                adfsserver = 'adfs02'
                fileserverdisk = 'fileserer'
                usernameprefix = '@customername2.cloud'
            }
        }
        company3  {
        $arraycust = New-Object -TypeName psobject -Property @{
                upnsuffix = 'customername3'
                emailsuffix = '@customername3.cloud'
                shortnamecust = 'C3'
                localexchange = 'Yes'
                mailformat = 'optie3'
                adfsserver = 'adfs02'
                fileserverdisk = 'fileserer'
                usernameprefix = '@customername3.cloud'
            }
        }
        }
    
    
    

    to make it easier to read

    Friday, December 7, 2018 9:02 AM