Commit 61f69f13 authored by O'Reilly Media, Inc's avatar O'Reilly Media, Inc
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+105 −0
Changes for Add-ExtendedFileProperties.ps1: 105 added lines, 0 removed lines.
Original line number Diff line number Diff line
##############################################################################
##
## Add-ExtendedFileProperties
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Add the extended file properties normally shown in Exlorer's
"File Properties" tab.

.EXAMPLE

PS > Get-ChildItem | Add-ExtendedFileProperties.ps1 | Format-Table Name,"Bit Rate"

#>

begin
{
    Set-StrictMode -Version 3

    ## Create the Shell.Application COM object that provides this
    ## functionality
    $shellObject = New-Object -Com Shell.Application
   
    ## Remember the column property mappings
    $columnMappings = @{}
}

process
{
    ## Store the property names and identifiers for all of the shell
    ## properties
    $itemProperties = @{}

    ## Get the file from the input pipeline. If it is just a filename
    ## (rather than a real file,) piping it to the Get-Item cmdlet will
    ## get the file it represents.
    $fileItem = $_ | Get-Item

    ## Don't process directories
    if($fileItem.PsIsContainer)
    {
        $fileItem
        return
    }

    ## Extract the file name and directory name
    $directoryName = $fileItem.DirectoryName
    $filename = $fileItem.Name

    ## Create the folder object and shell item from the COM object
    $folderObject = $shellObject.NameSpace($directoryName)
    $item = $folderObject.ParseName($filename)

    ## Populate the item properties
    $counter = 0
    $columnName = ""
    do
    {
        if(-not $columnMappings[$counter])
        {
            $columnMappings[$counter] = $folderObject.GetDetailsOf(
                $folderObject.Items, $counter)
        }
        
        $columnName = $columnMappings[$counter]
        if($columnName)
        {
            $itemProperties[$columnName] =
                $folderObject.GetDetailsOf($item, $counter)
        }

        $counter++
    } while($columnName)
    
    ## Process extended properties
    foreach($name in
        $item.ExtendedProperty('System.PropList.FullDetails').Split(';'))
    {
        $name = $name.Replace("*","")
        $itemProperties[$name] = $item.ExtendedProperty($name)
    }

    ## Now, go through each property and add its information as a
    ## property to the file we are about to return
    foreach($itemProperty in $itemProperties.Keys)
    {
        $value = $itemProperties[$itemProperty]
        if($value)
        {
            $fileItem | Add-Member NoteProperty $itemProperty `
                $value -ErrorAction `
                SilentlyContinue
        }
    }

    ## Finally, return the file with the extra shell information
    $fileItem
}
 No newline at end of file

Add-FormatData.ps1

0 → 100644
+67 −0
Changes for Add-FormatData.ps1: 67 added lines, 0 removed lines.
Original line number Diff line number Diff line
##############################################################################
##
## Add-FormatData
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Adds a table formatting definition for the specified type name.

.EXAMPLE

PS > $r = [PSCustomObject] @{
    Name = "Lee";
    Phone = "555-1212";
    SSN = "123-12-1212"
}
PS > $r.PSTypeNames.Add("AddressRecord")
PS > Add-FormatData -TypeName AddressRecord -TableColumns Name, Phone
PS > $r

Name Phone
---- -----
Lee  555-1212

#>

param(
    ## The type name (or PSTypeName) that the table definition should
    ## apply to.
    $TypeName,

    ## The columns to be displayed by default
    [string[]] $TableColumns
)

Set-StrictMode -Version 3

## Define the columns within a table control row
$rowDefinition = New-Object Management.Automation.TableControlRow

## Create left-aligned columns for each provided column name
foreach($column in $TableColumns)
{
    $rowDefinition.Columns.Add(
	    (New-Object Management.Automation.TableControlColumn "Left", 
            (New-Object Management.Automation.DisplayEntry $column,"Property")))
}

$tableControl = New-Object Management.Automation.TableControl
$tableControl.Rows.Add($rowDefinition)

## And then assign the table control to a new format view,
## which we then add to an extended type definition. Define this view for the
## supplied custom type name.
$formatViewDefinition = New-Object Management.Automation.FormatViewDefinition "TableView",$tableControl
$extendedTypeDefinition = New-Object Management.Automation.ExtendedTypeDefinition $TypeName
$extendedTypeDefinition.FormatViewDefinition.Add($formatViewDefinition)

## Add the definition to the session, and refresh the format data
[Runspace]::DefaultRunspace.InitialSessionState.Formats.Add($extendedTypeDefinition)
Update-FormatData
 No newline at end of file
+92 −0
Changes for Add-FormatTableIndexParameter.ps1: 92 added lines, 0 removed lines.
Original line number Diff line number Diff line
##############################################################################
##
## Add-FormatTableIndexParameter
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Adds a new -IncludeIndex switch parameter to the Format-Table command
to help with array indexing.

.NOTES

This commands builds on New-CommandWrapper, also included in the Windows
PowerShell Cookbook.

.EXAMPLE

