如果提供值,则仅传递参数 [英] Only pass parameter if value supplied

查看:87
本文介绍了如果提供值,则仅传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我已经在Write-EventLog周围创建了包装函数,这样我就可以轻松调用它,而不必担心检查&每次创建一个事件日志/源.通过简单地添加一个-Force参数,我希望它创建日志(如果它不存在的话).如果我不添加该参数,则该参数应正常运行(如果从我知道该日志将存在的代码中调用,则可以减少多次检查的开销).

I've created a wrapper function around Write-EventLog so that I can easily call it without having to concern myself with checking for & creating an event log/source each time. By simply adding a -Force parameter, I want it to create the log if it doesn't exist; if I don't add that parameter it should behave as normal (which reduces overhead of multiple checks if called from code where I know that log will exist).

我已经复制了write-eventlog可用的参数列表,以使我具有包装器可用的全部功能.然而;如果我不为其中一些参数提供值(例如RawData),则默认为null;然后我最终尝试为此参数传递null;这与不提供它是不同的.我不想列出所有可能的参数组合迭代,而在调用适当的方法签名之前检查是否提供了这些值.

I've copied the list of parameters available to write-eventlog such that I have the full functionality available to my wrapper. However; if I don't supply values to some of these parameters (e.g. RawData), they default to null; then I end up trying to pass null for this parameter; which is different to not supplying it. I don't want to have to list every possible iteration of parameter combinations with checks for whether those values were supplied before calling the appropriate method signature.

问题

是否有一种方法只能将值传递到已经传递给write-eventlog2的参数write-eventlog?

Is there a way to only pass values to parameters of write-eventlog where those parameters had been passed to write-eventlog2?

代码

cls
$myLog = 'Application'
$mySource = 'My PS Script'
$myEventId = 1 
[System.Diagnostics.EventLogEntryType]$myEntryType = [System.Diagnostics.EventLogEntryType]::Error 
$myMessage = 'This is a test message'

function Write-EventLog2
{
    [cmdletbinding()]
    param(  
        [Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String]$LogName
        ,
        [Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String]$Source
        ,
        [Parameter(Position=3,Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [Int32]$EventId
        ,
        [Parameter(Position=4,Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [System.Diagnostics.EventLogEntryType]$EntryType = [System.Diagnostics.EventLogEntryType]::Information
        ,
        [Parameter(Position=5,Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String]$Message
        ,
        [Parameter(Position=6,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
        [Int16]$Category = 1
        ,
        [Parameter(Position=7,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
        [String]$ComputerName = $env:COMPUTERNAME
        ,
        [Parameter(Position=8,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
        [Byte[]]$RawData
        ,
        [Parameter(Position=9,Mandatory=$false,ValueFromPipeline=$false,ValueFromPipelineByPropertyName=$true)]
        [switch]$Force 
    )

    begin
    {
        if($Force.IsPresent)
        {
            if (! ([System.Diagnostics.EventLog]::Exists($LogName) -and [System.Diagnostics.EventLog]::SourceExists($Source) )) 
            {
                New-EventLog -LogName $LogName -Source $Source 
            }
        }
    }
    process {
        Write-EventLog -LogName $LogName -Source $Source -EventId $EventId -EntryType $EntryType -Message $Message -Category $Category -ComputerName $ComputerName -RawData $RawData
    }
    #end{} #no ending actions required
}

Write-EventLog2 $myLog $mySource $myEventId $myEntryType $myMessage -Force

错误

Write-EventLog : Cannot validate argument on parameter 'RawData'. 
The argument is null or empty. Provide an argument that is not 
null or empty, and then try the command again.
At line:51 char:167

推荐答案

我会考虑 splatting 进行类似的操作,因为您可以在不更改太多代码的情况下说明null/empty.

I would consider splatting for something like this as you could account for null / empty without changing too much of the code.

begin
    {
        if($Force.IsPresent)
        {
            if (! ([System.Diagnostics.EventLog]::Exists($LogName) -and [System.Diagnostics.EventLog]::SourceExists($Source) )) 
            {
                New-EventLog -LogName $LogName -Source $Source 
            }
        }

        if($rawdata){
            $rawdataparameter = @{
                RawData = $rawdata 
            }
        }

    }
    process {
        Write-EventLog -LogName $LogName -Source $Source -EventId $EventId -EntryType $EntryType -Message $Message -Category $Category -ComputerName $ComputerName @RawDataParameter
    }

有条件地构建一个小的哈希表,该哈希表包含按名称和值表示的参数.使用at符号varialbe表示法来表示Write-Log命令.实际上,对于Get-ChildItem,如果表不包含任何对或为null,则将其忽略.

Conditionally build a small hashtable that contains the parameter by name and the value. The use the at sign varialbe notation to splat the Write-Log command. In practice, with Get-ChildItem, if the table contains no pairs or is null it is ignored.

这篇关于如果提供值,则仅传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