Avatar of amyassein
amyassein
 asked on

What is the best windows powershell scripting for absolute beginner?

Hello,

I've been studying PS scripting for 2 months but still feeling like overwhelmed with lots of information and i can't write some complex script. I know that you will ask "How come you want to write complex scripts while you studied only for 2 months?" ... The answer is that i've learned all of the basic stuff such as cmdlets syntax, aliases, getting properties and methods using get-member, customizing my profile,.. etc ... Then, to feel more excited, i also finished all the looping and flow control techniques. Bang!! i wrote couple of nice real life scripts and they work properly. However, when i jumped to study Functions, Advanced Funtions and Advanced Parameters, i felt my head completely lost. Simple functions are easy as i can write them with no problems.

Here are my random study resources:

TechNet About_ Topics
Windows PowerShell Scripting
Windows PowerShell Cookbook 2nd Edition

As you see, i study from several different resources. The reason because sometimes i find, for example, the cookbook is mentioning examples that talks to expert scripters not to beginners and the same with PS scripting book. The TechNet About_ Topics is good but it is lacking real life examples.

I AM LOST and CONFUSED! PLEASE HELP !

Powershell

Avatar of undefined
Last Comment
amyassein

8/22/2022 - Mon
soostibi

As far as books are concerned this is a must read: http://www.manning.com/payette2/ 

Other good sources are the Scripting Games examples, solutions:
http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/19/2011-scripting-games-all-links-on-one-page.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2010/04/26/2010-scripting-games-all-links-on-one-page.aspx

And the best is help other on Experts-Exchange! :-)
Chris Dent

Agreed, you can always talk to us, we'll happily work through things with you :)

Chris
amyassein

ASKER
Thank you guys and Hi Chris again ! :) i don't know if you can remember me but i grabbed lots of valuable scripts from you.

Ok .. i wrote a function code to get ACL of every subfolder from a folder i specify in the parameter.

Can you tell me what is wrong with it?


Function Get-FolderACL

{ Param([parameter(Mandatory=$true,
                   ValueFromPipeLine=$true)]
                   [String]$folder)


    $GetMusic=get-childitem $folder
    
    foreach ($fol in $GetMusic) {$ACL=get-acl $fol -verbose;out-file 

c:\acl.csv -Encoding ASCII -InputObject $ACL}

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
amyassein

ASKER
Soostibi,

i really didn't understand what did you mean by "And the best is help other on Experts-Exchange! :-) " but i beleive it is something cool :)

Do you mean to give up learning scripting and just come here to seek the scripts that i want directly?

Chris Dent

Aye, I do remember :)

Well, it'll overwrite the file each time (no append on Out-File). It'll be a bit messy because it's raw, barely formatted text. It won't process the input pipeline properly because there's no Process block (based on Parameter() statement).

Maybe you want Export-Csv instead? But that has no append. If it were mine I would not be writing content within a function, I'd have the function output an object I liked then leave the file part for the bitter end.

What would you like to see?

Chris
amyassein

ASKER
Well, All i want is to get the ACL from the subfolders and export them to a well formatted excel sheet. I intended to paste this code to make you correct it for me in order to know where are the mistakes in the code.

I am not asking for a script in this thread or a solution , i just want someone to correct this code for me. :)))
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

You can, of course, have that. But you must dictate what you want to see :)

For example, I recommend you run this:

Get-Acl | Format-List *

Then have a look at the fields returned. Particularly comparing what you get from the Access property compared to AccessToString. You should notice that these two do not show the same thing for Access at all (despite the fact that both show the "same" column):

Get-Acl
Get-Acl | Select-Object Access

Chris
amyassein

ASKER
Sorry, i want to see 3 columns in the spreadsheet.

One for Folder's name
One for Owner
One for Access type

Can you do this for me? :) thanks
soostibi

Sorry, there is a typo:

"And the best is helping others here on Experts-Exchange!"
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
amyassein

ASKER
I promise you when i master PS scripting i will do that indeed but i think it will take at least 1 year to master this language. By the way, i am not a developer and scripting stuff is new for me that's why PS is hard to me and not hard to an experienced developer.
amyassein

