[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 966
  • Last Modified:

setting user attributes from a CSV in Powershell

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
===========
i.e.  
============
$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)}
$SetUser}
-------------------------
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????
0
Denver_IT
Asked:
Denver_IT
  • 2
1 Solution
 
BSonPoshCommented:
I would recommend the Quest cmdlets. The Exchange cmdlets (from my experience) are very limited in functionality.

www.quest.com/powershell
0
 
BSonPoshCommented:
To answer your question specifically though

change the



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

Open in new window

0
 
Denver_ITAuthor Commented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now