gpt4 book ai didi

powershell - 找不到已安装的脚本作为可执行文件

转载 作者:行者123 更新时间:2023-12-04 07:53:16 25 4
gpt4 key购买 nike

有一个名为 Get-Stuff.ps1 的脚本以 PSScriptInfo 为首堵塞。该脚本将发布到 NuGet 存储库。然后将其安装到预期位置 C:\Users\lit\Documents\WindowsPowerShell\Scripts\Get-Stuff.ps1 . Get-InstalledScript报告它已安装。但是,它不是一个可执行文件。 C:\Users\lit\Documents\WindowsPowerShell\Scripts\目录不在 $Env:PSModulePath 中.
安装后我错过了什么步骤才能使该命令可执行?

PS C:\> (Get-ChildItem -Recurse -File -Path 'C:\' -Filter 'Get-Stuff*' -ErrorAction SilentlyContinue).FullName
C:\src\Modules\scripts\Get-Stuff.ps1
PS C:\> Test-ScriptFileInfo -Path 'C:\src\Modules\scripts\Get-Stuff.ps1'

Version Name Author Description
------- ---- ------ -----------
1.0.0.1 Get-Stuff lit Get-Stuff produces .csv files of record counts from tables.


PS C:\> Publish-Script -Path 'C:\src\Modules\scripts\Get-Stuff.ps1' -NuGetApiKey 'yanon' -Repository 'yrepo'
PS C:\> Find-Script -Name Get-Stuff

Version Name Repository Description
------- ---- ---------- -----------
1.0.0.1 Get-Stuff yrepo Get-Stuff produces .csv files of record counts from tables.


PS C:\> Find-Script -Name Get-Stuff | Install-Script
PS C:\> (Get-ChildItem -Recurse -File -Path 'C:\' -Filter 'Get-Stuff*' -ErrorAction SilentlyContinue).FullName
C:\src\Modules\scripts\Get-Stuff.ps1
C:\Users\lit\Documents\WindowsPowerShell\Scripts\Get-Stuff.ps1
C:\Users\lit\Documents\WindowsPowerShell\Scripts\InstalledScriptInfos\Get-Stuff_InstalledScriptInfo.xml
PS C:\> Get-InstalledScript -Name Get-Stuff

Version Name Repository Description
------- ---- ---------- -----------
1.0.0.1 Get-Stuff yrepo Get-Stuff produces .csv files of record counts from tables.


PS C:\> Get-Command Get-Stuff
Get-Command : The term 'Get-Stuff' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-Command Get-Stuff
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Stuff:String) [Get-Command], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

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

最佳答案

$env:PATH这对于仅按文件名调用 *.ps1 很重要文件 (脚本文件),而不是 $env:PSModulePath ,仅适用于模块。
不幸的是, PowerShellGet module 的早期版本未提供添加 Install-Script 所针对的目录cmdlet 到 $env:PATH坚持,但至少从 v2.2.4 开始,他们会这样做(出现提示),下次您调用 Install-Script执行实际安装。
尝试更新您的 PowerShellGet带有 Update-Module PowerShellGet 的版本或者,如果失败,Install-Module -Force PowerShellGet (在 Windows PowerShell 中需要提升,除非您指定 -Scope CurrentUser )。

如果你想做这些 $env:PATH添加自己,运行以下代码:
笔记:

  • 修改$env:PATH通过注册表持久化,仅在 Windows 上受支持(在 Unix 上,定义持久性环境变量的机制因平台而异)。
  • 然而,在 Unix 上, session 中的 $env:PATH 也是如此。变量被更新。
  • & {

    Write-Verbose -vb "Adding Install-Script install directories to `$env:PATH..."

    $isWin = $env:OS -eq 'Windows_NT'
    $isAdmin = if ($isWin) { [bool] (net session 2>$null) } else { 0 -eq (id -u) }

    $ErrorActionPreference = 'Stop'

    # Determine the locations: current-user, all-user.
    $scriptDirs = (Join-Path (Split-Path ($PROFILE, "$HOME/.local/share/powershell/Modules")[$env:OS -ne 'Windows_NT']) Scripts),
    (Join-Path (Split-Path ("$env:ProgramFiles\$(if ($PSVersionTable.PSEdition -ne 'Core') { 'Windows' })PowerShell\Modules", '/usr/local/share/powershell/Modules')[$env:OS -ne 'Windows_NT']) Scripts)

    if (-not $isWin) {
    # Note: There's no unified mechanism across macOS and Linux.
    Write-Warning "On Unix, this script only supports modifying the *current session*'s `$env:PATH variable."
    } elseif (-not $isAdmin) {
    Write-Warning "Since this session isn't elevated, only the *current-user* location will be added *persistently*."
    }

    $pathVarSep = [IO.Path]::PathSeparator

    $i = 0
    foreach ($dir in $scriptDirs) {
    # Always update the in-session variable.
    Write-Verbose -vb "-- Adding $dir..."
    if ($env:PATH -split $pathVarSep -notcontains $dir) {
    $env:PATH = ($env:PATH -replace "$pathVarSep`$") + $pathVarSep + $dir
    }
    else {
    Write-Verbose -vb "Already present in-session: $dir"
    }
    # On Windows, also try to update the *persistent* definitions
    if ($isWin) {
    $scope = ('User', 'Machine')[$i++ -eq 1]
    if ($scope -eq 'Machine' -and -not $isAdmin) { break } # skip due to lack of permissions
    # Note: We query the registry directly, so as to preserve unexpanded REG_EXPAND_SZ values.
    $currVal = Get-ItemPropertyValue ('registry::HKEY_CURRENT_USER\Environment', 'registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment')[$scope -eq 'Machine'] Path
    if ($currVal -split $pathVarSep -notcontains $dir) {
    [Environment]::SetEnvironmentVariable('Path', (($currVal -replace "$pathVarSep`$") + $pathVarSep + $dir), $scope)
    } else {
    Write-Verbose -vb "Already present persistently in the $scope scope: $dir"
    }
    }
    }

    Write-Verbose -vb 'Done.'

    }

    关于powershell - 找不到已安装的脚本作为可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66836654/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com