PowerShell跨平台开发实战

智能路径转换

1
2
3
4
5
6
7
8
9
10
11
function Get-UniversalPath {
param([string]$Path)
if ($IsLinux) {
$Path.Replace('\', '/').TrimEnd('/')
} else {
$Path.Replace('/', '\').TrimEnd('\')
}
}

# 示例:转换C:/Users到Linux格式
Get-UniversalPath -Path 'C:\Users\Demo' # 输出 /mnt/c/Users/Demo

条件编译技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#region WindowsOnly
if ($PSVersionTable.Platform -eq 'Win32NT') {
Add-Type -AssemblyName PresentationCore
[System.Windows.Clipboard]::SetText($content)
}
#endregion

#region LinuxOnly
if ($IsLinux) {
$tempFile = New-TemporaryFile
$content | Out-File $tempFile
xclip -selection clipboard -in $tempFile
}
#endregion

原生命令封装

1
2
3
4
5
6
7
8
9
10
11
function Invoke-NativeCommand {
param([string]$Command)
if ($IsWindows) {
cmd.exe /c $Command
} else {
bash -c $Command
}
}

# 跨平台调用系统命令
Invoke-NativeCommand -Command 'echo $Env:COMPUTERNAME'

开发注意事项

  • 区分文件系统大小写敏感特性
  • 处理CRLF/LF行尾差异
  • 避免平台特定别名使用
  • 统一字符编码为UTF-8