Sunday, April 14, 2013

Announcment! 2 Posts on Hey! Scripting Guy!

All,
On 12 March 2013 I attended a presentation by Ed Wilson, the author of Hey! Scripting Blog. To make a long story short, He asked me to write two articles for him to feature on his blog. These two articles where posted on the 13th and 14th of April 2013:
Weekend Scripter: Use PowerShell to Clean Out Temp Folders

http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/14/weekend-scripter-use-powershell-to-clean-out-temp-folders.aspx

Weekend Scripter: Pick Comments from a PowerShell Script

http://blogs.technet.com/b/heyscriptingguy/archive/2013/04/13/weekend-scripter-pick-comments-from-a-powershell-script.aspx

I fully encourage you to check them out yourself and please, comment and share them to your heart’s content!

Thank You,
Bob Stevens

Friday, March 29, 2013

Powershell Script DIY: Permissions


One of the most frustrating things with computers is permissions.  Not being able to access a file can quite often put a full stop to your Powershell adventures.  Microsoft has made this easy by giving us the ACL commandlets for our purpose today we will use these two commandlets:

               Get-ACL
               Set-ACL

The object is to take the contents of the Foo directory, which has the owner of Greg Foo and replace the permissions with administrator.  We start by defining variable.  First we need to find an object that we defiantly have permissions to.  The desktop is an excellent choice as we can read, write, and it most definitely exists for 99% of users. So we start with our variable and the Get-ACL command:

                $ACL = Get-ACL

We need to define the location we are pulling our permissions from so we are going to use the $home environmental variable.  This variable will return the path of the current working profile.  For me it is “C:\Documents and Settings\Administrator”.  This is not good enough.  We need the desktop so we append the “\Desktop” child directory to it.  Because we have a variable in the quotations we need to use double quotes otherwise Powershell will return the variable name:

                $ACL = Get-ACL “$home\Desktop

Now that the desired permissions are defined, we need to define what we want to apply them to.  For this we need the mighty Get-Childitem commandlet, and define our object as "C:\Foo"

                Get-Childitem “C:\Foo”

Because we want ALL Items in this folder, we need to tell Powershell to look in all subsequent folders with the “-recurse” operator and to select all hidden folders with the “-force” operators:

                Get-Childitem “C:\Foo”-recurse -force

Now that the files are selected, which is tantamount to CTRL+A with display hidden files on, we are going to dump the results into the Set-ACL command with a pipe (|) operator:

                Get-Childitem “C:\Foo”-recurse -force | Set-ACL

Because we already defined the object we are applying the permissions to we can move right along to the-aclobject operator.  This operator says apply the following permissions to the previous object.  The following being the $acl that we defined earlier in the article:

                Get-Childitem “C:\Foo”-recurse-hidden | Set-ACL-aclobject $acl

And there you have it.  We have successfully applied your permissions to this folder full of goodies that you need to accomplish your mission.  As usual, should you have any suggestions, or even a better way to do it, let me know!  And please don’t forget to share, +1, like, and tweet this post to all of your Powershell using compatriots below!



Tuesday, March 19, 2013

Powershell DIY: Install Powershell!


Below is the requirements and links to Powershell installations!  Please remember to meet all of the requirements before attempting to install Powershell.

Workstation Operating Systems:

Version
Operating systems supported
Requirements
- Windows XP
- Windows Vista
or
- Windows XP SP3
- Windows Vista SP1
- Windows 7*
-.NET Framework 3.0 for Powershell 2.0
-.NET Framework 3.5 for Powershell 2.0 ISE
Powershell 3.0***
- Windows 7 SP1
- Windows 8**
  Or

*Windows 7 has Powershell 2.0 and Powershell 2.0 ISE natively installed
**Windows 8 has Powershell 3.0 and Powershell 3.0 ISE natively installed
Server Operating Systems
*** Powershell comes in the Management Framework 3.0 package

