Options

Any powershell guru's in da house

dalesdales Member Posts: 225
Hi all,

Hoping that there are some powershell god's here that can help me. I have been given a task of setting some netware permissions for folders with a certain name.

This is all payroll stuff so for example HR have a folder for every person and in that folder are other subfolders such as training,Payroll,sickness. What I would like is to start at the parent folder and scan each persons folder and when the script comes across a folder called payroll then kick off a netware app called lrights which will allow me to script the permissions change.
I guess I will need to do some kind of get-childitem and save the results into an array!? which can then be called back and written into a variable for the lrights executable.

The kind of output into the array that I will need would be something like P:\user\payroll

At the moment the script I have created give this output for each user:

Directory: P:\lastname, firstname



Name : Payroll
CreationTime : 18/01/2010 11:27:08
LastWriteTime : 18/01/2010 11:27:08
LastAccessTime : 01/01/1601 00:00:00


My script at the moment looks like this:

Function FindFolder
{
$input | Where-Object {$_.Name -eq “Payroll”}
}
Get-ChildItem -Path P:\ -Recurse | FindFolder | Format-list

Any help/advice very gratefully accepted
Kind Regards
Dale Scriven

Twitter:dscriven
Blog: vhorizon.co.uk

Comments

  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    [/COLOR]
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=black]$payroll = get-childitem -recurse P:\ | Where-Object {$_.Name -eq “Payroll”}[/COLOR][/SIZE][/FONT]
    [SIZE=2][FONT=Courier New][COLOR=black]foreach($item in $payroll)[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=black]{[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=black]Write-Host $item.FullName[/COLOR][/FONT][/SIZE]
    [SIZE=2][FONT=Courier New][COLOR=black]}[/COLOR][/FONT][/SIZE][/SIZE][/FONT][/SIZE][/FONT]
    [COLOR=black]
    


    That will write the full path. Is that what you need?


    Rather than write-host you can use $item.FullName in the params of lrights.
  • Options
    dalesdales Member Posts: 225
    Ah brilliant thanks for the advice, thats seems to do just what I wanted, thanks very much!
    Kind Regards
    Dale Scriven

    Twitter:dscriven
    Blog: vhorizon.co.uk
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Glad I could help! It always amazes me how much you can do with just a few lines of code in PoSh.
  • Options
    astorrsastorrs Member Posts: 3,139 ■■■■■■□□□□
    It always amazes me how much you can do with just a few lines of code in PoSh.
    I'm amused by how you can turn almost anything into a single line of code. ;)

    Get-ChildItem -Recurse P:\ | ? {$_.Name -eq “Payroll”} | % { Write-Host $_.FullName }
  • Options
    HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    I'd suggest avoiding using where-object as a filter for this, especially if there are a lot of files and folders you're searching in. Using what you have will cause the server to grab all the files and folders there, THEN filter them down. Don't be surprised if you use a ton of resources (specifically RAM) doing this, and it takes a long time.

    Instead, this only gets what you want from the get go...

    get-childitem -path P:\ -recurse -filter "Payroll"

    Furthermore, you could widdle this small amount down to get just folders, eliminating the risk someone has a file called payroll...

    where-object {$_.PSIsContainer -eq $true}
    Good luck to all!
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    astorrs wrote: »
    I'm amused by how you can turn almost anything into a single line of code. ;)

    Get-ChildItem -Recurse P:\ | ? {$_.Name -eq “Payroll”} | % { Write-Host $_.FullName }

    Why do I always forget ? and %?

    Thanks guys, like I said. i always learn something.
  • Options
    HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Why do I always forget ? and %?

    Thanks guys, like I said. i always learn something.

    Don't crunch the code... (in a script)

    get-admin :: PowerShell PSA: Stop Crunching the CODE!
    Good luck to all!
  • Options
    PashPash Member Posts: 1,600 ■■■■■□□□□□
    HeroPsycho wrote: »
    Don't crunch the code... (in a script)

    get-admin :: PowerShell PSA: Stop Crunching the CODE!

    I am agreeing with this more and more now. It's becoming harder to read code with shorthand stuff in it. It's funny though, Microsoft branded powershell handbooks and best practice stuff always go with shorthand option. I have the powershell 2.0 admin pocket book, and a lot of it recommends using shorthand or creating own aliases.

    Anyway, nice suggestion about -filter property instead of piping it into where object, or if you are gonna pipe it, select only folders.

    Great stuff.
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • Options
    astorrsastorrs Member Posts: 3,139 ■■■■■■□□□□
    HeroPsycho wrote: »
    Don't crunch the code... (in a script)

    get-admin :: PowerShell PSA: Stop Crunching the CODE!
    LOL okay so I knew someone would have to say that. :)

    I agree that if you're sharing the code in a public forum or saving it in a script, be descriptive. With that said, I'm never using -Object for the usual suspects: ForEach-Object, Where-Object, Select-Object, Sort-Object, etc. I'd argue dropping it on those actually makes the code more readable.

    If you're banging it in on the command line to just "get something done", sorry but I'm all for $a + $b | ? ( $_ -ne $c } | ogv

    Pash wrote: »
    It's funny though, Microsoft branded powershell handbooks and best practice stuff always go with shorthand option.
    Easier to fit on the line/page I'd assume.
  • Options
    HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    astorrs wrote: »
    LOL okay so I knew someone would have to say that. :)

    I agree that if you're sharing the code in a public forum or saving it in a script, be descriptive. With that said, I'm never using -Object for the usual suspects: ForEach-Object, Where-Object, Select-Object, Sort-Object, etc. I'd argue dropping it on those actually makes the code more readable.

    If you're banging it in on the command line to just "get something done", sorry but I'm all for $a + $b | ? ( $_ -ne $c } | ogv


    Easier to fit on the line/page I'd assume.

    Completely agree aliases are good for banging stuff out on the keyboard. However, I'd argue in a script use the full cmdlet name always, and do the same when sharing on forums unless you're explicitly trying to show someone how to shorthand it.

    Especially when trying to get more people to use powershell (way too many people don't), aliasing the crap out of something even to illustrate something just intimidates people from using PowerShell.
    Good luck to all!
  • Options
    HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Pash wrote: »
    Anyway, nice suggestion about -filter property instead of piping it into where object, or if you are gonna pipe it, select only folders.

    Great stuff.

    Here's where get-childitem fails miserably. There's crap flexibility to filter natively to the cmdlet, which makes analyzing large collections of files for example in a file server a steaming hot crap sandwich.

    Let's take for example if I wanted to get all files (not folders) on a file server whose last access date is over one year. The only way you can do that is to get all files & folders, and use a where-object filter. Neither can be done natively with the get-childitem cmdlet.

    I recently needed to generate a report of all files last accessed and last modified over 7 years old on some file servers that stored 50-100 gigabytes per server. I couldn't do it because PoSh would run out of RAM before it could even filter it down due to the overhead of converting the file info into .net objects, which is how PowerShell works. In Microsoft's defense, this is why they made File Server Resource Manager, but these were Windows 2003 R1 file servers, so that wasn't an option.

    For craps and giggles, try this on a 32-bit workstation and run task manager and watch the RAM usage of the PowerShell.exe process...

    $files = get-childitem -path c:\ -recurse

    Your computer will hate you! And it's also important to understand that before you go running PoSh commands that seem rather innocent on a server. You can bring a server to it's knees far too easily.

    One other point I wanted to share is this perfectly illustrates why you should only use where-object as a last resort if you're scripting when you want to filter when dealing with large collections. For example, if you want to get let's say all the computer accounts for servers in your domain using the Quest AD cmdlets, don't do this:

    get-qadcomputer * | where-object {$_.OSName -like "*Server*"}

    Do this:

    get-qadcomputer * -OSName "*Server*"

    Performs a lot better, and even lazy people like astorrs get to type less. :D
    Good luck to all!
  • Options
    PashPash Member Posts: 1,600 ■■■■■□□□□□
    Makes sense even to me.

    So, when are you bringing out some powershell CBT's Hero?
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • Options
    HeroPsychoHeroPsycho Inactive Imported Users Posts: 1,940
    Sapien I believe has some excellent ones. I'm confident in my abilities to train, but I'm not confident I could do everything else necessary to produce a quality CBT compared to other companies who are already doing it.

    Appreciate the compliments though.
    Good luck to all!
Sign In or Register to comment.