Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PowerShell Scripting

Posted on 2014-03-18
7
Medium Priority
?
1,748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 18

Accepted Solution

by:
Learnctx earned 2000 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 18

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 18

Assisted Solution

by:Learnctx
Learnctx earned 2000 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

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Loops Section Overview

610 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