Linux上的PowerShell配置文件位置是什么?

时间:2019-04-07 15:05:29

标签: 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

在装有pwsh 6.2.0的Linux上,我只能找到两个位置。

CurrentUserAllHosts @ ~/.config/powershell/Microsoft.PowerShell_profile.ps1
CurrentUserCurrentHost @ ~/.config/powershell/profile.ps1

Linux上是否有“ AllUsers”配置文件脚本?如果是这样,他们在哪里?

1 个答案:

答案 0 :(得分:1)

Olaf在评论中提供了关键指针:

$profile | select *  # short for: $profile | Select-Object -Property *

显示所有配置文件的位置,单个配置文件是否存在

例如,在安装了/home/jdoe/.powershell的PowerShell的Ubuntu计算机上,我得到:

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

请注意[string]类型的标准Length属性的存在,如果改用
$profile | select *host*,则可以忽略。

鉴于$profile是一个 string 变量(类型[string]),因此以这种方式获取配置文件位置并不明显。
PowerShell用反映所有配置文件位置的[string]成员装饰NoteProperty实例,这就是selectSelect-Object)能够提取它们的原因。

仅输出$profile(即 string值)会产生/home/jdoe/.config/powershell/Microsoft.PowerShell_profile.ps1,即与其CurrentUserCurrentHost属性相同的路径。 [1]

您可以通过反射来验证这些属性的存在,如下所示(这也揭示了它们的值):

$profile | Get-Member -Type NoteProperty

这意味着您还可以使用常规属性访问标签补全来检索个人资料位置;例如:

# 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

便捷功能,用于获取配置文件位置并打开配置文件以进行编辑:

下面的代码定义:

  • Get-Profile枚举配置文件,显示配置文件的位置以及它们是否存在于给定的计算机上。

  • Edit-Profile打开配置文件进行编辑(使用-Force按需创建它们);请注意,修改所有用户的配置文件通常需要以admin身份运行。

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

}

[1] PowerShell考虑所关注的当前用户,当前主机配置文件 配置文件,这就是$profile字符串值包含该值。注意,为了用音符属性装饰[string]实例,仅靠Add-Member是不够的;您必须使用以下成语:$decoratedString = $string | Add-Member -PassThru propName propValue-参见Add-Member help topic