Version
Operating systems supported
Requirements
- Windows Server 2003
- Windows Server 2008****

- Windows Server 2003 SP2
- Windows Server 2008 R2
-.NET Framework 3.0 for Powershell 2.0
-.NET Framework 3.5 for Powershell 2.0 ISE
Powershell 3.0***
- Windows Server 2008 SP1
- Windows Server 2008 R2 SP1
- Windows Server 2012

  Or

*** Powershell comes in the Management Framework 3.0 package
****Optional Feature

Monday, March 18, 2013

HISIMP! Announcement

As many of my readers may know I strive to learn more about the functionality of Powershell.  Recently after attending a presentation given by Ed Wilson on Powershell 3.0 best practices I was asked to write an article for his blog, Hey! Scripting Guy!  So on 4/13/2013 you will see my article on his blog!  I invite you all to view and leave your comments.

Tuesday, February 26, 2013

Powershell Script DIY: Creating Objects



An essential part of working with Powershell is the end result.  While it is all well and good to read the output on the screen with the write-output command, it’s easier to just dump it into a log or a text file for later analysis.   As usual we start with Set-Location:

Set-Location “c:\Temp

I like to start by defining a variable that will act as a placeholder for this file name.  This has two advantages, it shortens the amount of text that is gunking up your script, and you will only need to change one string to effect the entire script should you choose to change the file name:

$foo = "foo.txt

With the filename defined by a variable it is now time to actually create the file itself.  This is done with the New-Item command:

New-Item

The new Item’s name needs to be clearly defined directly after the command, and you must encapsulate it in quotations.  If you so choose, you can opt to just name the actual file name if you wish, Just replace the variable  “$foo” with “foo.txt.”:

                New-Item "$foo"

After you Define the file name the –itemType operator must be used to tell Powershell what exactly it is you want to create:

New-Item "$foo" –ItemType

As we are creating “foo.txt” It lends to reason that we would use the File object type:

New-Item "$foo" -itemType File

This is what your script should look like so far:

Set-Location “c:\Temp
$foo = "foo.txt
New-Item "$foo" -itemType File

With a few variations New-Item can also be used to create directories.  As with creating a file, we first define our variable:

                $FooFolder = “FooFiles”

And we use the  New-Item command to create the object:

                New-Item “$FooFolder”

Instead of using the File object type we use Directory:

                New-Item “$FooFolder” –ItemType Directory

Giving us this:

Set-Location “c:\Temp
$FooFolder = “FooFiles”
New-Item “$FooFolder” –ItemType Directory

Amazing! The fundamentals are there to create your folder, and then create the file within the folder:

Set-Location “c:\Temp
$FooFolder = “FooFiles”
New-Item “$FooFolder” –ItemType Directory
Set-Location $FooFolder
$foo = "foo.txt
New-Item "$foo" -itemType File

As usual, Please provide me with feedback; we only learn by working together! Also a +1/Like/Tweet/LinkIn always helps


Sources: Myself

Friday, February 15, 2013

A word on standardization for this blog


Greetings!  I feel it is time for me to set down a set of standards for my blog so that none get confused.  I have renamed and reformatted all of my blog entries for the sake of standardization. 

11.       All Powershell specific posts will be labeled as “Powershell Script DYI” for Search Engine Optimization.

22.       All variables and filenames will be some variation of “Foo.”  “Foo” is an industry standard catch all term describing a generic file.  I use this for the sake of standardization

33.       All IP addresses will be 192.168.1.1 255.255.255.0.  This is so that if you do use my scripts, you do not accidentally get into a protected network.

44.       The folder we work out of will always be c:\Temp. 

55.       All usernames will be either John Reginald Doe or Jane Renee Doe

66.       All commands / code will be indented and Italicized and all variables/objects will be bolded.


Thank You & Have an excellent day!

Wednesday, February 13, 2013

Powershell Script DIY:Naming Conventions


Powershell Script DIY: Naming Conventions

