powershell $PROFILE是控制台和VS代码终端之间的不同类型

q1qsirdb  于 2022-12-04  发布在  Shell
关注(0)|答案(1)|浏览(138)

在PowerShell控制台中,$PROFILE变量是[string]。为什么在VS代码中不是[string]?

PS C:\> $PROFILE
C:\Users\pwatson2\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
PS C:\> $PROFILE.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

PS C:\> $PSVersionTable.PSVersion.ToString()
7.1.5

在VS代码终端中,它是一个[PSCustomObject]。

[DBG]: PS C:\> $PROFILE

AllUsersAllHosts                          AllUsersCurrentHost                                        CurrentUserAllHosts
----------------                          -------------------                                        -------------------
C:\Program Files\PowerShell\7\profile.ps1 C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1 C:\Users\pwatson2\Docu…
[DBG]: PS C:\> $PROFILE.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

[DBG]: PS C:\> $PSVersionTable.PSVersion.ToString()
7.1.5

更新日期:

<dropped for clarity>

更新2:

以下是VSCode PowerShell扩展启动时TERMINAL窗口中的输出。配置文件脚本如下所示。运行CurrentUserAllHosts脚本时,$PROFILE为NULL。这些脚本都不会设置$PROFILE的值。
有两个问题。
当调用其中一个非用户配置文件脚本(可能是AllUsersAllHosts)时,脚本的路径似乎没有使用正确放置的QUOTATION MARK字符。
当CurrentUserAllHosts脚本运行时,$PROFILE为NULL。

=====> PowerShell Preview Integrated Console v2021.10.3 <=====

C:\Program: The term 'C:\Program' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Running profile C:\Users\lit\Documents\PowerShell\profile.ps1
PROFILE is NULL
PS C:\Users\lit>

当TERMINAL命令提示符可用时,PROFILE变量现在包含以下内容。

PS C:\Users\lit> $PROFILE | Format-List -Property *

AllUsersAllHosts       : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1
CurrentUserAllHosts    : C:\Users\lit\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\lit\Documents\PowerShell\Microsoft.VSCode_profile.ps1

这些是概要文件脚本。请注意,这两个Microsoft.VSCode_profile.ps1文件都不存在。

PS C:\> Get-Content -Path 'C:\Program Files\PowerShell\7\profile.ps1'
$s = $($MyInvocation.MyCommand.Source)
Write-Host "Running profile $s"
if ($null -eq $PROFILE) {
    Write-Host "PROFILE is NULL"
} else {
    Write-Host "PROFILE is of type $($PROFILE.GetType())"

    $scope = $PROFILE |
        Get-Member -Type NoteProperty |
        Where-Object { $_.Definition.EndsWith($s) } |
        ForEach-Object { $_.Name }
    Write-Host "$($PSVersionTable.PSVersion.ToString()) $scope @ $($MyInvocation.MyCommand.Source)"
}
PS C:\> Get-Content -Path 'C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1'
Get-Content: Cannot find path 'C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1' because it does not exist.
PS C:\> Get-Content -Path 'C:\Users\lit\Documents\PowerShell\profile.ps1'
$s = $($MyInvocation.MyCommand.Source)
Write-Host "Running profile $s"
if ($null -eq $PROFILE) {
    Write-Host "PROFILE is NULL"
} else {
    Write-Host "PROFILE is of type $($PROFILE.GetType())"

    $scope = $PROFILE |
        Get-Member -Type NoteProperty |
        Where-Object { $_.Definition.EndsWith($s) } |
        ForEach-Object { $_.Name }
    Write-Host "$($PSVersionTable.PSVersion.ToString()) $scope @ $($MyInvocation.MyCommand.Source)"
}
PS C:\> Get-Content -Path 'C:\Users\lit\Documents\PowerShell\Microsoft.VSCode_profile.ps1'
Get-Content: Cannot find path 'C:\Users\lit\Documents\PowerShell\Microsoft.VSCode_profile.ps1' because it does not exist.
PS C:\>
8i9zcol2

8i9zcol21#

只是替换...
“$s = $($我的调用.我的命令.源代码)”
使用...“$s = $配置文件”

相关问题