一些 powershell 脚本

2020年5月13日 · 364 字 · 1 分钟 · Powershell

近期写的、收集的一些 pwsh 脚本,我完整的 pwsh 配置可看 batkiz/dotfiles

由于主要是为了满足个人使用,所以没怎么做错误处理之类的。

在 pwsh 中使用 wsl 中的 nvim

dos2nix 是将 windows 风格的路径转换为 wsl 中的路径。

function dos2nix {
    param($dosPath)

    $path = $dosPath.Replace('\', '/')

    if ($path -match '[a-zA-Z]:.*') {
        $drive = $path.split(':')[0].ToLower()
        $filePath = $path.split(':')[1]

        $nixPath = '/mnt/' + $drive + $filePath
    }
    else {
        $nixPath = $path
    }

    $nixPath
}

function vim {
    param (
        $Path = '.'
    )

    if ($Path -eq '.') {
        $Path = '.'
    }
    else {
        $Path = dos2nix -dosPath $Path
    }

    wsl -d debian -e nvim $Path
}

which

获取某个可执行文件的路径。

function which {
    $results = New-Object System.Collections.Generic.List[System.Object];
    foreach ($command in $args) {
        $path = (Get-Command $command).Source
        if ($path) {
            $results.Add($path);
        }
    }
    return $results;
}

ls

*nix 中的 ls 样式,可惜没搞出来着色。

function ListDirectory {
    Get-ChildItem $args | Format-Wide Name -AutoSize
}

Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem
Set-Alias -Name l -Value Get-ChildItem

related: powerls

nali

接收一个域名或 ip(-Query),返回其 ip 和地理位置信息。

默认是获取本地的 ip 和位置,语言可以切换英文或中文(默认是中文)。

function nali {
    param (
        $Query = '',
        [Alias('l')]
        $Lang = 'zh'
    )

    if ($Lang.ToLower() -eq 'en' ) {
        $Lang = 'en'
    }
    else {
        $Lang = 'zh-CN'
    }

    $ApiUrl = "http://ip-api.com/json/{0}?lang={1}" -f $Query, $Lang

    $info = (Invoke-WebRequest $ApiUrl).Content | ConvertFrom-Json

    $printInfo = "{0}`t[{1} @ {2}, {3}]" -f $info.query, $info.isp, $info.city, $info.country

    $printInfo
}

related: batkiz/nali-go