PowerShell Scripting

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
LVL 1
Ryan RoodAsked:
Who is Participating?
 
LearnctxEngineerCommented:
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
 
Ryan RoodAuthor Commented:
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
 
LearnctxEngineerCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ryan RoodAuthor Commented:
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
 
Ryan RoodAuthor Commented:
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
 
LearnctxEngineerCommented:
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
 
Ryan RoodAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.