Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Anyone good with powershell?


  • Please log in to reply
8 replies to this topic

#1 DeimosChaos

DeimosChaos

  • BC Advisor
  • 1,420 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:09:43 AM

Posted 07 February 2017 - 01:25 PM

Hey guys!

 

So, I am trying to create this powershell script that takes a CSV file and uses the "name" column in "Get-ADUser" cmdlet. I'm typically a Linux bash or python scripting guy, so powershell isn't something I know a whole lot about.

 

Did some searching online and found the following (modified a bit to fit my needs - source):

function Do-Whatever {

$file = Import-CSV 'C:\PowerShell Scripts\users.csv'

foreach ($line in $file) {

$result = Get-ADUser -Filter "Name -eq '$line.name'" | Select-Object samaccountname

$out = @{'accountname'=$result."accountname";'first'=$line."first";'last'=$line."last";'AssociateID'=$line."AssociateID";'BirthDate'=$line."BirthDate"}

$obj = new-object -Type PSObject -Prop $out

Write-Output $obj

}

}

Do-Whatever | Export-CSV output.csv

Basically I need the script to output the "SamAccountName" onto the same line it gets the "name" from. It runs through the CSV fine and outputs all the fields into "output.csv" as it should... except for the "accountname" (samaccountname). I'm not entirely sure if the Get-ADUser is even working right. At first glance does this look like its something that should be working to you all? The only errors it spits out is when it hits somebody that has an apostrophe in their name. Other than that it seems to chug along. Just no samaccount name in the output csv.....

 

Any thoughts at all?

 

Thanks!

 

*Edit

 

I added 'write-output $result' and I am actually getting this error now:

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\PowerShell Scripts\getSamAccountName.ps1:43 char:15
+ Do-Whatever | Export-CSV output.csv
+               ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCo
   mmand

Edited by DeimosChaos, 07 February 2017 - 01:43 PM.

OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


BC AdBot (Login to Remove)

 


#2 DeimosChaos

DeimosChaos
  • Topic Starter

  • BC Advisor
  • 1,420 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:09:43 AM

Posted 07 February 2017 - 04:14 PM

Never mind! Got it working!

 

I realized I only needed " 'samaccountname'=$result " in the $out array. I also added in the "$names = $line.name". Which I think helped the "Get-ADUser" line out as well.

Here is the code in case anyone is curious.

function Do-Whatever {

$file = Import-CSV 'C:\PowerShell Scripts\users.csv'

foreach ($line in $file) {

$names = $line.name

$result = Get-ADUser -Filter "Name -eq '$names'" | Select-Object samaccountname

$out = @{'FullName'=$line."name";'first'=$line."first";'last'=$line."last";'AssociateID'=$line."AssociateID";'BirthDate'=$line."BirthDate";'samaccountname'=$result}

$obj = new-object -Type PSObject -Prop $out

Write-Output $obj

}

}

Do-Whatever | Export-CSV output.csv -noType

OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


#3 SleepyDude

SleepyDude

  • Malware Response Team
  • 3,049 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Portugal
  • Local time:02:43 PM

Posted 07 February 2017 - 06:14 PM

Hi,

 

I don't have AD to test so can't help on your particular problem.

 

Just a note its possible to access the CSV contents like this:

$file = Import-CSV 'users.csv'

$file | ForEach-Object {
   Write-host $_.name
}

• Please do not PM me asking for support. Post on the forums instead it will increases the chances of getting help for your problem by one of us.
• Posts in the Malware section that are not replied to within 4 days will be closed. PM me or a moderator to reactivate.
• Please post your final results, good or bad. We like to know! Thank you!

 
Proud graduate of GeekU and member of UNITE
___
Rui

 
 


#4 JohnnyJammer

JohnnyJammer

  • Members
  • 1,117 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:11:43 PM

Posted 07 February 2017 - 11:18 PM

Also note that i use Expression when doing this with files, i assume it works with AD objects.

So you @{'FullName'=$line."name" i use @{Name="Name";Expression={$line.FullName}}


Edited by JohnnyJammer, 07 February 2017 - 11:18 PM.


#5 DeimosChaos

DeimosChaos
  • Topic Starter

  • BC Advisor
  • 1,420 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:09:43 AM

Posted 08 February 2017 - 05:28 PM

@SleepyDude - yup I tried using it that way as well. I think with it being in a variable it was acting weird for whatever reason.

 

@JohnnyJammer - that might work as well. Got it working though, so now I'm not too worried about it :)

It was literally a one time use script - lol


OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


#6 JohnnyJammer

JohnnyJammer

  • Members
  • 1,117 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:11:43 PM

Posted 08 February 2017 - 08:27 PM

No worries mate, personally i would have used WMIC (wmic useraccount get name, fullname, sid | sort).



#7 DeimosChaos

DeimosChaos
  • Topic Starter

  • BC Advisor
  • 1,420 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:09:43 AM

Posted 08 February 2017 - 10:43 PM

Hmmm haven't heard of WMIC. Will have to take a look at it for future endeavors!

OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


#8 JohnnyJammer

JohnnyJammer

  • Members
  • 1,117 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:11:43 PM

Posted 09 February 2017 - 07:15 PM

WMIC is just as powerful as powershell if you ask me, the only downside is you can use .NET class's for calculating math etc.

Open a dos prompt and type wmic, then once in the terminal type /? or to get a full version of a commanlet type somethign like useraccount get /?:full to display all the details about each switch



#9 DeimosChaos

DeimosChaos
  • Topic Starter

  • BC Advisor
  • 1,420 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:09:43 AM

Posted 09 February 2017 - 08:14 PM

Cool! Thanks for the info! I'll check it out!

OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users