Ryan Rood
asked on
PowerShell Scripting
Hello Experts,
This is my first learning experience with PowerShell ... looking for some guidance. Here is my current query:
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,Micr osoft.Acti veDirector y.
Management.Commands.SetADU ser
Any assistance of guidance is greatly appreciated.
Thanks,
Ryan
ADUsersImport2.csv
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}
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.
At line:1 char:508
+ ... Title -Enabled $_.Enabled}
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBind
ingException
+ FullyQualifiedErrorId : CannotConvertArgument,Micr
Management.Commands.SetADU
Any assistance of guidance is greatly appreciated.
Thanks,
Ryan
ADUsersImport2.csv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
There is no difference between the following they achieve the same result in the end in their own way.
Another example for you would be.
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
}
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)
You could also use your existing method like so.$UserList | Foreach {
$Enable = $false
If ($_.Enabled -eq "True")
{
$Enable = $true
}
Set-ADUSer -Identity .........
}
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
}
It doesn't matter that I'm using $Number or $Letter to iterate through the array, it is just a variable name being used.
ASKER
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?
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?
ASKER
I am also trying to check a variable to see if it is $null. For instance the EmailAddress field.
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?
If ($User.EmailAddress -ne $null)
{
$EmailAddress = $User.EmailAddress
}
else
{
$User.EmailAddress = $null
}
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome - thank you! Very informative and helpful information. Looking forward to learning more about PowerShell and starting to use it more.
Thank you again.
Thank you again.
ASKER