ASKER
Also, please guys advise if the "Cookbook 2nd Edition" by Lee Holmes is good for beginners or do you want to suggest another book for me?

Write-host "All i want is: MASTER PS SCRIPT" ... :))
soostibi

First Payette's book, then the Cookbook.

I am not a developer at all, but I still enjoy PS, and think I'm not too bad at that. The most important is to make some vital concept clear: what is output, what is an object, what is a collection. If you know what is the difference between Select-Object -property .... and Select-Object -expandproperty ... then you are getting to the right track. If you can notice, that Compare-Object should rather be called Compare-Collection, then you can imagine what a real Compare-Object could be, or even could write a function to implement that.

It is also important to select the right excercises to practice and master on. For example, could you create a table from this array of strings 'apple', 'banana', 'peach' and the result:

String          Length
-------          ---------
apple           5
banana        6
peach          5
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
soostibi

Back to your original question, here is my prototype. I commented out the export part, so that you see the output at once. Probably you want to see a different result, but it is also important to learn how to formulate your problem.
Function Get-FolderACL  
{ Param([parameter(Mandatory=$true,  
                   ValueFromPipeLine=$true)]  
                   [String]$folder)  
  
    get-childitem $folder | get-acl #| out-file c:\acl.txt -Encoding ASCII 
}

Open in new window

Chris Dent

Sorry, had to have dinner :)

So your requirements are:

One for Folder's name
One for Owner
One for Access type

Well that's fine, all of those are exposed, the last is the hardest, but only because of obscurity. If you got to look at the bit I posted earlier you should, hopefully, have seen that perhaps AccessToString is a good one to pick on.

With that in mind, you might consider:

Get-Acl "SomeFolder" | Select-Object Name, Owner, AccessToString

You're familiar with your output options from there? And how you might get "SomeFolder" in?

> but i think it will take at least 1 year to master this language

Perhaps, but there's no reason you can't make a valuable contribution long before then. I've been working with PS since 2007, and I still learn new things all the time. For me, EE has always been instrumental in the learning process, an endless supply of problems, great and small, to work on.

Of course, you should bear in mind that a lot of the questions we see are on pretty basic, chaining together a small number of commands and handling output. Or combining output from two different CmdLets. Figuring those two out properly is half the battle won :)

Alternatively, you could always read the questions, then hit the Monitor option if the subject-matter makes you wonder.

> please guys advise if the "Cookbook 2nd Edition" by Lee Holmes is good for beginners

I recommend it because it's one of a number very good books on the topic. But, I haven't really done more than flick through it so it's difficult for me to say in any definitive way.

> or do you want to suggest another book for me?

If you already have the cookbook, and you've already covered the basic material, it's more about practice, getting used to how things fit together, and reading around the topic than anything else.

Chris

PS remember that we can quite happily discuss theoretical topics, or syntax, or .NET, or WMI, or regular expressions, or optimisation, or style or, well, you get the idea.
amyassein

ASKER
What is the name of Payette's book? ....

Agree, it is important to understand first the cmdlets and their parameters but this is not my issue my friend. My issue is with the programming itself (or scripting). Before start learning scripting, i almost used all the built in cmdlets in my work in a daily basis. However, scripting is critical when it comes to not typing these annoying commandlets lines every time you need to perform a task and of course is critical for automation.

I know you're asking "why the heck you are asking for a beginner book if you already know all the basic stuff?" ... You're right, maybe i don't know what i need in this stage but let's say i need an book to teach me the advanced stuff but in easy way. In other words, advanced stuff for beginners :))

Here is what i know and what i played with so far:

Cmdlets
Cmdlets (methods and properties) ... Easy! by Get-Member
.Net types ... by Get-Member too
Looping (Do,While,Until,foreach,for)
Conditional (If..Else)
Functions
Advanced Functions (Without CmdLetBinding) this one confuses me
Advanced Parameters
Env. Variables
Variables


Are these things enough to build complex script?  :)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
soostibi

Bruce Payette: Windows PowerShell in Action, Second Edition

