Solved

PowerShell Scripting

Posted on 2014-03-18
7
1,421 Views
Last Modified: 2014-03-19
Hello Experts,

This is my first learning experience with PowerShell ... looking for some guidance. Here is my current query:

Import-Module ActiveDirectory
$UserList = Import-CSV 'c:\users\someuser\desktop\ADUsersImport2.csv'
$UserList | Foreach {Set-ADUSer -Identity $_.SamAccountName -GivenName $_.GivenName -Surname $_.Surname -Description $_.Description -DisplayName $_.DisplayName -Company $_.Company -Department $_.Department -Office $_.Office -EmailAddress $_.EmailAddress -OfficePhone $_.OfficePhone -Mobile $_.Mobile -Fax $_.Fax -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode -Country $_.Country -LogonWorkstations $_.LogonWorkstations -HomePage $_.HomePage -Title $_.Title -Enabled $_.Enabled}

Open in new window


Please see attached CSV file that I am trying to import.

First question ... how do I keep my query the same but allow the query to run with a blank import field. For instance ... this user does not have an email address or a mobile phone number. Everything works if I put in $NULL in these two fields but then I have $NULL in Active Directory.

The second question is ... I am trying to also set whether the user is enabled or not. Everything I have tried to set this field will not work. All I get is:

Set-ADUser : Cannot convert 'System.String' to the type
'System.Nullable`1[System.Boolean]' required by parameter 'Enabled'.
At line:1 char:508
+ ... Title -Enabled $_.Enabled}
+                    ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBind
   ingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.
   Management.Commands.SetADUser

Any assistance of guidance is greatly appreciated.

Thanks,
Ryan
ADUsersImport2.csv
0
Comment
Question by:Ryan Rood
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
Learnctx earned 500 total points
ID: 39938422
Use Get-Help.

Get-Help | Set-ADUser -Full | More

Open in new window

You will see what the -Enabled parameter requires.

-Enabled <System.Nullable[bool]>
    Specifies if an account is enabled. An enabled account requires a
    password. This parameter sets the Enabled property for an account
    object. This parameter also sets the ADS_UF_ACCOUNTDISABLE flag of the
    Active Directory User Account Control (UAC) attribute. Possible values
    for this parameter include:
      $false or 0
      $true or 1

    The following example shows how to set this parameter to enable the
    account.
      -Enabled $true

    Required?                    false
    Position?                    named
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false
So it only accepts a boolean value which means $true/$false or 1/0. You're passing it a string. For a quick fix just change your script a little.

Import-Module ActiveDirectory
$UserList = Import-CSV 'c:\users\someuser\desktop\ADUsersImport2.csv'
Foreach ($User in $UserList)
{
   $Enable = $false
   If ($_.Enabled -eq "True")
   {
      $Enable = $true
   }
   Set-ADUSer -Identity $_.SamAccountName -GivenName $_.GivenName -Surname $_.Surname -Description $_.Description -DisplayName $_.DisplayName -Company $_.Company -Department $_.Department -Office $_.Office -EmailAddress $_.EmailAddress -OfficePhone $_.OfficePhone -Mobile $_.Mobile -Fax $_.Fax -StreetAddress $_.StreetAddress -City $_.City -State $_.State -PostalCode $_.PostalCode -Country $_.Country -LogonWorkstations $_.LogonWorkstations -HomePage $_.HomePage -Title $_.Title -Enabled $Enable
}

Open in new window

0
 
LVL 1

Author Comment

by:Ryan Rood
ID: 39938476
Can you explain where you are getting/setting the $User variable? The code does not seem to work as is ... just trying to understand why. I understand the -Enabled feature based on your code now. Thank you.
0
 
LVL 16

Expert Comment

by:Learnctx
ID: 39938492
You could use $Enable or $Enabled. It's just a variable name. You could use $SuperCool if you wanted :)

