SORU
26 EYLÜL 2008, Cuma


Eğer PowerShell `profil.ps1` dosya?

Ne önemli şeyler (fonksiyonlar, diğer adları, komut başlangıç) profili var mı?

CEVAP
3 Ocak 2010, Pazar


Ben sık sık bazı temel agregates bazı şeylerin toplamı/. Kont gerek kendim gerek bul Bu fonksiyonları tanımlanmış ve sık sık onları kullanıyorum. gerçekten çok hoş bir boru hattı sonunda çalışıyorlar :

#
# useful agregate
#
function count
{
    BEGIN { $x = 0 }
    PROCESS { $x  = 1 }
    END { $x }
}

function product
{
    BEGIN { $x = 1 }
    PROCESS { $x *= $_ }
    END { $x }
}

function sum
{
    BEGIN { $x = 0 }
    PROCESS { $x  = $_ }
    END { $x }
}

function average
{
    BEGIN { $max = 0; $curr = 0 }
    PROCESS { $max  = $_; $curr  = 1 }
    END { $max / $curr }
}

Zaman elde edebilmek için ve benim istemi renklerin yolu :

function Get-Time { return $(get-date | foreach { $_.ToLongTimeString() } ) }
function prompt
{
    # Write the time 
    write-host "[" -noNewLine
    write-host $(Get-Time) -foreground yellow -noNewLine
    write-host "] " -noNewLine
    # Write the path
    write-host $($(Get-Location).Path.replace($home,"~").replace("\","/")) -foreground green -noNewLine
    write-host $(if ($nestedpromptlevel -ge 1) { '>>' }) -noNewLine
    return "> "
}

Aşağıdaki fonksiyonları bir blogdan çalıntı ve benim zevkime uyacak şekilde değiştirilmiş, ama renkler mi çok güzel :

# LS.MSH 
# Colorized LS function replacement 
# /\/\o\/\/ 2006 
# http://mow001.blogspot.com 
function LL
{
    param ($dir = ".", $all = $false) 

    $origFg = $host.ui.rawui.foregroundColor 
    if ( $all ) { $toList = ls -force $dir }
    else { $toList = ls $dir }

    foreach ($Item in $toList)  
    { 
        Switch ($Item.Extension)  
        { 
            ".Exe" {$host.ui.rawui.foregroundColor = "Yellow"} 
            ".cmd" {$host.ui.rawui.foregroundColor = "Red"} 
            ".msh" {$host.ui.rawui.foregroundColor = "Red"} 
            ".vbs" {$host.ui.rawui.foregroundColor = "Red"} 
            Default {$host.ui.rawui.foregroundColor = $origFg} 
        } 
        if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "Green"}
        $item 
    }  
    $host.ui.rawui.foregroundColor = $origFg 
}

function lla
{
    param ( $dir=".")
    ll $dir $true
}

function la { ls -force }

Ve bazıları gerçekten tekrarlayan görevleri filtreleme önlemek için kısayollar :

# behave like a grep command
# but work on objects, used
# to be still be allowed to use grep
filter match( $reg )
{
    if ($_.tostring() -match $reg)
        { $_ }
}

# behave like a grep -v command
# but work on objects
filter exclude( $reg )
{
    if (-not ($_.tostring() -match $reg))
        { $_ }
}

# behave like match but use only -like
filter like( $glob )
{
    if ($_.toString() -like $glob)
        { $_ }
}

filter unlike( $glob )
{
    if (-not ($_.tostring() -like $glob))
        { $_ }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • iZAPPA

    iZAPPA

    16 Temmuz 2010
  • Jay Will

    Jay Will

    19 NİSAN 2006
  • NikkoNantone

    NikkoNantone

    21 Kasım 2011