It seems to me that you have not get the concept of pipelines deeply yet, so you have to make sure that you can use it effectively, before you start build complex scripts.
Chris Dent

I agree, that might be a stumbling block, a very common one whether you're new to PowerShell or moving in from a different language.

Still, if you get that, or start to get that there's no reason you can't make some very complex scripts.

I did think of a few additional bits and pieces:

http://social.technet.microsoft.com/wiki/contents/articles/windows-powershell-survival-guide.aspx

Quick reference (good if you haven't memorised all the syntax):

http://www.microsoft.com/downloads/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0

Chris
amyassein

ASKER
Thank you guys ....

Soos, I ran your modified script and i have a question now.

Why in powershell the Access column is always looks bad like this?

For example, i need to see the owner and permissions correctly. I see dots which means the line is cut because of screen size or something.

I considered the Format-Noun cmdlet but it didn't work too.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

When you run this:

Get-Acl

The value you see for "Access" is held in the Property AccessToString (the Format definition files dictate this):

Consider:

Get-Acl | Select-Object Access
vs
Get-Acl | Select-Object AccessToString

It'll be truncated based on width parameters. That's why output options like Out-GridView and Export-Csv tend to be nicer to work with.

Chris
amyassein

ASKER
No, i understand pipeline good but sorry i forgot to mention it.

I also understand which kind of cmdlets are accepting inputs from pipeline either by value or by propertyname.

Pipeline is the easiest topic in powershell honestly.

I think my problem is that i know all the components but still not able to combine them to build a script. Is it by practice or i am just in hurry to learn things?
Chris Dent

Practice :)

Chris
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
amyassein

ASKER
Chris, sorry for letting you repeat yourself regarding this AccessToString thing.

Will try it. :)
amyassein

ASKER
Thanks Man, you make feel good now by saying Practice.

This means that i am in hurry.

Patience also is important as i am loosing patience fast :(
amyassein

ASKER
will buy this book soon. :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
amyassein

ASKER
Guys,

Because i received equal responses from your side, i am going to divide the points and assign them to you equally.

Please let me know if there is an objection :)))
ASKER CERTIFIED SOLUTION
soostibi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Dent

Just take it slow, for me building something goes like this:

1. Break down whatever task into a list of small steps
2. Find a means of doing individual steps (sometimes the most time-consuming part)
3. Plumbing, fitting it all together
4. Debugging
5. Optimisation

Figuring out how to handle data, input and output when you're plumbing it takes practice. I know the stuff I wrote 3+ years ago bears very little resemblance to the stuff I do now. Finally, the last two steps often repeat until I'm happy, sometimes over a number of months depending on complexity, and sometimes I'm never happy :)

I even have an example of that. Back in 2009 I wrote a complex script (it's pretty horrible to attempt to understand):

http://www.indented.co.uk/index.php/2009/12/01/syslog-in-powershell/

That script has been replaced by the version in one of my modules. The module, the psm1 file, is just a long script with a bunch of functions in it, one of those is Start-SysLog:

http://www.indented.co.uk/index.php/2010/11/25/netshell/

The year that elapsed between those wasn't spent only rewriting that, but revisiting things, then finding a better, cleaner and neater is part of the learning process.

Chris
amyassein

ASKER
Wow, that's what i wanted to hear , some advise to make me feel i am in the right track, thanks man for this :)

I agree 300 % that the most difficult part is to know what you want to do by these scripting components after you learn them.

Well, i will take that advise and slow things down.

Also, the reason i opened this topic is because i looked at one of the complex scripts in TechNet Scripting Repository and i really felt bad. I told myself that i am not going to learn this thing anymore. However, you advise will lead me to build complex scripts one day but not now or even not this year. lol

Thanks Chris
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
Chris Dent

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
amyassein

ASKER
I understand that ...

Well, i hope i can do something unique by this language other than these familiar scripts that you can find them everywhere on net.

I am even trying to think about new ideas to implement using PS.

Maybe one day i can write a script to send an email whenever a server is down.

I have to be patient, that's all , as i am a rush bastard :)