https://github.com/dahlbyk/posh-git/blob/master/src/Utils.ps1#L102

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-6

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6

function Add-PoshGitToProfile {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter()]
[switch]
$AllHosts, [Parameter()]
[switch]
$AllUsers, [Parameter()]
[switch]
$Force, [Parameter(ValueFromRemainingArguments)]
[psobject[]]
$TestParams
) if ($AllUsers -and !(Test-Administrator)) {
throw 'Adding posh-git to an AllUsers profile requires an elevated host.'
} $underTest = $false $profileName = $(if ($AllUsers) { 'AllUsers' } else { 'CurrentUser' }) `
+ $(if ($AllHosts) { 'AllHosts' } else { 'CurrentHost' })
Write-Verbose "`$profileName = '$profileName'" $profilePath = $PROFILE.$profileName
Write-Verbose "`$profilePath = '$profilePath'" # Under test, we override some variables using $args as a backdoor.
if (($TestParams.Count -gt 0) -and ($TestParams[0] -is [string])) {
$profilePath = [string]$TestParams[0]
$underTest = $true
if ($TestParams.Count -gt 1) {
$ModuleBasePath = [string]$TestParams[1]
}
} if (!$profilePath) { $profilePath = $PROFILE } if (!$Force) {
# Search the user's profiles to see if any are using posh-git already, there is an extra search
# ($profilePath) taking place to accomodate the Pester tests.
$importedInProfile = Test-PoshGitImportedInScript $profilePath
if (!$importedInProfile -and !$underTest) {
$importedInProfile = Test-PoshGitImportedInScript $PROFILE
}
if (!$importedInProfile -and !$underTest) {
$importedInProfile = Test-PoshGitImportedInScript $PROFILE.CurrentUserCurrentHost
}
if (!$importedInProfile -and !$underTest) {
$importedInProfile = Test-PoshGitImportedInScript $PROFILE.CurrentUserAllHosts
}
if (!$importedInProfile -and !$underTest) {
$importedInProfile = Test-PoshGitImportedInScript $PROFILE.AllUsersCurrentHost
}
if (!$importedInProfile -and !$underTest) {
$importedInProfile = Test-PoshGitImportedInScript $PROFILE.AllUsersAllHosts
} if ($importedInProfile) {
Write-Warning "Skipping add of posh-git import to file '$profilePath'."
Write-Warning "posh-git appears to already be imported in one of your profile scripts."
Write-Warning "If you want to force the add, use the -Force parameter."
return
}
} if (!$profilePath) {
Write-Warning "Skipping add of posh-git import to profile; no profile found."
Write-Verbose "`$PROFILE = '$PROFILE'"
Write-Verbose "CurrentUserCurrentHost = '$($PROFILE.CurrentUserCurrentHost)'"
Write-Verbose "CurrentUserAllHosts = '$($PROFILE.CurrentUserAllHosts)'"
Write-Verbose "AllUsersCurrentHost = '$($PROFILE.AllUsersCurrentHost)'"
Write-Verbose "AllUsersAllHosts = '$($PROFILE.AllUsersAllHosts)'"
return
} # If the profile script exists and is signed, then we should not modify it
if (Test-Path -LiteralPath $profilePath) {
if (!(Get-Command Get-AuthenticodeSignature -ErrorAction SilentlyContinue))
{
Write-Verbose "Platform doesn't support script signing, skipping test for signed profile."
}
else {
$sig = Get-AuthenticodeSignature $profilePath
if ($null -ne $sig.SignerCertificate) {
Write-Warning "Skipping add of posh-git import to profile; '$profilePath' appears to be signed."
Write-Warning "Add the command 'Import-Module posh-git' to your profile and resign it."
return
}
}
} # Check if the location of this module file is in the PSModulePath
if (Test-InPSModulePath $ModuleBasePath) {
$profileContent = "`nImport-Module posh-git"
}
else {
$modulePath = Join-Path $ModuleBasePath posh-git.psd1
$profileContent = "`nImport-Module '$modulePath'"
} # Make sure the PowerShell profile directory exists
$profileDir = Split-Path $profilePath -Parent
if (!(Test-Path -LiteralPath $profileDir)) {
if ($PSCmdlet.ShouldProcess($profileDir, "Create current user PowerShell profile directory")) {
New-Item $profileDir -ItemType Directory -Force -Verbose:$VerbosePreference > $null
}
} if ($PSCmdlet.ShouldProcess($profilePath, "Add 'Import-Module posh-git' to profile")) {
Add-Content -LiteralPath $profilePath -Value $profileContent -Encoding UTF8
}
}

最新文章

  1. 【转】php Thread Safe(线程安全)和None Thread Safe(NTS,非 线程安全)之分
  2. C#的前世今生,学会C#还能找到高薪工作吗?
  3. 进程间通信 System V 消息队列
  4. FastReport 隐藏matrix的列如何实现
  5. 系统安装之:虚拟机VMware V12.0.1 专业版 + 永久密钥
  6. TODO软件工程--如何预算项目的工期
  7. tp_link路由器 重新设置
  8. Java数据结构漫谈-Vector
  9. 【原】vue单文件组件互相通讯
  10. 自定义view-滑动开关
  11. SAP S4系统创建Customer和Vendor的BAPI
  12. bash语法
  13. linux 性能分析
  14. CF719E. Sasha and Array [线段树维护矩阵]
  15. vue父子组件实现v-model
  16. 在ubuntu下搜索文件的几种方式
  17. uniGUI日志的控制
  18. PHP编译安装时常见错误解决办法,php编译常见错误
  19. 团队项目个人进展——Day07
  20. [uboot]uboot中run的一些command在源码位置

热门文章

  1. python mock模块使用(一)
  2. 【ITOO 3】.NET 动态建库建表:实用EF框架提供的codeFirst实现动态建库
  3. Poj1704:staircase nim【博弈】
  4. spring security3.1升级到4.1问题(1)访问/j_spring_security_check 404
  5. 单源最短路径 Bellman_ford 和 dijkstra
  6. msp430入门编程35
  7. Fractal---POJ2083(dfs)
  8. stored procedure --存储过程
  9. nexus-3本地下载jar的settipng.xml配置
  10. FIREDAC保存ORACLE的BLOB字段数据