【问题标题】:Select-Object CmdLet fails when used inside a FunctionDefinition of New-PSSessionConfigurationFile在 New-PSSessionConfigurationFile 的 FunctionDefinition 中使用 Select-Object CmdLet 失败
【发布时间】:2015-04-16 18:21:44
【问题描述】:

考虑下面的这个例子,我在其中创建了一个受约束的管理端点。我的目标是在 Get-EventLog CmdLet 上创建一个“代理”函数。此示例按预期工作,直到我添加 | Select-Object -First 5。当我这样做时,我收到以下错误消息:“找不到与参数名称'First'匹配的参数”。为什么?

$getAppEventLog = {
    #this throws an error, see below
    get-eventlog -log application | Select-Object -First 5 

    #this works
    #get-eventlog -log application    
}

New-PSSessionConfigurationFile -Path c:\PSScripts\panos.pssc `
                               -Description 'Delegation EndPoint Repro' `
                               -ExecutionPolicy Restricted `
                               -SessionType RestrictedRemoteServer `
                               -LanguageMode FullLanguage `
                               -FunctionDefinitions @{Name="Get-AppEventLog";ScriptBlock=$getAppEventLog; Options="AllScope"}

Unregister-pssessionconfiguration -name EventLogManagement -force
Test-PSSessionConfigurationFile -Path c:\PSScripts\panos.pssc
Register-PSSessionConfiguration -Path 'c:\PSScripts\panos.pssc' `
                                -Name EventLogManagement `
                                -ShowSecurityDescriptorUI `
                                -AccessMode Remote `
                                -Force

Enter-PSSession -ComputerName localhost -ConfigurationName EventLogManagement
Get-AppEventLog
Select-Object : A parameter cannot be found that matches parameter name 'First'.
At line:2 char:51
+     get-eventlog -log application | Select-Object -First 5
+                                                   ~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Select-Object

PowerShell 信息

PS C:\Windows\system32> $PSVersionTable

Name                           Value                                                                                                                                                                                        
----                           -----                                                                                                                                                                                        
PSVersion                      4.0                                                                                                                                                                                          
WSManStackVersion              3.0                                                                                                                                                                                          
SerializationVersion           1.1.0.1                                                                                                                                                                                      
CLRVersion                     4.0.30319.34209                                                                                                                                                                              
BuildVersion                   6.3.9600.17400                                                                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}                                                                                                                                                                         
PSRemotingProtocolVersion      2.2                                                                                                                                                                                          

在考虑了这一点之后,我意识到可以以更简单的方式重现该问题 - 无需函数定义。 当 SessionType 等于 RestrictedServer 时,以下两个 Cmdlet 可用:Get-Command 和 Select-Object。因此,我可以通过简单地执行以下操作来重现我的问题:

[localhost]: PS> Get-Command | Select-Object -first 1
A parameter cannot be found that matches parameter name 'first'.
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Select-Object

鉴于此,我可以创建一个重现问题的会话,如下所示:

New-PSSessionConfigurationFile -Path c:\PSScripts\panos.pssc `
                               -Description 'Delegation EndPoint Repro' `
                               -ExecutionPolicy Restricted `
                               -SessionType RestrictedRemoteServer `
                               -LanguageMode FullLanguage


Unregister-pssessionconfiguration -name EventLogManagement -force
Test-PSSessionConfigurationFile -Path c:\PSScripts\panos.pssc
Register-PSSessionConfiguration -Path 'c:\PSScripts\panos.pssc' `
                                -Name EventLogManagement `
                                -ShowSecurityDescriptorUI `
                                -AccessMode Remote `
                                -Force

因此,我可以将我原来的问题改写为:鉴于上述注册参数,为什么 Select-Object Cmdlet 在受限会话中不起作用。

【问题讨论】:

  • 这对我有用。您使用的是哪个 powershell 版本?
  • 通过提供 $PSVersionTable 输出来编辑我的问题

标签: powershell


【解决方案1】:

我遇到了同样的问题,创建了一个 PS 端点作为 PSPKI 模块的包装器。 PSPKI 模块也使用了 Select-Object,出现了同样的错误。

对我来说,解决方案是更新我的端点 .psrc 文件以包含 Select-Object 的 AliasDefinition 以指向 FQDN Select-Object。

AliasDefinitions = @{ Name = 'Select-Object' ;值 = 'Microsoft.PowerShell.Utility\Select-Object' }

另请参阅:https://jamesone111.wordpress.com/2016/07/01/just-enough-admin-and-constrained-endpoints-part-2-startup-scripts/

Grtx, BvZanten

【讨论】:

    【解决方案2】:

    您可以尝试使用:

    Get-EventLog -log application -newest 5 
    

    【讨论】:

    • MysticHeroes,感谢您的建议,但我确实需要让 Select-Object 工作。实际上,我的真实场景要复杂得多,并且使用了不同的 CmdLets 集。我使用 Get-EventLog 示例创建了我能想到的最简单的 re-pro。
    【解决方案3】:

    使用 SessionType RestrictedRemoteServer 函数 Select-Object 与 Default-SessionType 中的不同:

    RestrictedRemoteServer。仅包括以下代理函数:Exit-PSSession、Get-Command、Get-FormatData、Get-Help、Measure-Object、Out-Default 和 Select-Object。使用此 cmdlet 的参数将模块、函数、脚本和其他功能添加到会话中。

    此处描述了该问题并提供了一些解决方法:Constrained PowerShell endpoints – Visible cmdlets, session types, and language modes:

    代理函数是原始 cmdlet 的包装函数,它修改 cmdlet 的功能,例如,通过添加或删除参数

    【讨论】:

      猜你喜欢
      • 2014-06-21
      • 2014-08-25
      • 1970-01-01
      • 2018-11-23
      • 2013-12-30
      • 1970-01-01
      • 2017-07-11
      • 2023-03-11
      • 2020-07-02
      相关资源
      最近更新 更多