Trying to Improve my Powershell Skills - Need some guidance

  • Import-Module ActiveDirectory 
    $userInput = $null 
    	$prompt = Read-Host -Prompt "New employee" 
    	$proxy = Read-host -Prompt "full proxy address as SMTP:[email protected]"
    	$user = Get-ADUser $prompt -Properties mail, ProxyAddresses
    	$userInput = "$($user.samaccountname)" 
    } While($user -eq $null)
    Set-ADUser $user -Add @{ProxyAddresses="$proxy"}

    What I'd like to remove is the need to fill in the full proxy address.

    I had tried Set-ADUser $user -Add @{ProxyAddresses=("SMTP:"+"$user"+"")}

    That didn't work at least not how I'd expected... What I have now, adds the proxy address, but it leaves room for a lot of error. Any pointers on how I can concatenate that last entry?

  • I think I got it.

    $fullproxy = ("SMTP:"+$userInput+"")

    and then the set-aduser . . . .

  • I wouldn't try to automatically use a capitalized "SMTP" as in the example you "had tried" at the end of your post. If you did then every entry you add in to a proxyAddress attribute would have a capitalized SMTP. Only the main login should have capital SMPT, the rest of them (aliases) should be lower case).

    As for improving PowerShell skills, I bought this when it was released:
    and I highly recommend it. I haven't gotten though much of it yet because I'm trying to finish up some MCSE's first, but I will be diving a lot more into it soon.

  • Maybe if I create a variable as

    $fullproxy = ("SMTP:"+"$user"+"")

    And use the $fullproxy variable...

  • @Tim_G We recently had to break our hybrid environment, so we have to create the proxy records. So this would be the only record we "care" about so we can create user accounts.

    I'm trying to avoid have to open ADUC, finding the user and then adding the record by hand.

    Thanks for the tip though.

  • I think I got it.

    $fullproxy = ("SMTP:"+$userInput+"")

    and then the set-aduser . . . .

  • @DustinB3403

    I usually make big lists in Excel and use that in PowerShell. You could output all users "sAMAccountName" to CSV... then on the first line in the next column type the and drag it down to the end.

    Then in the columns before and after it, do the same thing... type in the powershell commands you need, drag it down tot he bottom to copy it to each line, copy it into notepad++, replace all tabs with spaces, and call it a .PS1.

    Sounds like a lot, but it really only takes literally 2 minutes to make a list of hundreds or thousands and turn it into a .ps1.

  • Also, if you have a specific group of people, you can use a "get-" command instead of .csv list. I meant that lists like I said about above are best if it's not an easy grab using a get command.

    I'm probably not explaining myself right, you know what I mean?

  • @Tim_G I considered using a csv to import the records, but this is only for the users we have to add going forward from today. (until we reconnect the hybrid)

    It was actually the first kind of post I had found (import using CSV) but since we have so few uses we add (maybe a handful a month) doing so is overkill.

  • @DustinB3403 Ahh I see, then yes a prompt for input script is best then.