wirte function in powershell
https://github.com/dahlbyk/posh-git/blob/master/src/Utils.ps1#L102
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的更多相关文章
- 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误
问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...
- CentOS 7 上面安装PowerShell
看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过程. 首先我们要从github上下载最新 ...
- PowerShell_零基础自学课程_6_PS中获取帮助信息详解、管道、格式化输
前些文章陆续的说了一些关于这些主题,但是讨论的都不够深入,今天我们深入的了解一下获取帮助信息.管道以及格式化输出的内容. 一.获取帮助信息 在PS中获取帮助信息,最常用的有: -? .get-comm ...
- JavaScript权威指南--脚本化文档
知识要点 脚本化web页面内容是javascript的核心目标. 第13章和14章解释了每一个web浏览器窗口.标签也和框架由一个window对象所示.每个window对象有一个document对象, ...
- Windows 挂起进程
A thread can suspend and resume the execution of another thread. While a thread is suspended, it is ...
- 通过百度echarts实现数据图表展示功能
现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...
- PowerShell随笔8 --- function
为了脚本逻辑的重复使用,我们更多时候会封装成方法.PowerShell的function和C#.JavaScript的定义有些区别. 我们直接看例子: 可以看到,定义方法并不是这样的: functio ...
- 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
问题描述 编写Powershell Function,登录到China Azure并获取Azure AD User信息,但是发现遇见了 [Error] ERROR: ManagedIdentityCr ...
- 【转】PowerShell 函数(Function)
转至:http://blog.csdn.net/kk185800961/article/details/49022395 函数基本操作: [plain] view plain copy #创建函数 F ...
随机推荐
- Android TransitionDrawable:过渡动画Drawable
Android TransitionDrawable实现一种可以用动画表示的Drawable.写一个例子. package zhangphil.app; import android.graphics ...
- 理工个人积分赛最后一场(FZU)G - 五子棋,坑爹的大水题~~
Problem 1490 五子棋 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 五子棋是起源于中国古代的传统黑白 ...
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
https://vjudge.net/contest/68966#problem/J [Accepted] #include<iostream> #include<cstdio> ...
- 【分段哈希】H. Paint the Wall
https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/H [题意] 在一个长为H,宽为W的白墙上选一个矩形区域涂颜色,后涂的颜色会覆盖先 ...
- POJ1256 Anagram
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu Submit Status Descript ...
- 【2018 Multi-University Training Contest 4】
01: 02:https://www.cnblogs.com/myx12345/p/9407551.html 03: 04:https://www.cnblogs.com/myx12345/p/940 ...
- mybatis连接mysql
配置web.xml 1. <context-param> 参考文章 <context-param> <param-name>contextConfigLoca ...
- 我的Github,个人博客
Github: github.com/wuxinwei 个人博客: blog.wuxinwei.org
- hdu 3237
dp 状态压缩 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- Spring事务管理概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/transaction-management.html: 数据库事务是一个被视为单一的工作单元的操作 ...