Friday 28 November 2014

Get-Stack1.ps1


<#
.SYNOPSIS
MSDN sample showing push and other stack processing using PowerShell
.DESCRIPTION
This script creates a script then performs stack operations.
.NOTES
File Name : Get-Stack1.p1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://www.pshscripts.blogspot.com
.EXAMPLE
PSH [C:\foo]: .\get-stack1.ps1'
Stack at start:
fox
quick
The

(Pop) fox
Stack value after Pop:
brown
quick
The

(Pop) brown
Stack values after 2nd pop:
quick
The

(Peek) quick
Stack values after a peek:
quick
The
#>

##
# start of script
###

# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( "The" )
$myStack.Push( "quick" )
$myStack.Push( "brown" )
$myStack.Push( "fox" )

# Display the Stack
"Stack at start:"
$myStack
""# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
"Stack value after Pop:"
$myStack
""

# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()

# Display the Stack after 2nd pop
"Stack values after 2nd pop:"
$myStack
""

# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()

# Display the Stack after the peek
"Stack values after a peek:"
$myStack

Thursday 27 November 2014

Zip-Pshscripts3.ps1

#Requires –Version 5.0
#
#.Synopsis
#    Creates a zip file of PowerShell scripts 
#.Description
#    The script creates a zip file containing all the files, 
#    recursing through the top level PowerShell Script Library folder.
#
#.Notes
#    This script require PowerShell V5 for the zip file cmdlets!
#    Author - Thomas Lee - tfl@psp.co.uk
#
#.Example
#    PS [c:\foo]>  .\Zip-PSHScripts3.ps1
#    Total files  : 347
#    ps1 files    : 342
#    txt files    : 1
#    other  files : 4


# Define what to zip and from where
$zipfile  = "C:\foo\ScriptLib.ZIP"
$zipfrom  = "C:\Users\tfl\Dropbox\PowerShell Script Library (master)"
$recurse  = "true"
$ziptoadd = "ps1"

# Check it out
if ( ! (Test-path -Path $zipfrom ))
{
  Write-Host 'scripts folder does not exist'
}

# Zip it up!
Try 
  {
    Compress-Archive -Path $zipfrom -DestinationPath $zipfile -CompressionLevel Optimal -Update
  }
Catch 
  {
  Write-Host ' Error Zipping up the script library'
  $Error[0]
  }

# Stats
$files = ls $zipfrom -file -recurse
$files_ps1 = $files | Where-Object Extension -eq '.ps1'
$files_txt = $files | Where-Object Extension -eq '.txt'
$files_other = $files | Where-Object { $_.extension -NE '.PS1' -and $_.Extension -ne '.txt'} 

"Total files  : {0}" -f $($files.count)
"ps1 files    : {0}" -f $($files_ps1.count)
"txt files    : {0}" -f $($files_txt.count)
"other  files : {0}" -f $($files_other.count)


# All done
ls $zipfile