Link to home
Start Free TrialLog in
Avatar of abdull sheriff
abdull sheriff

asked on

Power Shell Script for output

i am having a csv file with user's Display Name and i need a powershell script to enable all the accounts which is in the csv file and get an output in csv
Avatar of FOX
FOX
Flag of United States of America image

Make sure the heading above the displaynames reads    DisplayName
Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
Avatar of abdull sheriff
abdull sheriff

ASKER

i have a csv file with the heading as DisplayName and below them i have the users displayname listed

when i run, i get the below error

Get-ADuser : Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
+ % {Get-ADuser $_.DisplayName | Enable-AdAccount} |
+    ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Abdull Sheriff:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetAD
   User
That is basically stating that it doesn't find that user Abdull Sheriff in that domain.  
1.Are you in a multi domain environment?  
2.Are you sure that user is sitting in that domain?
3.Was that user actually created already
4. Verify the users display name is matching from AD to your .csv file.
1.Are you in a multi domain environment?  NO
2.Are you sure that user is sitting in that domain? YES
3.Was that user actually created already YES
4. Verify the users display name is matching from AD to your .csv file. Correct Display names

STILL Error, let me explain in this way
 

i have a csv file name DisplayName.csv , i have attached the csv file for your refernce

First Name: Abdull , Last Name: Sheriff, Display Name: Abdull Sheriff - Accounts India
First Name: Peter  , Last Name: Paul, Display Name: Peter Paul - Finance Asia
First Name: John , Last Name: Paul, Display Name:  - IT Japan

All the display name have their county and department mentioned so my csv file have those as the display name
Now i run the below command (which you gave)

Import-Csv c:\displaynameIN.csv | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv c:\displaynameout.csv -notypeinformation

I get the same error as before. And i am running this command in the AD server
please attach .csv, upload to experts-exchange and then submit. I don't see it here.
this is how it looks when you open displaynameIN.csv

DisplayName
Abdull Sheriff - Accounts India
Peter Paul - Finance Asia
John Paul - IT Japan
please put opening and closing quotes for each DisplayName in your csv file.

 Ask a Question

Experts Exchange > Questions > Power Shell Script for output
?
Power Shell Script for output
Posted on 2018-04-19
Powershell
6
Low Priority ?
32 Views
Last Modified: 2018-04-30
i am having a csv file with user's Display Name and i need a powershell script to enable all the accounts which is in the csv file and get an output in csv
 Comment Report Question
Question by:
abdull sheriff
 
Collapse Question
6 Comments
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42538602
2018-04-19
Make sure the heading above the displaynames reads    DisplayName
Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
Report Comment
Author Comment

by:abdull sheriff
ID: 42539487
2018-04-20
i have a csv file with the heading as DisplayName and below them i have the users displayname listed

when i run, i get the below error

Get-ADuser : Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
+ % {Get-ADuser $_.DisplayName | Enable-AdAccount} |
+    ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Abdull Sheriff:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetAD
   User
Report Comment
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42539822
2018-04-20
That is basically stating that it doesn't find that user Abdull Sheriff in that domain.  
1.Are you in a multi domain environment?  
2.Are you sure that user is sitting in that domain?
3.Was that user actually created already
4. Verify the users display name is matching from AD to your .csv file.
Report Comment
Author Comment

by:abdull sheriff
ID: 42549429
56m
1.Are you in a multi domain environment?  NO
2.Are you sure that user is sitting in that domain? YES
3.Was that user actually created already YES
4. Verify the users display name is matching from AD to your .csv file. Correct Display names

STILL Error, let me explain in this way
 

i have a csv file name DisplayName.csv , i have attached the csv file for your refernce

First Name: Abdull , Last Name: Sheriff, Display Name: Abdull Sheriff - Accounts India
First Name: Peter  , Last Name: Paul, Display Name: Peter Paul - Finance Asia
First Name: John , Last Name: Paul, Display Name:  - IT Japan

All the display name have their county and department mentioned so my csv file have those as the display name
Now i run the below command (which you gave)

