我有一个接受管道参数的脚本。我不能确定用户在管道传输到我的脚本之前是否已经通过 Sort-Object 管道输入,所以我想在开始在我的脚本中对其进行操作之前将管道输入发送出去进行排序(和其他处理)。
该脚本具有以下参数:
$script:creds
$script:confObj
function Get-ConfigureServerInput
{
<#
#>
[CmdletBinding(DefaultParameterSetName = "FromPipe")]
Param(
[Parameter(ParameterSetName="FromUser", Mandatory=$true, Position=0)] [string[]]$ComputerName,
[Parameter(ParameterSetName="FromUser", Mandatory=$true, Position=1)] [string[]]$AppName,
[Parameter(ParameterSetName="FromUser", Mandatory=$true, Position=2)] [string[]]$key,
[Parameter(ParameterSetName="FromUser", Mandatory=$true, Position=3)] [string[]]$setting,
[Parameter(ParameterSetName="FromPipe", Mandatory=$true, ValueFromPipeline=$true)] [PSObject]$pobj,
[Parameter(ParameterSetName="FromFile", Mandatory=$true)] [string]$File,
[Parameter()] [switch]$force,
[Parameter()] [switch]$mt,
[Parameter()] [int]$MaxThreads = 0,
[Parameter()] [System.Management.Automation.PSCredential]$Credentials
)
BEGIN
{
#Runs once regardless of how many items are in the pipe
If ($Credentials -ne $null) {$script:creds = $Credentials}
Switch ($PSCmdlet.ParameterSetName)
{
"FromPipe"
{
Write-Verbose "Input is from pipe"
$script:confObj = $($pobj)
break
}
"FromUser"
{
Write-Verbose "Input is from user arguments"
$uObj = New-Object PSObject
$uObj | Add-Member -type NoteProperty -name ComputerName -value $ComputerName
$uObj | Add-Member -type NoteProperty -name AppName -value $AppName
$uObj | Add-Member -type NoteProperty -name key -value $key
$uObj | Add-Member -type NoteProperty -name setting -value $setting
$script:confObj = $uObj
break
}
"FromFile"
{
Write-Verbose "Input is from file"
Write-Verbose "Import-CSV -Header 'ComputerName','AppName','key','setting' -path $File "
$script:confObj = Import-CSV -Header "ComputerName","AppName","key","setting" -path $File
break
}
}
$script:confObj
}
我认为我需要的是 BEGIN 块中的内容,例如:
$input = $input | Sort-Object ComputerName,AppName,key,setting -Unique
但是 BEGIN 块中似乎没有 $input 变量。如果我把 $input | 在 BEGIN 块中获取成员我收到错误:
Get-Member : No object has been specified to the get-member cmdlet.
At E:\Powershell\ConfigureServer\ConfigureServer.psm1:53 char:22
+ $input | Get-Member <<<<
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
+ FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand
我可以将 Sort-Object 行放在 PROCESS 块中,但正如预期的那样,我一次得到一个对象,每个对象都自行排序,导致数据未排序。
关于在我的脚本中处理它之前如何通过一些过滤器(例如:Sort-Object)运行我的传入管道的任何建议(除了依靠用户将Sort-Object的输出通过管道传输到我的脚本)?
这就是我向脚本发送对象的方式:
Import-CSV -header "ComputerName","AppName","key","setting" duplicates.dat | Get-Member
此处显示将转到 Get-Member 以查看我正在获取一个对象。
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AppName NoteProperty System.String AppName=garbage
ComputerName NoteProperty System.String ComputerName=google.com
key NoteProperty System.String key=trash
setting NoteProperty System.String setting=null
使用 -File 或 -ComputerName (以及其他强制性参数)调用它会产生我期望的对象。例如:
PS E:\Powershell\ConfigureServer> Get-ConfigureServerInput -Verbose -File duplicates.dat
VERBOSE: Input is from file
VERBOSE: Import-CSV -Header 'ComputerName','AppName','key','setting' -path duplicates.dat
ComputerName AppName key setting
------------ ------- --- -------
google.com garbage trash null
alpha nonsense nilhilist nil
yahoo.com aname akey avalue
alpha nonsense nilhilist nil
yahoo.com aname akey avalue
使用管道输入调用不返回输出。