We help IT Professionals succeed at work.

New to PowerShell

compdigit44
compdigit44 asked
on
Ok, I've decided to break away from Windows batch files in favor for Windows POwerShell...

Now I'm not a programmer by any means so I have been doing a lot of research on powershell. Yet after all of the articles I have read, I'm still having problem understanding what is Object-Based programmnig? How does this different from a batch file? What make powershell so great..

Remember I'm not a programmer now ..;-)
Comment
Watch Question

Commented:
Here is some info that should help you. You can read the full article at http://technet.microsoft.com/en-us/library/bb963725.aspx

Windows PowerShell, although it is based on object-oriented programming and Microsoft .NET Framework classes, does not require server administrators to gain an exhaustive knowledge of these topics. On the contrary, Windows PowerShell is designed to enable server administrators to learn quickly how to manipulate objects either interactively at the command-line or through scripting.



One of the most important features to understand about objects is how the use of objects by Windows PowerShell differs from the text-based processing that is characteristic of many earlier command shells. Traditional command shells that use text-based processing require a variety of text processing utilities in order to function.

By contrast, Windows PowerShell transfers data between its cmdlets as structured objects rather than as text, which means that each Windows PowerShell cmdlet can interpret and act on any object that is passed to it. The cmdlet that receives the object can act directly on that object and its properties without requiring any conversion or manipulation, as is the case for text-based processing.



An object is not the same as the entity that the object represents. For example, a library server object is not the library server itself. Rather, it represents the physical file server that is configured as a Virtual Machine Manager library server so that you can use cmdlets to perform operations on that server. You might use the Add-LibraryServer cmdlet to add a new library server object to the Virtual Machine Manager database, or you might use the Get-LibraryServer cmdlet to retrieve the object that represents an existing library server and view its properties.

A Windows PowerShell array, including a Virtual Machine Manager array, is an array of objects. You might, for example, create an array that stores the object that represents each virtual machine that is currently turned off and then pass all of these virtual machine objects to a cmdlet that starts each virtual machine in the array.

Typically, an array contains objects of the same type. However, an untyped array in Windows PowerShell can contain different types of .NET Framework objects in each element of the array.

--------------------------------------------------------------------------------------------------------------------

Also take a look at Tip #5 here http://www.computerworld.com/s/article/9066180/PowerShell_Tips_and_Tricks


I hope this helps
Chris DentPowerShell Developer
Top Expert 2010

Commented:

PowerShell deals in Objects, everything you get is, on one level or another, an Object. For instance, we might have a string like this:

$String = "Something"

That String is an Object, System.String in fact, from .NET (see $String.GetType()). It has Properties, and it has Methods.

System.String only had one property, the Length of the string. See:

$String.Length

But it has quite a lot of methods. For example, we might make it all upper-case:

$String.ToUpper()

You can see all the methods and properties on an object with Get-Member:

$String | Get-Member

Get-Member is an extremely valuable CmdLet. If that's not enough, the .NET Framework reference shows details of everything (everything which isn't a CmdLet that is), the only downside is that it does not provide PS examples, it really should.

> How does this different from a batch file? What make powershell so great..

Okay so we've had Objects... But this question has other answers:

 - Extremely flexible. From WMI, to the registry, to Active Directory management, the list goes on and on.
 - It's an explorers language. Not sure about something? Get-Member and Format-List * will help you find out what something can do.
 - Access to the vast majority of the .NET Framework. PS is based on .NET, there's little you can do in C# that cannot be done in PowerShell, quite an acomplishment for a scripting language.
 - Exchange 2007 / 2010 use it as the management interface of choice.

I also recommend you have a dig into the content here:

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

Finally, you can always ask here for explanations / help in as much or as little detail as you like :)

Chris
Chris DentPowerShell Developer
Top Expert 2010

Commented:

I did want to add that PowerShell's object-oriented nature shouldn't put you off. You could completely ignore that aspect (and indeed the .NET framework) and you wouldn't really be any worse off. If you choose to explore it later you can.

Perhaps there's something you've previously done in Batch we can convert to PowerShell? That way you can see it in action and have a direct comparison when it comes to ease of use or performance.

Chris

Author

Commented:
OK..

Let me see if I'm understanding things corret..

If you wanted to created a batch file to xopy file to a directory then change permissons via xCacls you would need to write a could of lines in notepad called 2 or 3 differnect DOS based command where in powershell these cmdlets would lump such action in to one command

is this correct?

I still do not understand the would object thing though

Author

Commented:
I've read articles that you can build your own cmdlets but al lof these article go way over my head
Chris DentPowerShell Developer
Top Expert 2010

Commented:

No, not really.

That action is actually a bit more complex in PowerShell. In part because rather than using XCACLS to change permissions you have to construct the access right yourself and apply it. The benefit is that you get more control over the ACL.

Some commands in PowerShell are pretty much equal to DOS commands. For instance, in PowerShell we might copy files like this, a simple CmdLet:

  Copy-Item "C:\Stuff\Test.txt" "C:\Stuff\NewFolder"

