你的 PowerShell `profile.ps1` 文件中有什么? [英] What’s in your PowerShell `profile.ps1` file?

查看:69
本文介绍了你的 PowerShell `profile.ps1` 文件中有什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您的个人资料中有哪些重要内容(函数、别名、启动脚本)?

解决方案

我经常发现自己需要一些基本的聚合来计算/求和某些事情.我已经定义了这些函数并经常使用它们,它们在管道的末端:

<预><代码>## 有用的聚合#功能计数{开始 { $x = 0 }过程{ $x += 1 }结束 { $x }}功能产品{开始 { $x = 1 }过程{ $x *= $_ }结束 { $x }}函数和{开始 { $x = 0 }过程{ $x += $_ }结束 { $x }}函数平均{开始 { $max = 0;$curr = 0 }过程{ $max += $_;$curr += 1 }结束 { $max/$curr }}

为了能够在我的提示中使用颜色获取时间和路径:

function Get-Time { return $(get-date | foreach { $_.ToLongTimeString() } ) }功能提示{# 写时间写主机 "[" -noNewLine写主机 $(Get-Time) -foreground 黄色 -noNewLine写主机]" -noNewLine# 写路径写主机 $($(Get-Location).Path.replace($home,"~").replace("\","/")) -foreground green -noNewLine写主机 $(if ($nestedpromptlevel -ge 1) { '>>' }) -noNewLine返回>"}

以下功能是从博客中偷来的,修改后符合我的口味,但是ls with colours 非常好:

# LS.MSH# 彩色LS函数替换#/\/\o\/\/2006# http://mow001.blogspot.com函数LL{参数 ($dir = ".", $all = $false)$origFg = $host.ui.rawui.foregroundColorif ( $all ) { $toList = ls -force $dir }else { $toList = ls $dir }foreach ($toList 中的 $Item){开关($Item.Extension){".exe" {$host.ui.rawui.foregroundColor = "黄色"}".cmd" {$host.ui.rawui.foregroundColor = "红色"}".msh" {$host.ui.rawui.foregroundColor = "红色"}".vbs" {$host.ui.rawui.foregroundColor = "红色"}默认 {$host.ui.rawui.foregroundColor = $origFg}}if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "Green"}$item}$host.ui.rawui.foregroundColor = $origFg}功能二{参数 ( $dir=".")ll $dir $true}函数 la { ls -force }

还有一些避免真正重复过滤任务的捷径:

# 行为类似于 grep 命令# 但在对象上工作,使用# 还可以使用grep过滤匹配( $reg ){if ($_.tostring() -match $reg){ $_ }}# 表现得像一个 grep -v 命令# 但在对象上工作过滤器排除( $reg ){if (-not ($_.tostring() -match $reg)){ $_ }}# 表现得像 match 但只使用 -like过滤器($glob){if ($_.toString() -like $glob){ $_ }}过滤器不同( $glob ){if (-not ($_.tostring() -like $glob)){ $_ }}

What essential things (functions, aliases, start up scripts) do you have in your profile?

解决方案

I often find myself needing needing some basic agregates to count/sum some things., I've defined these functions and use them often, they work really nicely at the end of a pipeline :

#
# 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 }
}

To be able to get time and path with colors in my prompt :

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 "> "
}

The following functions are stolen from a blog and modified to fit my taste, but ls with colors is very nice :

# 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 }

And some shortcuts to avoid really repetitive filtering tasks :

# 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))
        { $_ }
}

这篇关于你的 PowerShell `profile.ps1` 文件中有什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