【发布时间】: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