setting user attributes from a CSV in Powershell

Posted on 2009-02-17
Last Modified: 2012-05-06
I want to write account attributes such as title , department and telephone numbers, etc... to my users.  I was able to build the script but I found it failed on users who did not have something in every field.  Therefore I created an IF series to build the "Set-User" command.  The command format looks good but it will not run if I simply build the entire statement in a varriable
$setuser = ("Set-User -Identity " + $Alias)
IF ($Office -ne $blank) {$SetUse = ($SetUser + " -Office " + $Office)}
IF ($StreetAddress -ne $blank) {$SetUsr = ($SetUser + " -StreetAddress " + $StreetAddress)}
IF ($City -ne $blank) {$SetUsr = ($SetUser + " -City " + $City)}
IF ($State -ne $blank) {$SetStr = ($SetUser + " -StateOrProvince " + $State)}
IF ($Zip -ne $blank) {$SetUsr = ($SetUser + " -PostalCode " + $Zip)}
IF ($Department -ne $blank) {$SetUser = ($SetUser + " -Department " + $Department)}
IF ($Title -ne $blank) {$SetUser = ($SetUser + " -Title " + $Title)}
IF ($OfficePhone -ne $blank) {$SetUser = ($SetUser + " -Telephone " + $OfficePhone)}
IF ($OfficePhone2 -ne $blank) {$SetUser = ($SetUser + " -OtherTelephone " + $OfficePhone2)}
IF ($MobilePhone -ne $blank) {$SetUser = ($SetUser + " -MobilePhone " + $MobilePhone)}
IF ($Fax -ne $blank) {$SetUser = ($SetUser + " -Fax " + $Fax)}
IF ($Fax2 -ne $blank) {$SetUser = ($SetUser + " -OtherFax " + $Fax2)}
IF ($Pager -ne $blank) {$SetUser = ($SetUser + " -Pager " + $Pager)}
IF ($HomePhone -ne $blank) {$SetUser = ($SetUser + " -HomePhone " + $HomePhone)}
IF ($HomePhone2 -ne $blank) {$SetUser = ($SetUser + " -OtherHomePhone " + $HomePhone2)}
IF ($AssistantName -ne $blank) {$SetUser = ($SetUser + " -AssistantName " + $AssistantName)}
IF ($TelephoneAssistant -ne $blank) {$SetUser = ($SetUser + " -TelephoneAssistant " + $TelephoneAssistant)}
IF ($Webpage -ne $blank) {$SetUser = ($SetUser + " -WebPage " + $Webpage)}
All this does is print on the screen the command but it does not run the command.
So I tried settings the varriable after the "set-user" command and I get
$SetUser = ("-Identity " + $Alias)
(all the IF Statements)
Set-User $SetUser}

The problem here is that it does not understand the individual data in the varriable.

Any help????
Question by:Denver_IT
    LVL 18

    Expert Comment

    I would recommend the Quest cmdlets. The Exchange cmdlets (from my experience) are very limited in functionality.
    LVL 18

    Accepted Solution

    To answer your question specifically though

    change the

    Set-User $SetUser
    invoke-expression "Set-User $SetUser"

    Open in new window

    LVL 2

    Author Closing Comment

    Thanks for the solution.  using the invoke-expression command requires me to quote any attributes that have spaces in them.  I did not have to do that if I ran the command manually.  This means I have to ensure the CSV has trippled quotes (when reading it from notepad) so that the varriables show up corrctly

    Example: $Address will equal "Long Drive" and in the CSV it must be listed as """Long Drive""",

    Thanks again

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now