lit*_*lit 4 powershell powershell-v6.0
在 Windows 上,不包括 ISE 或 x86,有四 (4) 个配置文件脚本。
AllUsersAllHosts @ C:\Program Files\PowerShell\6\profile.ps1
AllUsersCurrentHost @ C:\Program Files\PowerShell\6\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts @ C:\Users\lit\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost @ C:\Users\lit\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Run Code Online (Sandbox Code Playgroud)
在带有 pwsh 6.2.0 的 Linux 上,我只能找到两个位置。
CurrentUserAllHosts @ ~/.config/powershell/Microsoft.PowerShell_profile.ps1
CurrentUserCurrentHost @ ~/.config/powershell/profile.ps1
Run Code Online (Sandbox Code Playgroud)
Linux 上是否有任何“AllUsers”配置文件脚本?如果有,他们在哪里?
奥拉夫在评论中提供了关键的指针:
$profile | select * # short for: $profile | Select-Object -Property *
Run Code Online (Sandbox Code Playgroud)
显示所有配置文件位置,无论单个配置文件是否存在。
例如,在我安装了 PowerShell 的 Ubuntu 机器上/home/jdoe/.powershell
,我得到:
AllUsersAllHosts : /home/jdoe/.powershell/profile.ps1
AllUsersCurrentHost : /home/jdoe/.powershell/Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : /home/jdoe/.config/powershell/profile.ps1
CurrentUserCurrentHost : /home/jdoe/.config/powershell/Microsoft.PowerShell_profile.ps1
Length : 62
Run Code Online (Sandbox Code Playgroud)
请注意[string]
类型的标准Length
属性的存在,如果您使用它,您可以省略$profile | select *host*
。
鉴于这$profile
是一个字符串变量(类型[string]
),您可以通过这种方式获取配置文件位置并不明显。
PowerShell[string]
使用NoteProperty
反映所有配置文件位置的成员装饰该实例,这就是select
( Select-Object
) 能够提取它们的原因。
仅输出$profile
- 即字符串值- 产生/home/jdoe/.config/powershell/Microsoft.PowerShell_profile.ps1
,即与其CurrentUserCurrentHost
属性相同的路径。[1]
您可以使用反射来验证这些属性的存在,如下所示(也显示了它们的值):
$profile | Get-Member -Type NoteProperty
Run Code Online (Sandbox Code Playgroud)
这意味着您还可以使用常规属性访问和选项卡完成来检索单个配置文件位置;例如:
# Use tab-completion to find a specific profile location.
# Expands to .Length first, then cycles through the profile-location properties.
$profile.<tab>
# Open the all-users, all-hosts profiles for editing.
# Note: Only works if the file already exists.
# Also, you must typically run as admin to modify all-user profiles.
Invoke-Item $profile.AllUsersAllHosts
Run Code Online (Sandbox Code Playgroud)
下面的代码定义:
Get-Profile
枚举配置文件,显示它们的位置以及它们是否存在于给定的机器上。
Edit-Profile
打开配置文件进行编辑(用于-Force
按需创建);请注意,修改所有用户配置文件通常需要以管理员身份运行。
function Get-Profile {
<#
.SYNOPSIS
Gets the location of PowerShell profile files and shows whether they exist.
#>
[CmdletBinding(PositionalBinding=$false)]
param (
[Parameter(Position=0)]
[ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
[string[]] $Scope
)
if (-not $Scope) {
$Scope = 'AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost'
}
foreach ($thisScope in $Scope) {
[pscustomobject] @{
Scope = $thisScope
FilePath = $PROFILE.$thisScope
Exists = (Test-Path -PathType Leaf -LiteralPath $PROFILE.$thisScope)
}
}
}
function Edit-Profile {
<#
.SYNOPSIS
Opens PowerShell profile files for editing. Add -Force to create them on demand.
#>
[CmdletBinding(PositionalBinding=$false, DefaultParameterSetName='Select')]
param (
[Parameter(Position=0, ValueFromPipelineByPropertyName, ParameterSetName='Select')]
[ValidateSet('AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost')]
[string[]] $Scope = 'CurrentUserCurrentHost'
,
[Parameter(ParameterSetName='All')]
[switch] $All
,
[switch] $Force
)
begin {
$scopes = New-Object Collections.Generic.List[string]
if ($All) {
$scopes = 'AllUsersAllHosts', 'AllUsersCurrentHost', 'CurrentUserAllHosts', 'CurrentUserCurrentHost'
}
}
process {
if (-not $All) { $scopes.Add($Scope) }
}
end {
$filePaths = foreach ($sc in $scopes) { $PROFILE.$sc }
$extantFilePaths = foreach ($filePath in $filePaths) {
if (-not (Test-Path -LiteralPath $filePath)) {
if ($Force) {
if ((New-Item -Force -Type Directory -Path (Split-Path -LiteralPath $filePath)) -and (New-Item -Force -Type File -Path $filePath)) {
$filePath
}
} else {
Write-Verbose "Skipping nonexistent profile: $filePath"
}
} else {
$filePath
}
}
if ($extantFilePaths.Count) {
Write-Verbose "Opening for editing: $extantFilePaths"
Invoke-Item -LiteralPath $extantFilePaths
} else {
Write-Warning "The implied or specified profile file(s) do not exist yet. To force their creation, pass -Force."
}
}
}
Run Code Online (Sandbox Code Playgroud)
[1] PowerShell 将当前用户、当前主机配置文件视为感兴趣的配置文件,这就是为什么$profile
的字符串值包含该值的原因。请注意,为了[string]
用音符属性装饰实例,Add-Member
仅靠是不够的;您必须使用以下习惯用法:$decoratedString = $string | Add-Member -PassThru propName propValue
- 请参阅Add-Member
帮助主题。
归档时间: |
|
查看次数: |
575 次 |
最近记录: |