Import-Csv c:\displaynameIN.csv | %{Get-ADuser $_.DisplayName | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv c:\displaynameout.csv -notypeinformation

I get the same error as before. And i am running this command in the AD server
Report Comment
Your Comment

by:FOX
Active Directory/Exchange Engineer
ID: 42549447
37m
please attach .csv, upload to experts-exchange and then submit. I don't see it here.
Report Comment
Author Comment

by:abdull sheriff
ID: 42549473
21m
this is how it looks when you open displaynameIN.csv

DisplayName
"Abdull Sheriff - Accounts India"
"Peter Paul - Finance Asia"
i tried both ' and " 
still the same :(

Cannot find an object with identity: 'Abdull Sheriff' under: 'DC=123,DC=4567,DC=com'.
At line:2 char:4
Is that the only user erroring out?

Please run the below command and paste the results.  You must have some type of typo in the .csv file

Get-Aduser -properties * -filter "Surname -like 'Sheriff'" | Select CN,DisplayName,SamaccountName
Abdull Sheriff            Abdull Sheriff - Accounts India           ASheriff
Is that the only account that is throwing the error?

Your displayname under the DisplayName header in the .csv should be as above   "Abdull Sheriff - Accounts India"
If this command found it there is something wrong with your .csv.  Remove the entry in your .csv and readd it.  Make sure there are no spaces after the closing colon.  There may be a space somewhere and when the command is pulling the name it is not recognizing it.
no for all the users its happening, i have checked the spelling and spaces all are correct but still the same error.

ok can you give me a script for email address rather than display name , let me try that with emailaddress in the csv
Let us use the samaccountname and NOT the emailaddress if we are enabling the accounts.
At the top of the .csv column with the samaccountnames name it Samaccountname

Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-ADuser $_.Samaccountname | Enable-AdAccount}  |  Select DisplayName,samaccountname,enabled | Export-Csv 'c:\temp\results.csv' -notypeinformation
hey Fox, Looks like script works when we try with samaccountname .
Once i run the script i can see the accounts are getting enabled BUT in the c:\temp\results.csv output file is 0 kb and no data(just blank) can been seen.
I need something which tells the accounts are enabled in the output file
hey Fox, Looks like script works when we try with samaccountname .
Once i run the script i can see the accounts are getting enabled BUT in the c:\temp\results.csv output file is 0 kb and no data(just blank) can been seen.
I need something which tells the accounts are enabled in the output file
Can we make this happen ???
Fair enough- Since we know the accounts are already enabled and you just need a report -

Import-Csv 'c:\filelocation\nameofcsvfile' | %{Get-Aduser $_.Samaccountname -properties * | Select Samaccountname, DisplayName,enabled} | Export-Csv 'c:\temp\results.csv' -notypeinformation
when i run the below script the accounts get enabled , cant the output file SamACCOUNT.csv have the enable information rather than just a blank file ? i dont want to run a different script just to get the report

Import-Csv c:\SamACCIN.csv | %{Get-ADuser $_.Samaccountname | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv c:\SamACCOUT.csv -notypeinformation
run the below

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv 'c:\SamACCOUT.csv' -notypeinformation
after running the below the accounts get enabled but the outputfile SAMACCOUT is blank and 0kb still :(

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount} |
Select DisplayName,samaccountname,enabled | Export-Csv 'c:\SamACCOUT.csv' -notypeinformation
ASKER CERTIFIED SOLUTION
Avatar of FOX
FOX
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you copied and pasted it into powershell ise. I could not upload the script as a .ps1 to EE so I had to rename it to a .txt just for the upload. Yes it works...in it's native form it has to be renamed  back to EnableAccts.ps1
i was wondering why you created the enableAccts.psi instead you could have just copied the scripts

Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Enable-AdAccount}
Import-Csv 'c:\SamACCIN.csv' | %{Get-ADuser $_.Samaccountname -properties * | Select DisplayName,samaccountname,enabled} |  Export-Csv 'c:\SamACCOUT.csv' -notypeinformation

like this here in EE comment session.

Since you did .ps1 i was wondering what could be the reason
abdull,
If you open up your shell and go to the directory where you saved EnableAccts.ps1 all you have to do is type .\EnableAccts.ps1 and press enter.   It is just the script wrapped into one name instead of you copying and pasting lines.
thank you so much FOX