我平时喜欢在一个文件夹里创建很多平时用到程序的快捷方式,然后通过快捷方式运行配备了各种命令行参数的程序。
问题是有时候切换branch有些频繁,导致手工创建这些快捷方式很繁琐,所以,写个power shell的脚本,自动生成快捷方式。
__gen.ps1
echo "Generate shortcuts started" # Path $basePath = "SomeBasePath" $currPath = Split-Path -Parent $MyInvocation.MyCommand.Definition echo "basePath: $basePath" echo "currPath: $currPath" # move to the current path so shortcuts is created here CD $currPath # clean old shortcuts del *.lnk # shortcuts defined here: $shortcuts = "$currPath\1.lnk", #快捷方式 "$basePath\Binaries\xx.exe", #exe "-aaa -bbb", #参数 "$basePath\Binaries", #"Start in" 程序运行时的当前目录 "$currPath\1-ds.lnk", "$basePath\Binaries\xx.exe", "-aaa -bbb -ds", "$basePath\Binaries" # wscript.shell com object --其实还是用了com object,只不过我们通过power shell来操纵它 $shell = New-Object -com "wscript.shell" $i = 0 while($i -lt $shortcuts.length) { echo $shortcuts[$i] $lnk = $shell.CreateShortcut($shortcuts[$i++]) $lnk.TargetPath = $shortcuts[$i++] $lnk.Arguments = $shortcuts[$i++] $lnk.WorkingDirectory = $shortcuts[$i++] $lnk.Save() }
值得注意的是如果当前windows不允许执行powershell脚本,那么需要设置一下:
- Set-ExecutionPolicy unrestricted
这是因为默认的 Windows PowerShell 策略不允许执行脚本,我们通过上面的命令更改默认策略
- Get-ExecutionPolicy 表示获得当前环境执行powershell脚本的权限,
这种权限有4个值
- Restricted 默认设置,不允许运行任何脚本
- AllSigned 仅运行受信任脚本
- RemoteSigned 运行本地脚本,不管这些脚本是否受信任
- Unrestricted 允许运行所有脚本,甚至是不受信任的
一般状况下我们把级别设置为RemoteSigned这样方便脚本调试运行。