#修改适配前务必找到正确路径,可能版本不对
#消除一个微软的pwsh启动bug,使在开始菜单启动pwsh时会进入home作为工作目录
if(($PWD).Path -eq "C:\Program Files\WindowsApps\Microsoft.PowerShell_7.5.3.0_x64__8wekyb3d8bbwe" -or
($PWD).Path -eq "C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy" -or
($PWD).Path -eq "C:\Windows\System32"){
Set-Location $HOME
}
#挂载WSL virtual network drive file system to PSdrive
#这样做可以使用命令ls wsl:
try {
if($null -eq (get-psdrive dev -ErrorAction SilentlyContinue).name){new-psdrive -Name "dev" -PSProvider FileSystem -Root "\\WSL$\dev"}
if($null -eq (get-psdrive desk -ErrorAction SilentlyContinue).name){new-psdrive -Name "desk" -PSProvider FileSystem -Root "\\WSL$\dev"}
}
catch {
<#Do this if a terminating exception happens#>
}
#环境设置
#scoop 方便安装scoop,也可以设置在其他位置比如 C:\users\bakam\onedrive\scoop
$env:scoop = 'C:\users\bakam\scoop'
$env:http_proxy = 'http://127.0.0.1:10808'
$env:https_proxy = 'http://127.0.0.1:10808'
$env:all_proxy = 'socks5://127.0.0.1:10808'
# 定义self变量为个人scoop bucket git仓库路径
$self = "C:\Users\bakam\oneDrive\Desktop\self-scoop-bucket"
#所有配置,使用mklink创建链接让OneDrive同步
$config = "C:\Users\bakam\OneDrive\Desktop\all_config\"
#windows开发目录
$dev = "C:\Users\bakam\OneDrive\Dev"
# 设置PowerShell默认编码为UTF-8,确保脚本和输出文件使用UTF-8编码, 消除一些奇怪的中文输入bug
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
# 设置控制台输出编码为UTF-8,确保在控制台中正确显示UTF-8字符。
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
##定义快捷键Ctrl+w,快速输入exit并执行,退出当前PowerShell会话
Set-PSReadLineKeyHandler -Key Ctrl+w -LongDescription "exit" -ScriptBlock {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("exit")
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
# 定义exp进入本工作目录下的explorer快捷命令
function exp {explorer $pwd}
function py {uv run $args }
# 定义 claude 函数,调用 WSL 中的 claude 命令,切换到 /home/bakamake 目录后执行
# 使用 wsl.exe -e /bin/bash -c "cd /home/bakamake && claude" 确保正确的目录上下文
# 确保 WSL 已安装,claude CLI (@anthropic-ai/claude-code) 位于 PATH 中
function claude {
param (
# 接受所有剩余参数,传递给 claude 命令
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Arguments
)
try {
# 构建 WSL 命令,切换到 /home/bakamake 后运行 claude
if ($Arguments) {
# 转义参数以防止 shell 注入
$escapedArgs = $Arguments | ForEach-Object { "'$($_ -replace "'", "'\\''")'" }
$command = "cd /home/bakamake && claude $escapedArgs"
wsl.exe -e /bin/bash --login -i -c $command 2>&1 | Out-String
} else {
wsl.exe -u bakamake -d debian -- bash -c "cd ~ && claude"
}
}
catch {
# 捕获并显示错误信息,提供具体诊断
Write-Error "Failed to execute claude command: $_"
Write-Host "Troubleshooting steps:" -ForegroundColor Cyan
Write-Host "- Verify WSL is installed and configured: Run 'wsl --list --verbose'" -ForegroundColor Cyan
Write-Host "- Ensure claude CLI is installed: Run 'wsl.exe -e which claude'" -ForegroundColor Cyan
Write-Host "- Check claude usage: Run 'wsl.exe -e claude --help'" -ForegroundColor Cyan
Write-Host "- 如果还没有在wsl安装claude和npm"
Write-Host " sudo apt update && sudo apt install nodejs npm -y" -ForegroundColor Cyan
Write-Host " npm install -g @anthropic-ai/claude-code" -ForegroundColor Cyan
}
}
## scoop-search工具替代scoop search原生命令
. ([ScriptBlock]::Create((& scoop-search --hook | Out-String)))
# 导入Visual Studio开发者命令行环境模块,并启用模块
#可选命令:cl,link,lib
$vsEnvSetState = $env:VSINSTALLDIR -and $env:VCINSTALLDIR
if (-not $vsEnvSetState) {
Write-Host "初始化 Visual Studio 开发环境..." -ForegroundColor Yellow
Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\18\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -InstallPath "C:\Program Files (x86)\Microsoft Visual Studio\18\BuildTools" -DevCmdArguments "-arch=x64 -host_arch=x64"
} else {
Write-Host "Visual Studio 环境已就绪" -ForegroundColor Green
}
#不知道为什么我的Microsoft.VisualStudio.DevShell模块没添加dotnet执行相关到path用来调用dotnet命令
if ($env:Path -notmatch "C:\\Program Files\\dotnet\\") {
$env:Path += ";C:\Program Files\dotnet\"
}
if ($env:Path -notmatch "C:\\Program Files\\Microsoft Office\\root\\Office16\\") {
$env:Path += ";C:\Program Files\Microsoft Office\root\Office16\"
}
Write-Output "查看环境变量请使用以下命令:"
Write-Output "tip : ls env:"
Write-Output "tip : ls HKLM::"
Write-Output "tip : Get-PSDrive 查看可以和上面一样的虚拟驱动器,"
Write-Output "psdrive抽象了一些东西用来帮助pwsh 命令更好的集成他们"
Write-Output "tip : `$env:PATH -split ';'"
Write-Output "tip : rr 命令将文件夹/文件转移到`$Recycle"
set-Alias la Format-List
Write-Output "tip : 加上管道 | la 或者 | fl 展开被 ... 折叠的pwsh命令对象"
write-Output "tip : Get-PSReadLineKeyHandler | fl 查看所有快捷键绑定"
write-Output "tip : curl 会带上环境变量代理, curl -v https://google.com debug 代理环境下 http 请求, "
# 定义函数:将文件/文件夹移到回收站
# 该函数使用了 Microsoft.VisualBasic.FileIO 命名空间的方法
# 来实现将指定路径的文件或文件夹移动到回收站的功能
# 这个命名空间的年龄有点大,而且不够安全,似乎不会认可文件和文件夹权限,不要用这个命令删除高权限文件和文件夹
# 未来微软可能会淘汰掉这个命名空间,届时需要寻找替代方案
# Remove-toRecycle 安全性补丁: 验证删除对象的权限
function Test-FileSafeToDelete {
param([string]$Path)
if (-not (Test-Path $Path)) { return $false }
try { [System.IO.File]::OpenWrite($Path).Close() }
catch { return $false }
$user = $env:USERNAME
$acl = Get-Acl $Path
$hasDeny = $acl.Access | Where-Object {
$_.AccessControlType -eq "Deny" -and
$_.IdentityReference -like "*$user*" -and
$_.FileSystemRights -match "Delete"
}
if ($hasDeny) { return $false }
$hasAllow = $acl.Access | Where-Object {
$_.AccessControlType -eq "Allow" -and
$_.IdentityReference -like "*$user*" -and
$_.FileSystemRights -match "FullControl|Modify|Delete"
}
return ($null -ne $hasAllow)
}
function Remove-toRecycle {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string[]]$Path,
[switch]$Force,
[switch]$Recurse
)
begin {
# 加载必要的程序集
# 只在函数开始处理管道输入前加载一次程序集
Add-Type -AssemblyName Microsoft.VisualBasic
}
process {
foreach ($item in $Path) {
$fullpath = (Get-Item -Path $item -ErrorAction SilentlyContinue).FullName
$Deletesecurity = Test-FileSafeToDelete -Path $fullpath
if ($Deletesecurity -eq $True -or $Force) {
if ($fullpath) {
if (Test-Path -Path $fullpath -PathType Container) {
# 处理文件夹
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory(
$fullpath,
[Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs,
[Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin,
[Microsoft.VisualBasic.FileIO.UICancelOption]::DoNothing
)
} else {
# 处理文件
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile(
$fullpath,
[Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs,
[Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin,
[Microsoft.VisualBasic.FileIO.UICancelOption]::DoNothing
)
}
} else {
Write-Error "'$item' 未找到"
}
}
else {
Write-Error "'$fullpath' 无删除权限,使用 -Force 参数强制删除 ,警告:可能会导致系统不稳定,最好手动检查而非使用此私有命令 "
}
}
}
}
# 设置 rm 别名为该函数(AllScope 确保在所有作用域生效)
Set-Alias rr Remove-toRecycle -Option AllScope
remove-alias rm
Set-Alias rm Remove-toRecycle -Option AllScope
function Test-ProgramsRunning {
param(
[Parameter(Mandatory=$true)]
[string]$Path,
[string[]]$IncludeExtensions = @('.exe', '.com', '.bat', '.cmd', '.ps1'),
[string[]]$ExcludeExtensions = @('.dll', '.config', '.manifest'),
[switch]$ShowOnlyRunning
)
begin {
$script:SkipExecution = $false
# 转换为绝对路径(处理相对路径)
$resolvedPath = Resolve-Path $Path -ErrorAction Stop | Select-Object -ExpandProperty Path
if ($resolvedPath -match '^(C:\\Windows|C:\\Program Files|C:\\Program Files \(x86\))') {
Write-Error "出于安全考虑,禁止检查系统目录: $Path"
$script:SkipExecution = $true
}
if ($resolvedPath -eq 'C:\') {
Write-Error "出于安全考虑,禁止检查整个系统盘根目录: $Path"
$script:SkipExecution = $true
}
}
process{
if ($script:SkipExecution) {
exit 1
}else {
try {
Write-Host "检查路径: $resolvedPath" -ForegroundColor Cyan
Write-Host "=" * 50 -ForegroundColor Cyan
# 检查路径是否存在
if (-not (Test-Path $resolvedPath)) {
Write-Error "路径不存在: $resolvedPath"
return
}
# 递归获取所有可执行文件
$exeFiles = Get-ChildItem -Path $resolvedPath -Recurse -File -ErrorAction SilentlyContinue |
Where-Object {
$_.Extension -in $IncludeExtensions -and
$_.Extension -notin $ExcludeExtensions
}
if ($exeFiles.Count -eq 0) {
Write-Warning "在路径中未找到可执行文件"
return
}
Write-Host "找到 $($exeFiles.Count) 个可执行文件" -ForegroundColor Yellow
Write-Host ""
# 获取当前运行的所有进程(缓存以提高性能)
$runningProcesses = Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.MainModule } |
Select-Object -ExpandProperty MainModule |
Select-Object -ExpandProperty FileName
$runningCount = 0
foreach ($file in $exeFiles) {
$isRunning = $file.FullName -in $runningProcesses
if ($isRunning) {
$runningCount++
$processInfo = Get-Process | Where-Object { $_.MainModule.FileName -eq $file.FullName }
Write-Host "✓ 正在运行: $($file.Name)" -ForegroundColor Green
Write-Host " 完整路径: $($file.FullName)"
$processInfo | Format-List
Write-Host ""
} elseif (-not $ShowOnlyRunning) {
Write-Host "✗ 未运行: $($file.Name)" -ForegroundColor Red
Write-Host " 完整路径: $($file.FullName)"
Write-Host ""
}
}
Write-Host "=" * 50 -ForegroundColor Cyan
Write-Host "$runningCount/$($exeFiles.Count) 个程序正在运行" -ForegroundColor Cyan
} catch {
Write-Error "错误: $($_.Exception.Message)"
}}
}
end {
Write-Host "检查完成。" -ForegroundColor Cyan
}
}
Set-Alias tpr Test-ProgramsRunning -Option AllScope
write-Host "检查校园网环境" -ForegroundColor Yellow
curl -v --silent --stderr - -v 10.26.192.3 | Select-String Location
$env:MINIMAX_API_KEY="此处隐藏" #为了claude和codex使用