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 } }
作者:Chuck Lu GitHub |
分类:
PowerShell
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-09-07 Can not Stop-Computer in powershell 6.0
2017-09-07 powershell无法拖动文件到命令行
2017-09-07 How to Download Windows 10 Spotlight/Lock Screen Images
2016-09-07 nuget.exe the application could not be started
2016-09-07 choco命令
2016-09-07 How do I reset Windows Update components?
2015-09-07 How to convert an IPv4 address into a integer in C#?