PS > $items = dir
PS > $items | Format-Table -IncludeIndex
PS > $items[4]

#>

Set-StrictMode -Version 3

New-CommandWrapper Format-Table `
    -AddParameter @{
        @{
            Name = 'IncludeIndex';
            Attributes = "[Switch]"
        } = {

        function Add-IndexParameter {
            begin
            {
                $psIndex = 0
            }
            process
            {
                ## If this is the Format-Table header
                if($_.GetType().FullName -eq `
                    "Microsoft.PowerShell.Commands.Internal." +
                    "Format.FormatStartData")
                {
                    ## Take the first column and create a copy of it
                    $formatStartType =
                        $_.shapeInfo.tableColumnInfoList[0].GetType()
                    $clone =
                        $formatStartType.GetConstructors()[0].Invoke($null)

                    ## Add a PSIndex property
                    $clone.PropertyName = "PSIndex"
                    $clone.Width = $clone.PropertyName.Length

                    ## And add its information to the header information
                    $_.shapeInfo.tableColumnInfoList.Insert(0, $clone)
                }

                ## If this is a Format-Table entry
                if($_.GetType().FullName -eq `
                    "Microsoft.PowerShell.Commands.Internal." +
                    "Format.FormatEntryData")
                {
                    ## Take the first property and create a copy of it
                    $firstField =
                        $_.formatEntryInfo.formatPropertyFieldList[0]
                    $formatFieldType = $firstField.GetType()
                    $clone =
                        $formatFieldType.GetConstructors()[0].Invoke($null)

                    ## Set the PSIndex property value
                    $clone.PropertyValue = $psIndex
                    $psIndex++

                    ## And add its information to the entry information
                    $_.formatEntryInfo.formatPropertyFieldList.Insert(
                        0, $clone)
                }

                $_
            }
        }

        $newPipeline = { __ORIGINAL_COMMAND__ | Add-IndexParameter }
    }
}
 No newline at end of file
+65 −0
Changes for Add-ObjectCollector.ps1: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
##############################################################################
##
## Add-ObjectCollector
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Adds a new Out-Default command wrapper to store up to 500 elements from
the previous command. This wrapper stores output in the $ll variable.

.EXAMPLE

PS > Get-Command $pshome\powershell.exe

CommandType     Name                          Definition
-----------     ----                          ----------
Application     powershell.exe                C:\Windows\System32\Windo...

PS > $ll.Definition
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

.NOTES

This command builds on New-CommandWrapper, also included in the Windows
PowerShell Cookbook.

#>

Set-StrictMode -Version 3

New-CommandWrapper Out-Default `
    -Begin {
        $cachedOutput = New-Object System.Collections.ArrayList
    } `
    -Process {
        ## If we get an input object, add it to our list of objects
        if($_ -ne $null) { $null = $cachedOutput.Add($_) }
        while($cachedOutput.Count -gt 500) { $cachedOutput.RemoveAt(0) }
    } `
    -End {
        ## Be sure we got objects that were not just errors (
        ## so that we don't wipe out the saved output when we get errors
        ## trying to work with it.)
        ## Also don't caputre formatting information, as those objects
        ## can't be worked with.
        $uniqueOutput = $cachedOutput | Foreach-Object {
            $_.GetType().FullName } | Select -Unique
        $containsInterestingTypes = ($uniqueOutput -notcontains `
            "System.Management.Automation.ErrorRecord") -and
            ($uniqueOutput -notlike `
                "Microsoft.PowerShell.Commands.Internal.Format.*")

        ## If we actually had output, and it was interesting information,
        ## save the output into the $ll variable
        if(($cachedOutput.Count -gt 0) -and $containsInterestingTypes)
        {
            $GLOBAL:ll = $cachedOutput | % { $_ }
        }
    }
 No newline at end of file
+51 −0
Changes for Add-RelativePathCapture.ps1: 51 added lines, 0 removed lines.
Original line number Diff line number Diff line
##############################################################################
##
## Add-RelativePathCapture
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Adds a new CommandNotFound handler that captures relative path
navigation without having to explicitly call 'Set-Location'

.EXAMPLE

PS C:\Users\Lee\Documents>..
PS C:\Users\Lee>...
PS C:\>

#>

Set-StrictMode -Version 3

$executionContext.SessionState.InvokeCommand.CommandNotFoundAction = {
    param($CommandName, $CommandLookupEventArgs)

    ## If the command is only dots
    if($CommandName -match '^\.+$')
    {
        ## Associate a new command that should be invoked instead    
        $CommandLookupEventArgs.CommandScriptBlock = {

            ## Count the number of dots, and run "Set-Location .." one
            ## less time.
            for($counter = 0; $counter -lt $CommandName.Length - 1; $counter++)
            {
                Set-Location ..
            }

        ## We call GetNewClosure() so that the reference to $CommandName can
        ## be used in the new command.
        }.GetNewClosure()

        ## Stop going through the command resolution process. This isn't
        ## strictly required in the CommandNotFoundAction.
        $CommandLookupEventArgs.StopSearch = $true
    }
}
 No newline at end of file
Loading