分享我的powershell配置


#------------------------------- Import Modules BEGIN -------------------------------
# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
#oh-my-posh init pwsh | Invoke-Expression
#oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/peru.omp.json" | Invoke-Expression
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/peru.omp.json" | Invoke-Expression

# 引入 ps-read-line
Import-Module PSReadLine


#------------------------------- Import Modules END   -------------------------------





#-------------------------------  Set Hot-keys BEGIN  -------------------------------
# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# 设置 Tab 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
#-------------------------------  Set Hot-keys END    -------------------------------





#-------------------------------    Functions BEGIN   -------------------------------
# Python 直接执行
$env:PATHEXT += ";.py"

# 更新系统组件
function Update-Packages {
	# update pip
	Write-Host "Step 1: 更新 pip" -ForegroundColor Magenta -BackgroundColor Cyan
	$a = pip list --outdated
	$num_package = $a.Length - 2
	for ($i = 0; $i -lt $num_package; $i++) {
		$tmp = ($a[2 + $i].Split(" "))[0]
		pip install -U $tmp
	}

	# update TeX Live
	$CurrentYear = Get-Date -Format yyyy
	Write-Host "Step 2: 更新 TeX Live" $CurrentYear -ForegroundColor Magenta -BackgroundColor Cyan
	tlmgr update --self
	tlmgr update --all

	# update Chocolotey
	Write-Host "Step 3: 更新 Chocolatey" -ForegroundColor Magenta -BackgroundColor Cyan
	choco outdated
}
#-------------------------------    Functions END     -------------------------------





#-------------------------------   Set Alias BEGIN    -------------------------------
# 1. 编译函数 make
function MakeThings {
	nmake.exe $args -nologo
}
Set-Alias -Name make -Value MakeThings

# 2. 更新系统 os-update
Set-Alias -Name os-update -Value Update-Packages

# 3. 查看目录 ls & ll
function ListDirectory {
	(Get-ChildItem).Name
	Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem

# 4. 打开当前工作目录
function OpenCurrentFolder {
	param
	(
		# 输入要打开的路径
		# 用法示例:open C:\
		# 默认路径:当前工作文件夹
		$Path = '.'
	)
	Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder
#-------------------------------    Set Alias END     -------------------------------





#-------------------------------   Set Network BEGIN    -------------------------------
# 1. 获取所有 Network Interface
function Get-AllNic {
	Get-NetAdapter | Sort-Object -Property MacAddress
}
Set-Alias -Name wk -Value Get-AllNic

# 2. 获取 IPv4 关键路由
function Get-IPv4Routes {
	Get-NetRoute -AddressFamily IPv4 | Where-Object -FilterScript {$_.NextHop -ne '0.0.0.0'}
}
Set-Alias -Name ly4 -Value Get-IPv4Routes

# 3. 获取 IPv6 关键路由
function Get-IPv6Routes {
	Get-NetRoute -AddressFamily IPv6 | Where-Object -FilterScript {$_.NextHop -ne '::'}
}
Set-Alias -Name ly6 -Value Get-IPv6Routes

# 4. 获取DNS
function Get-Dns{
	$(get-netipconfiguration).dnsserver
}
Set-Alias -Name dns -Value Get-Dns

# 5.获取ip
function Get-GateWay{
	Get-NetIPConfiguration |findstr /i "gateway"
	}
Set-Alias -Name wg -Value Get-GateWay
Set-Alias -Name ip -Value Get-NetIPConfiguration

# 6.掩码
function Get-SubnetMask{
# 获取所有网络适配器
$networkAdapters = Get-NetIPConfiguration

foreach ($adapter in $networkAdapters) {
    # 获取网卡属性
    $adapterName = $adapter.InterfaceAlias
    $adapterDescription = $adapter.NetAdapter.InterfaceDescription
    $interfaceIndex = $adapter.InterfaceIndex
    $subnetPrefix = $adapter.IPv4Address.PrefixLength
    $macAddress = $adapter.NetAdapter.MacAddress

    # 获取IP地址
    $ipAddress = $adapter.IPv4Address.IPAddress

    # 获取网关
    $gateway = $adapter.IPv4DefaultGateway.NextHop

    # 获取DNS服务器
    $dnsServers = $adapter.DNSServer.ServerAddresses -join ', '

    # 将子网掩码前缀长度转换为二进制形式
    $binaryMask = "".PadLeft($subnetPrefix, '1').PadRight(32, '0')

    # 将二进制子网掩码转换为十进制形式
    $subnetMaskDecimal = ([Convert]::ToInt32($binaryMask.Substring(0,8), 2),
                          [Convert]::ToInt32($binaryMask.Substring(8,8), 2),
                          [Convert]::ToInt32($binaryMask.Substring(16,8), 2),
                          [Convert]::ToInt32($binaryMask.Substring(24,8), 2)) -join '.'

    # 输出结果
    Write-Output "网卡名称: $adapterName"
    Write-Output "网卡描述: $adapterDescription"
    Write-Output "接口索引: $interfaceIndex"
    Write-Output "IP地址: $ipAddress"
    Write-Output "子网掩码前缀长度: $subnetPrefix"
    Write-Output "子网掩码(十进制): $subnetMaskDecimal"
    Write-Output "网关: $gateway"
    Write-Output "DNS服务器: $dnsServers"
    Write-Output "MAC地址: $macAddress"
    Write-Output "--------------------------"
}

}


Set-Alias -Name ym -Value Get-SubnetMask

# 7. 检测端口
function Test-Port {
    param (
        [string]$ComputerName,
        [int]$Port
    )

    $connection = Test-NetConnection -ComputerName $ComputerName -Port $Port
    if ($connection.TcpTestSucceeded) {
        Write-Host "$ComputerName 端口:$Port 可用"
    } else {
        Write-Host "$ComputerName 端口:$Port 关闭"
    }
}

# 8. 多端口检测
function Test-Ports {
    param (
        [string]$ComputerName,
        [int[]]$Ports
    )

    foreach ($port in $Ports) {
        $connection = Test-NetConnection -ComputerName $ComputerName -Port $Port
        if ($connection.TcpTestSucceeded) {
            Write-Host "Port $port is open on $ComputerName"
        } else {
            Write-Host "Port $port is closed on $ComputerName"
        }
    }
}


#-------------------------------    Set Network END     -----------------------------




点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部