But yes, my example above has not properly replaced the pipeline variables (The $_'s). It should be as below. I've now replaced all the pipeline variables with $User

Import-Module ActiveDirectory
$UserList = Import-CSV 'c:\users\someuser\desktop\ADUsersImport2.csv'
Foreach ($User in $UserList)
{
   $Enable = $false
   If ($User.Enabled -eq "True")
   {
      $Enable = $true
   }
   Set-ADUSer -Identity $User.SamAccountName -GivenName $User.GivenName -Surname $User.Surname -Description $User.Description -DisplayName $User.DisplayName -Company $User.Company -Department $User.Department -Office $User.Office -EmailAddress $User.EmailAddress -OfficePhone $User.OfficePhone -Mobile $User.Mobile -Fax $User.Fax -StreetAddress $User.StreetAddress -City $User.City -State $User.State -PostalCode $User.PostalCode -Country $User.Country -LogonWorkstations $User.LogonWorkstations -HomePage $User.HomePage -Title $User.Title -Enabled $Enable
}

Open in new window

The $User variable is just created on a fly. It could have been anything.

There is no difference between the following they achieve the same result in the end in their own way.

$UserList | Foreach
and
Foreach ($User in $UserList)

Open in new window

You could also use your existing method like so.

$UserList | Foreach {
   $Enable = $false
   If ($_.Enabled -eq "True")
   {
      $Enable = $true
   }
   Set-ADUSer -Identity .........
}

Open in new window

Just different ways of achieving the same result. It just depends on how you like to structure your scripts. It is my personal preference as it is how I learned from languages before PowerShell.

Another example for you would be.

$Array = 1,2,3,4,5,6,7,8,9,10
Foreach ($Number in $Array)
{
   $Number
}

Foreach ($Letter in $Array)
{
   $Letter
}

Open in new window

It doesn't matter that I'm using $Number or $Letter to iterate through the array, it is just a variable name being used.
0
 
LVL 1

Author Comment

by:Ryan Rood
ID: 39938573
Thanks for the explanation - follow up question:

1. When you define "$User in $UserList" are you relating $User to $User.GivenName? In my CSV the column headers are GivenName in this case. I am not a programmer so I guess I am just trying to understand why you are assigning the variables differently?
0
 
LVL 1

Author Comment

by:Ryan Rood
ID: 39938617
I am also trying to check a variable to see if it is $null. For instance the EmailAddress field.

   If ($User.EmailAddress -ne $null)
   {
		$EmailAddress = $User.EmailAddress
	}
	else
	{
		$User.EmailAddress = $null
	}

Open in new window


This doesn't work ... but why not? Is there a better way to do this? Is there a way to check all fields for $null values and set them to null before it tries to write a null value and the script errors out?
0
 
LVL 16

Assisted Solution

by:Learnctx
Learnctx earned 500 total points
ID: 39939028
1. When you define "$User in $UserList" are you relating $User to $User.GivenName? In my CSV the column headers are GivenName in this case.
$User becomes an object containing all of the properties of each line of the CSV file. So $User would contain all the properties for each user in the CSV. It would have a sAMAccountName, givenname, sn, etc as properties the same as in your first script $_ was the object from the pipe that represented the same thing.
This doesn't work ... but why not? Is there a better way to do this? Is there a way to check all fields for $null values and set them to null before it tries to write a null value and the script errors out?
Powershell probably doesn't consider it 'null'. Take this example.

$x = [string]""
if ($x -eq $null)
{
   "null value"
}
else
{
   "not null
}

Open in new window

You'll find that $x is not null at all even though it does not contain a useful value, it is just an empty string, but its still a string. So you'll find your import-csv imports the blank value as an empty string.

Try one of these instead.

If ($User.EmailAddress -ne "")
{
   $EmailAddress = $User.EmailAddress
}
else
{
   $User.EmailAddress = $null
}

If ($User.EmailAddress)
{
   $EmailAddress = $User.EmailAddress
}
else
{
   $User.EmailAddress = $null
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:Ryan Rood
ID: 39939249
Awesome - thank you! Very informative and helpful information. Looking forward to learning more about PowerShell and starting to use it more.

Thank you again.
0

Join & Write a Comment

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A procedure for exporting installed hotfix details of remote computers using powershell
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 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

16 Experts available now in Live!

Get 1:1 Help Now