Greetings and welcome to “Help! I’m stuck in my Powershell!”  Today we address an important issue that plagues all Powershell scripter’s: The dreaded “I have 44 different script files named some variation of "Foo.PS1!” A naming convention is a documented system of naming files, variables, folders,   ect.  Naming convention in a production environment is paramount as it helps reduce confusion when you have multiple scripts you are working on.  Here I will help you with a Simple naming convention for a file, a folder, and a script. But first, I will define what can, and cannot be used as an Object name in windows:

File / folder limitations & why they are in place:

/  and \
used for directory paths
? and *
Used as wildcards
< and  >
Used for system functions
:
Used to define a port when referencing a port location
|
Used to string commands together
Used to define a path or file that contains spaces
Any name over 255 characters long (Fat32) or 256 characters long (NTFS)
The maximum file name can only be 260 characters long, including extension.
Space at the end of a name
A space indicates a separation, while allowed inside a name it creates system conflicts at the end
Period at the end of a name
A period indicates a separation between the name and the extension
com1, com2, com3, com4, com5, com6, com7, com8, com9, lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, lpt9, con, nul, and prn
Reserved by windows for system use
(Source: http://support.grouplogic.com/?p=1607)


Now that we have defined what you cannot do, let’s look at some of the things you can use in the naming convention:
Purpose
Computer name
Date
Server name
Time
Physical location
Version number
Group Name
Author
Data Source

FILE Naming Conventions:
When creating a file naming convention you must first define the intent of the file.  Our purpose is to house source data from our source “foo”:

Foosourcedata

I prefer to separate portions of my name with either – or _: 

Foosourcedata_

The source of our data is Server1:

Foosourcedata_Server1_

I like to include the date-time group:

Foosourcedata_Server1_01JAN2013_1200

And the important thing to add at the end is the extension.  An extension is not necessary for Powershell to pull data; rather it is useful for debugging.  I prefer to use the *.log for source data:

Foosourcedata_Server1_01JAN2013_1200.log

So we used “Intent_Source_Date_Time.extention” Remember, this is only an example, and you do not need to follow this example to the letter.

Folder Naming Conventions:
Folders are a slightly different case as their purpous and function are fundamentally different.  With that being said, instead of starting with “intent,” we will start with “contains.”  Defining what your folder contains is important, as it helps navigation to the files it contains.  This example will use “Foo source data files”:

Foo source data files

If this is a sub directory, you can append the group, or variable that differentiates those files contained within from the rest of the files:

Foo source data files_Server1

Additional items can be added at the end, but remember that the folder name should generally describe everything it contains.

Script Naming Conventions
As scripts are inherently files, they can be treated as such when naming them, with a few caveats:  The file name should start with its purpose and not what it contains, and you should always end it with a version number.  To start we will use fooscript to describe its purpose:

Fooscript

It is a good idea to follow this with the author’s initials if you are working in a multi-admin environment:

Fooscript_JRD

Instead of a date I prefer using a version number (I will explain more on this later on in the post):

Fooscript_JRD_1.0.1

If this is a work in progress you can append “Working” after this, while it is not required, it is a good idea if you work on a shared system, or the script resides on a network drive:

Fooscript_JRD_1.0.1_Working

Finally, you should append the extention particular to your scripting language.  We use Powershell here, so the extension is “*.PS1”:

Fooscript_JRD_1.0.1_Working.PS1

 The version number is an unofficial industry number that is delimited by periods used to identify the progress of the script.  Normally, there are 3 values:

1.2.3

The first number, “1” indicates major changes.  This should only change if there is an major revision to the code.  The second number, “2” refers to Incremental change.  This means an added minor feature, or changing out a block of code for a more effective block without fundamentally altering the function of the script.  The final number, “3” indicates a bug fix, i.e. if the script crashes under specific circumstances.

As always, I encourage commenting & sharing.  It does no one good if we keep knowledge and resources to our self! 


Sources: http://support.grouplogic.com/?p=1607