You'll find that many DOS commands appear to work in PowerShell, such as mkdir, cd and dir, all of these are aliases for PowerShell CmdLets, New-Item, Set-Location and Get-ChildItem respectively.

An example of how permissions are set is below, with super-verbose comments. It is important to note that xcacls does most of this for you. If you were to look at the source code for xcacls.vbs (the script version of xcacls rather than the exe) you'd see it's really very complex.

Chris
# Get the current Access Control List so we can make changes
# This gives us a System.Security.AccessControl.DirectorySecurity object, a .NET class:
# http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx

$Acl = Get-Acl "C:\Stuff\NewFolder"

# Disable Inheritance and copy existing rules. This is a Method for this Object.
# SetAccessRuleProtection has two arguments:
#   isProtected         - Set to True if inheritance is disabled
#   preserveInheritance - Set to True when disabling inheritance and preserving rights
# Both of these are boolean values, $True or $False.

$Acl.SetAccessRuleProtection($True, $True)

# Create an access rule - another .NET class
#
# A constructor helps create an instance of an object, we will use the constructor for
# FileSystemAccessRule to create an object. Taken from this:
# http://msdn.microsoft.com/en-us/library/sfe70whw.aspx
#
# @( ) defines an array
#
# The constructor takes the following arguments (this is only one of 
# the possible constructors):
#   User             - Or a Group, or any other security principal.
#                      Generally in the form Domain\User. Also referred to as a Trustee.
#   AccessRights     - As an array, but we're only setting Modify this time.
#                      Taken from here:
#                      http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights.aspx
#   InheritanceFlags - As an array, this time we're setting two values.
#                      Taken from here:
#                      http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.inheritanceflags.aspx
#   PropagationFlags - Again, as an array, but no settings this time.
#                      Taken from here:
#                      http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.propagationflags.aspx
#   ACE Type         - A single value, Allow or Deny.
#                      Taken from here:
#                      http://msdn.microsoft.com/en-us/library/w4ds5h86.aspx

$AccessRule = New-Object Security.AccessControl.FileSystemAccessRule( `
  "domain\user", "Modify", @("ObjectInherit", "ContainerInherit"), $Null, "Allow")

# Add the AccessRule to the ACL by calling the AddAccessRule Method

$Acl.AddAccessRule($AccessRule)

# Finally, apply the changes

Set-Acl "C:\Stuff\NewFolder" -AclObject $Acl

Open in new window

Author

Commented:
Is the $Acl character a variable to you created on your own?

Author

Commented:
I'm sorry I guess I'm having a really hard time understand by basic concepts of powershell and O.O.P
Chris DentPowerShell Developer
Top Expert 2010

Commented:
>  Is the $Acl character a variable to you created on your own?

It is indeed, the $ generally denotes a variable in PowerShell. You can call it whatever you want really, I used $Acl because it's a descriptive name (the variable holds an Access Control List object).

You can see the pre-defined variables by looking at:

Get-Variable

That doesn't cover all of them, a few are reserved on top of those but it's a good start.

>  I'm sorry I guess I'm having a really hard time understand by basic concepts of powershell and O.O.P

Nothing you need apologise for :)

It takes time, I know I didn't get it when I first started out with PowerShell (2 years ago) and I'm sure my descriptions really don't do it justice. But then you don't necessarily need to understand OO languages. After all, it isn't a programming language even if it can be used like one, and understaning OO properly is very much in programming territory.

If you want to learn it, and learning it is a good idea, then start with simple things. Pick a task that's a bit repetitive, or pick a task that's more time consuming than it need be and make it into a learning exercise. Pick something that's a pain in the proverbial in Batch :)

For example, you might have a new user script, it can create the user and mailbox as well as creating any home drive. Or you might do something really simple like a command to figure out who is logged onto a PC. e.g.

(Get-WmiObject Win32_ComputerSystem -Computer SomeComputer).Username

Don't get too bogged down in the objects underneath because it'll make learning the language hard work unless that aspect really interests you.

Chris
If you want to start with PowerShell, just practice and read the startup guides.

http://msdn.microsoft.com/en-us/library/dd835506%28VS.85%29.aspx
http://powershell.com/Mastering-PowerShell.pdf
http://www.powershellpro.com/wp-content/uploads/WindowsPowershell-EN.pdf
http://www.computerperformance.co.uk/powershell/powershell_intro.htm

There are many free tutorials you can find on the Internet. Play with the basics and then maybe move on to things such as altering ACL, etc. Once you have the understanding of working with objects rather than text you will understand much more.
Senior Systems Admin
Top Expert 2010
Commented:
Compdigit, I just wrote an article on Powershell that was published here on EE. It should give you a little bit easier introduction into powershell and help you get started: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Windows_Server_2008/A_3296-The-Absolute-Beginner%27s-Guide-to-Powershell.html