neděle 27. února 2011

History script powershell

V ukázce bude nové vylepšené start-demo powershell,verze 3.3.3,hlavně ale viz niže, script pro praci s historii,vylepšený export,import atd.

Start-demo mimochodem podporuje vylepšenou automatizaci – autoexecute mode a autospeed parametr a mnoho mnoho dalšího.Je to předělavka původního Jeffrey Snover's original Start-Demo script od Joel "Jaykul" Bennetta.

Jak pracuje start demo a jak si tam vkladat svoje sekvence je popsáno jak na strankach poshcode kde je upravené start-demo ke stažení tak někde,bůhvíkde o hodně přispěvků zpět na tomto blogu.

Na konci videa je ještě ukazka prace s profilem powershellu– jeho uprava ,vlastní alias,vložení funkce,volání funkce.

Slibovaný script pro práci s historií,význam má alespon pro mne je to tak ,pokud je v profilu.Autora a popis si vyhledejte – bude bud na pashcode nebo technet scriptcentru,změnil jsem nazev funkce a maxcount,takže kdo chce původní hodnoty může se po něm mrknout,vzhledem k tomu že ho mam v profilu původní popis scriptu už nemám.Nicmeně vychazet by se mělo z http://www.nivot.org/2009/08/15/PowerShell20PersistingCommandHistory.aspx

#region History archiving           

function exporthistory {
param ([string]$path=$historyPath)
$cmdArray = @()
if (Test-Path $path) {
  $savedHistory = @(Import-Clixml $historyPath)           

        $savedHistory | % { $cmdArray += $_.CommandLine }           

  Get-History -Count $MaximumHistoryCount | % {
                #first level of filtering
                if ($cmdArray -notcontains $_.CommandLine) { $savedHistory += $_ }            

                #Second level of filtering to remove duplicates from current session also
                $cmdArray = @()
                $savedHistory | % { $cmdArray += $_.CommandLine }
                }           

  $savedHistory | Export-Clixml $path
  Write-Host -ForegroundColor Green "`nExported history to $path along with old import`n"
} else {
  Get-History -Count $MaximumHistoryCount | Export-Clixml $path
  Write-Host -ForegroundColor Green "`nExported history to $path`n"
}
}           

function Import-History {
param ([parameter(mandatory=$true)][string]$path=$historyPath)
if (Test-Path $(Split-Path $path)) {
  Import-Clixml $path | ? {$count++;$true} | Add-History
  Write-Host -Fore Green "`nLoaded $count history item(s) from $path`n"
}
}           

function Get-HistoryArchives {
$historyArchives = @()
$historyItems = Get-ChildItem $(Split-Path $profile) History*.clixml
$historyItems | % {
  $archive = New-Object PSObject
  $archive | Add-Member NoteProperty Name $_.Name
  $archive | Add-Member NoteProperty FullName $_.FullName
  $archive | Add-Member NoteProperty CreatedOn ($_.LastWriteTime).GetDateTimeFormats()[18]
  $historyArchives += $archive
}
$historyArchives
}           

function Show-HistoryArhive {
param ([parameter(mandatory=$true)][string]$path)
if (Test-Path $path) {
  Import-Clixml $path
}
}           

#reset $MaximumHistoryCount to 300
$MaximumHistoryCount = 300      

#Generate Histry export path for this session
$date = Get-Date
#This is not so good. But OK for now
$historyPath = "$((split-path $profile))\History$($date.Month)$($date.Day)$($date.Year).clixml"           

# This is from Nivot Ink's (@oising) blog post http://www.nivot.org/2009/08/15/PowerShell20PersistingCommandHistory.aspx
Register-EngineEvent -SourceIdentifier powershell.exiting -SupportEvent -Action { Export-History }           

# load the most recent history, if it exists
if ((Test-Path $(Split-Path $profile))) {
try {
     if (Test-Path $historyPath) {
   Import-History $historyPath
  } else {
    Write-Host -Fore Red "`nNo command history to restore`n"
   }
}
catch {
  Write-Host -Fore Red "`nNo command history to restore`n"
}
}
#endregion

function prompt {
  $mapped_drives =    Get-WmiObject Win32_LogicalDisk -Filter "drivetype=4" | foreach {echo $_.deviceid}
  $local_drives =     Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" | foreach {echo $_.deviceid}
  $removable_drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=2" | foreach {echo $_.deviceid}
  $t = $(get-date -format "HH:mm:ss")
  $a = (get-location).path
  $d = (get-location).path.substring(0,$a.indexof(":")+1)
  $a = $a.substring($a.LastIndexOf("`\")+1)
  if ((get-location).path.substring(0,(get-location).path.indexof(":")) -eq "Microsoft.PowerShell.Core\FileSystem") {
    $a = (get-location).path
    $a = $a.substring($a.indexof(":")+2)
    write-host -fore white -back blue "$t - $a ";"`$`> "}
  else {
    if ($a -eq "") {$a = "`\"}
    if ($d.length -gt 2) {
      write-host -ForegroundColor black -backgroundcolor red "[$t] - [$d] $a ";"`$`> "}
    elseif ($local_drives -contains "$d") {
      write-host -ForegroundColor black -backgroundcolor green "[$t] - [$d] $a ";"`$`> "}
    elseif ($removable_drives -contains "$d") {
      write-host -ForegroundColor black -backgroundcolor yellow "[$t] - [$d] $a ";"`$`> "}
    elseif ($mapped_drives -contains "$d") {
      write-host -ForegroundColor black -backgroundcolor magenta "[$t] - [$d] $a ";"`$`> "}
  }
}