Pablo's Powershell Pow-Wow

Discussion in 'Business & Enterprise Computing' started by PabloEscobar, Sep 18, 2014.

  1. Myne_h

    Myne_h Member

    Joined:
    Feb 27, 2002
    Messages:
    11,442
    I'm more of a batch file person.

    I'd do something like

    for /f "tokens=1,2 delims=|" in %i (list.csv) do powershell Set-BrokerApplication -Name %j -MaxPerUserInstances %k

    That's not guaranteed to work btw
    Infact, it probably won't. I'm rusty af.
     
    Last edited: Jul 23, 2019
    RaZ likes this.
  2. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,508
    Location:
    Melbourne
    The Way i would do it would be like below :

    Code:
    $csv = Import-Csv "<csvfile>"
    
    ForEach($line in $csv)
        {
        Set-BrokerApplication -Name $line.appname -MaxPerUserInstances $line.userlimit
        }

    The varibles in the command need to match the headings in the csv file, but other than that its quite easy to pull off
     
    RaZ, Myne_h and PabloEscobar like this.
  3. OP
    OP
    PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,638
    This sort of thing is a great gateway to powershell, and once you've done it, its so much easier/quick than search/replace and copy/paste from excel, and is far more readable than using batch files.

    We'll dive into Ravennoir's example, as it's on the money. I copied and pasted your data into C:\Scripts\CitrixCrap.csv for the purposes of this example.
    Code:
    Appname | Userlimit
    myapp3 | 5
    myapp4 | 6
    myapp5 | 10
    myapp6 | 2 
    Import-CSV is Love, Import-CSV is life. Import-CSV will deal with pipe delimitation if you ask it to. (via the -Delimiter parameter)

    Code:
    $applist = import-csv "C:\scripts\CitrixCrap.csv" -Delimiter "|"
    
    Import-CSV will take your data, and unless you specify headers with the -Header paramter, it will use the first line in the CSV as the property names for the objects it creates.

    This led to some interesting behavior with my copy pasted data... because

    Code:
    Appname | Userlimit
    
    is different to

    Code:
    Appname|Userlimit
    
    In this case, the property name that is automatically created is 'Appname ' (note the trailing space).

    TIL Import-CSV won't accept a string as a delimiter, only a single character, so...

    We get to mung the data coming in, again, this is a very common operation, when writing scripts that take the output from one system (that you can't control the format of) and feeding it into another (that needs to be in a specific format).

    Code:
    $MungedData = (Get-Content C:\scripts\CitrixCrap.csv).replace(" | ",",")
    
    Here, we are using get-content instead of import-csv. and we are just looking through the file, and replaceing " | " (space pipe space) with a comma, so $MungedData actually ends up as comma separated rather than pipe separated, so we don't need to use the -delimiter parameter anymore... and because we're dealing with an object now ($MungedData) rather than a file (CitrixCrap.csv) we use convertfrom-csv rather than import-csv

    Code:
    $MungedData = (Get-Content C:\scripts\CitrixCrap.csv).replace(" | ",",")
    
    
    $applist = ConvertFrom-Csv $MungedData
    [CODE]
    
    I like to use more descriptive variable names than $line and $csv,  as you start to nest loops it's very easy to lose track of what object you're dealing with,  descriptive variables FTW :).
    
    [CODE]
    foreach ($app in $applist){
     set-brokerapplication -name $app.Appname -MaxPerUserInstance $app.Userlimit
    }
    This is a ForEach loop. it basically cycles over the second object ($applist), and provides you the first one ($app), for use within the loop, then its academic to use
    dot notation to access to properties of our $app object, and feed another cmdlet (set-brokerapplication).


    TL:DR
    Use Ravennoirs example - mine got sidetracked because of what happened when I copied and pasted your sample data :). your actual data is probable in a proper format, and won't need munging.
     
    colmaz, RaZ and Myne_h like this.
  4. Ravennoir

    Ravennoir Member

    Joined:
    May 1, 2007
    Messages:
    6,508
    Location:
    Melbourne
    This so much

    That and Export-csv of course :)

    I actually had to stop myself from exporting to csv only to import it again into a different script
     
  5. Myne_h

    Myne_h Member

    Joined:
    Feb 27, 2002
    Messages:
    11,442
    Gotta say... I really should do some learnin on powershell.

    It does look a lot simpler than bats.
     
    RaZ likes this.
  6. RaZ

    RaZ Member

    Joined:
    Aug 6, 2001
    Messages:
    391
    Location:
    Brissy
    Firstly huge thank you guys :) I really appreciate it and yep - saved for much future use.

    And you are dead right Pablo; Learning to do it this way is also the way of the future. Being an older IT guy (Did I just say that at 38?) lol- and having first done work on Windows NT - the change over to powershell has actually been much fun learning all about it and what it can do. In fact I have been out right amazed at some of the things I have seen done via Powershell scripting.

    A few years ago I was pointed to MS virtual training for a powershell 3 intro - the videos were awesome and done by the guy that created powershell. Highly recommended if your just starting in to it.

    @Ravennoir - thanks heaps! That foreach loop was what I couldn't remember and didn't have a good example.

    Again thanks to all that replied and Pablo - ta for the lesson ;) also the code made me laugh - lots lol "$MungedData = (Get-Content C:\scripts\CitrixCrap.csv)" hahah
    But seriously - that was really well described :thumbup::thumbup:
     
    Myne_h likes this.
  7. RaZ

    RaZ Member

    Joined:
    Aug 6, 2001
    Messages:
    391
    Location:
    Brissy
    In the end it is; its just so huge tho! :) It was never designed for you to memorise - its too big to do that, the help function is amazing (When I remember it lol) and finding example and using them for your needs is great, but sometimes understanding things like the csv-import vs get-content and the foreach loop really helps.

    For Citrix Xenapp stuff - everything you see in the gui can be accessed via their powershell snapins that you import; really helps when you need to set something across a heap of apps. and also many settings are not in the gui but in powershell ;) a good reason to learn it as many software makers are using powershell more and more ;)
     
    Myne_h likes this.
  8. RaZ

    RaZ Member

    Joined:
    Aug 6, 2001
    Messages:
    391
    Location:
    Brissy
    Just wanted to say you were 100% correct there too lol actual data is proper csv and doesn't need munging lol But now I know how to deal with that if I need to :)
     
  9. Myne_h

    Myne_h Member

    Joined:
    Feb 27, 2002
    Messages:
    11,442
    If you guys want a bit of a laugh at bat files - and my hiiiiiiilarious function names.

    Here's something I wrote well over a decade ago to create md5 checksums, store them alternate NTFS streams, and compare later.
    It needs md5sum.exe which is probably still available on the net.

    I'm guessing it's a lot simpler with powershell?

     
    RaZ likes this.
  10. RaZ

    RaZ Member

    Joined:
    Aug 6, 2001
    Messages:
    391
    Location:
    Brissy
    Also this - I wasn't 100% sure on this - thanks again guys :D so stoked and I understand it heaps more now too. How I mentioned how much I love you guys <3
     
  11. RaZ

    RaZ Member

    Joined:
    Aug 6, 2001
    Messages:
    391
    Location:
    Brissy
    Wow - holy batballs, now thats a batch file! hahaha
    I have nfi how you would do that in powershell - but yes I bet it would be much small code :)
     
  12. m0n4g3

    m0n4g3 Member

    Joined:
    Aug 5, 2009
    Messages:
    3,742
    Location:
    Perth, WA
    Saw

    Code:
     go to bbq 
    Forgot what i was doing and started dreaming of bbq, didn't read rest...


    Good bat i approve! :D
     
  13. Myne_h

    Myne_h Member

    Joined:
    Feb 27, 2002
    Messages:
    11,442
    Hahah!

    IIRC I really didn't understand entirely how I made it work because it's basically gibberish even though it's written properly.
    So I ended up with functions like OMG WTF BBQ HAX :p

    (which also kinda dates it)
     
  14. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Can you put the script in code tags so i don't have to edit out the "smilies"?

    Did you want feature parity, or just the basics?
     
  15. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    7,501
    Location:
    Mount Cotton, QLD
    Is

    Code:
    Set-ADAccountPassword -Identity (Get-AdUser $user.Username) -NewPassword $pwd 
    functionally any way different to

    Code:
    Get-AdUser $user.Username | Set-ADAccountPassword -NewPassword $pwd
     
  16. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    The short answer is "no" because BODMAS.

    but if $user is already the result of "Get-ADUser", you could just
    Code:
     $user | Set-ADAccountPassword -NewPassword $pwd
     
  17. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    7,501
    Location:
    Mount Cotton, QLD
    $user is from a foreach
     
  18. looktall

    looktall Working Class Doughnut

    Joined:
    Sep 17, 2001
    Messages:
    26,668
    You should then be able to pipe straight into set-adaccountpassword.

    Code:
    Foreach {
    Set-ADAccountPassword -NewPassword $pwd}
    
    Or some such.
     
  19. freaky_beeky

    freaky_beeky Member

    Joined:
    Dec 2, 2004
    Messages:
    1,169
    Location:
    Brisbane
    Share the script in its entirety?
     
  20. miicah

    miicah Member

    Joined:
    Jun 3, 2010
    Messages:
    7,501
    Location:
    Mount Cotton, QLD
    Code:
    Import-Module ActiveDirectory
    
    $userList = Import-Csv .\users.csv
    $logLocation = ".\Log.txt"
    
    foreach ($user in $userList)
    {
        $pwd = ConvertTo-SecureString -AsPlainText $user.Password -Force
        Set-ADAccountPassword -Identity (Get-AdUser $user.Username) -Reset -NewPassword $pwd
        $logMessage = "$(Get-Date -Format "yyyy-MM-dd - HH:mm"): Reset the password for account $($user.Username) to $($user.Password)"
        Add-Content -Path $logLocation $logMessage
    }
    Csv file is:

    Username,Password
    user.1234,Password1234
    user.5678,Password5678

    etc etc
     

Share This Page

Advertisement: