Stuck on powershell

Discussion in 'Software' started by zxspectrum, Feb 21, 2020.

  1. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,956
    156
    189
    Hello there

    I have this script which works great
    Get-ADUser 12345678 -Properties extensionAttribute11 | Select extensionAttribute11. What this does is get me the attrbute 11 data from AD which maps to a users card and then instead of a tutor having to manually take a register, the students will just swipe their cards etc.

    One thing I cant seem to do is run this for multiple users, for which there will be 290, I can get a list of the users from a class report which will be in the form of an excel sheet.

    Does anyone have any pointers that I could use at all? I have tried a few things but nothing is working

    Cheers for any info

    Ed
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  2. FlashDangerpants

    FlashDangerpants Byte Poster

    130
    27
    49
    What do you get where that property ought to be? If it's a {} you might just need to us select -expand <property>
     
    Certifications: MCITP Exchange 2010, MCSA Svr 2012
    WIP: Exchange 2013
  3. Nyx

    Nyx Byte Poster

    176
    18
    15
    Powershell is awesome for such stuff...
    save that excel as csv
    $csv = import-csv file.csv
    you can refer to any column with it's name, if it's for example called "student" then $csv.student will list you all students.
    then
    foreach ($bloke in $csv.student){
    get-aduser $bloke -properties etc
    }
    depends how you want the result, you can create an array or just dump it or add it to your csv or whatever

    hth
     
    Juelz likes this.
  4. Juelz

    Juelz Gigabyte Poster

    Top Poster
    of the Month

    1,677
    333
    201
    Whilst I am far from a Powershell expert, this appears to be correct. I've dabbled in something similar a few years back..
     
  5. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,956
    156
    189
    See, what you have done there is made something sound simple, which I get then I don't get but sort of do, which I know doesn't make sense, however, when I am back in work Monday, i will do this and see what goes. Best thing is, once its done, I can save it and reuse it every time, which is nice.

    One problem I have found with PS is that I get things but putting them into practice is harder especially seeing as I dont use it every day.

    Cheers for the tips mind

    ZX
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  6. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,956
    156
    189
    So I have been doing this with some progress but not the exact results that I have been hoping for.

    Importing the users, easy and done. For some reason I can make my original file open, with no extra input so I then changed the output to a new file, all that does now is open the new notepad file without any populating of data.

    This is what I have at the moment

    Import-CSV G:\Users.csv | Foreach-Object {
    get-aduser -Filter * -Properties | Select Name, SamAccountName
    } | export-csv 'G:\Users test.txt' -notype

    However, i know its not correct so a bit more playing around for my

    @Nyx In regard to you understanding PS, what resources did you use, was there a book that you swear by?

    Cheers

    Eddie
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  7. FlashDangerpants

    FlashDangerpants Byte Poster

    130
    27
    49
    For a book on this stuff, I would suggest this
    https://www.manning.com/books/learn-powershell-scripting-in-a-month-of-lunches
    It will teach you structure the scrupts very differently, using foreach instead of foreach-object
    https://devblogs.microsoft.com/scripting/getting-to-know-foreach-and-foreach-object/
    Personally I find foreach works much better for this sort of thing.

    get-aduser -Filter * will return every user in the directory for every iteration in that loop.
    You want the current object in the pipeline rather than every user in AD, so $_ would work with a plain list of user names
    https://4sysops.com/archives/understanding-the-powershell-_-and-psitem-pipeline-variables/
    But if you are using csv input, $_.<column_name> is more likely for your use case.

    The output is blank because you aren't writing anything to the file. As that foreach-object loop iterates, it is dropping everything at the end.
    Before you loop through the users in that csv, you can create an empty array, something like
    $report = @()
    https://docs.microsoft.com/en-us/po...ell.core/about/about_arrays?view=powershell-7

    Then as you loop each object, you can splat that user's details into the $report
    https://redmondmag.com/articles/2019/01/16/how-to-use-powershell-splatting.aspx
    And then at the end, that report array is what you send to the csv.
     
    Certifications: MCITP Exchange 2010, MCSA Svr 2012
    WIP: Exchange 2013
  8. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,956
    156
    189
    Cheers for the feedback Flash

    Will look into that this afternoon

    ZX
     
    Certifications: BSc computing and information systems
    WIP: 70-680
  9. Nyx

    Nyx Byte Poster

    176
    18
    15
    lots of good info from flash above

    I used to have access to CBT Nuggets on powershell, tbh I learned a lot from my colleague who was writing scripts at one place I worked at. Then again I had programming at Uni (over 15 years ago though) and it definitely helped.

    For what you are trying to do here (and in general) - try not to use pipes too much, especially if you don't exactly know what is happening in each part. figure out what you need bit by bit:
    Import-csv and look at how you can get/access data imported.
    try to use that data in a foreach, like simply write it on the console
    add get-aduser - see if you can get what you want.
    use powershell ISE, run bits of code - not necessarily all of it, then you can then see what your variables contain at given time
    you can do various things with the result, try few different ways - displaying it, adding to array, exporting to csv etc

    googling powershell stuff when you need a simple thing can be hard - a lot of information out there is written by people who code and are hard to understand if you are learning the basics. Definitely spend some time with the most basic tutorials, everything will be easier later on.
     
  10. zxspectrum

    zxspectrum Gigabyte Poster Gold Member

    1,956
    156
    189
    Well, I have this done now so thanks for the help

    You know when you get to a point where you are certain you are on the right path but something isn't working and you focus on one thing well I forgot the most important part of this and I will admit I had to get a bit of help with it.
    Import-Module ActiveDirectory - Did this no problem

    $users(totally forgot about this the next day, so basically I didn't create the array) = Import-Csv "G:\users.csv" - Did this

    $users | ForEach-Object {
    get-ADUser $($_.userid) -Properties extensionAttribute11, name, displayname | Select name, displayname, extensionAttribute11} did this

    It does what I need now thankfully and also it has identified some students where they dont have a card, meaning they cant be tracked on our attendance system on the day when we go to test it, so with this, we can make sure they get on the system and can swipe in.

    Thanks for all the help and pointers. I wish I was more regular on PS to be fair, my role doesn't have that much to do with it

    ZX
     
    Certifications: BSc computing and information systems
    WIP: 70-680

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.