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
}
}

wirte function in powershell的更多相关文章

  1. 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误

    问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...

  2. CentOS 7 上面安装PowerShell

    看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过程. 首先我们要从github上下载最新 ...

  3. PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输

    前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...

  4. JavaScript权威指南--脚本化文档

    知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...

  5. Windows 挂起进程

    A thread can suspend and resume the execution of another thread. While a thread is suspended, it is ...

  6. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  7. PowerShell随笔8 --- function

    为了脚本逻辑的重复使用,我们更多时候会封装成方法.PowerShell的function和C#.JavaScript的定义有些区别. 我们直接看例子: 可以看到,定义方法并不是这样的: functio ...

  8. 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed

    问题描述 编写Powershell Function,登录到China Azure并获取Azure AD User信息,但是发现遇见了 [Error] ERROR: ManagedIdentityCr ...

  9. 【转】PowerShell 函数(Function)

    转至:http://blog.csdn.net/kk185800961/article/details/49022395 函数基本操作: [plain] view plain copy #创建函数 F ...

随机推荐

  1. 使用JQuery.slideBox实现图片滚动效果

    1.下载JQuery.slideBox和jquery插件,并引用 <link href="css/jquery.slideBox.css" rel="stylesh ...

  2. OO第二次作业

    第一次作业: 由于第一次作业的调度较为简单,采用FIFO策略,以及不支持捎带功能,因此我的第一次电梯作业并没有设置单独的调度器,而会直接将任务交给电梯,电梯进行调度策略也仅为先运动到people的In ...

  3. Linux怎么读? Linux读音考古一日游

    Linux怎么读?  Linux读音考古一日游/*凡是准备踏入Linux大门的叉子们(N年不关注了,不知道这个称呼是否还有),都必须经历疑问 那就是linux到底怎么读? 也许有些人很容易 什么里纽克 ...

  4. 【HDOJ6308】Time Zone(模拟)

    题意: 以"UTC+X'', "UTC-X'', "UTC+X.Y'', or "UTC-X.Y'' 四种格式给定当地时间,要求转换为北京时间 思路:Gold_ ...

  5. 12.3——类作用域,构造函数,友元,static类成员

    类作用域: (1)成员函数在类外定义时,因为函数体还有形参列表都出现在成员名之后,都是在类作用域内定义,所以不用加域作用符 来引用其他的成员. (2)函数的返回值不一定需要在类的作用域中,但是若是返回 ...

  6. hdu 3943

    数位dp #include <cstdio> #include <cstdlib> #include <cmath> #include <stack> ...

  7. BZOJ4555求和(cdq分治+NTT)

    题意: 输出f(n)对998244353(7 × 17 × 223 + 1)取模的结果.1 ≤ n ≤ 100000 其中S(i,j)是第二类Stirling数,即有i个球,丢到j个盒子中,要求盒子不 ...

  8. mysql.bat

    cls  @echo off :设置窗口字体颜色 color 0a  :设置窗口标题 TITLE MySQL管理程序    call :checkAdmin    goto menu :菜单 :men ...

  9. 地球Gauss_Kruger中央0度经线图

  10. 外排序 & 败者树 & 多路归并-学习

    来来来,根据这篇文章,学一下败者树吧: http://blog.csdn.net/whz_zb/article/details/7425152 一.胜者树 胜者树的一个优点是,如果一个选手的值改变了, ...