Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell在函数中传递不同的参数_Powershell - Fatal编程技术网

使用Powershell在函数中传递不同的参数

使用Powershell在函数中传递不同的参数,powershell,Powershell,我正在使用powershell编写一个GUI工具,它将从exchange服务器提取邮件跟踪信息。根据用户选择的内容确定搜索类型。如果在运行Get-MessageTrackingLog时没有传递所有参数,那么该命令会删除该参数还是传递空值? 示例:如果未向“-sender$sender”传递任何内容,那么该值将仅为“null”还是该参数也将被删除,如下所示 Get-MessageTrackingLog-server$hts-sender$sender#正在传递一个值 或 Get-MessageTr

我正在使用powershell编写一个GUI工具,它将从exchange服务器提取邮件跟踪信息。根据用户选择的内容确定搜索类型。如果在运行Get-MessageTrackingLog时没有传递所有参数,那么该命令会删除该参数还是传递空值? 示例:如果未向“-sender$sender”传递任何内容,那么该值将仅为“null”还是该参数也将被删除,如下所示

Get-MessageTrackingLog-server$hts-sender$sender#正在传递一个值 或 Get-MessageTrackingLog-服务器$hts#未传递发件人的值

换句话说,如果我不传递带有参数的arg,那么我不希望该参数包含在命令中

我试图避免根据用户的选择为每个不同的场景编写get-messagetrackinglog

希望这对每个人都有意义,谢谢

If ($sender -and $chk_Mailbox.checked -and $chk_End.checked -and $chk_start.checked){Msg -sender **$sender** -Start **$Start** -End **$End** -max_res_size **$max_res_size**}
If ($sender -and $chk_Mailbox.checked -and (!$chk_End.checked) -and (!$chk_start.checked)){Msg -sender $sender -max_res_size **$max_res_size**}

Function Msg{
[CmdletBinding()]
Param(
[Parameter(Position=0,Mandatory=$false)]
$Sender,
[Parameter(Position=1,Mandatory=$false)]
$Start,
[Parameter(Position=2,Mandatory=$false)]
$End,
[Parameter(Position=3,Mandatory=$true)]
$max_res_size,
[Parameter(Position=4,Mandatory=$false)]
$EventID,
[Parameter(Position=5,Mandatory=$false)]
$MsgID
)
    BEGIN
    {
        If ($max_res_size -match "unlimited"){$maxloop = 100000000} else {$maxloop = $max_res_size}

        $ht = Get-ExchangeServer | ?{$_.admindisplayversion -match '14.3' -and $_.ServerRole -match 'HubTransport'}  |% {$_.name} 
        $startstop = $true
    Foreach ($hts in $ht)
        {
            Get-MessageTrackingLog -server $hts -sender **$sender** -Start **$Start** -End **$End** -resultsize **$max_res_size** -EventID **$EventID** -MessageId **$MsgID** -warningaction 0 |
            %{
                if ($rescount -ge $maxloop){$startstop = $false; break}
                $dataGridView1.rows.add($_.TimeStamp,$_.Sender,[string]$_.Recipients,$_.RecipientCount,`
                $_.TotalBytes,$_.ReturnPath,$_.MessageLatency,$_.MessageLatencyType,$_.EventId,$_.Source,$_.ServerHostname,$_.ConnectorId,$_.MessageId)
                $rescount++
                $Res_Count.text = $rescount
            }
        }
    }
}

是的,这就是它的工作原理。未绑定到值的参数将具有该类型的默认值(或为该参数指定的默认值,如果存在)。对于没有特定类型的引用类型或参数,这是
$null

所以对于一个函数

function x($a, $b){}
x 1
您将使
$b
等于
$null
,但在以下情况下

function x($a, [int]$b){}
x 1
它应该是
0
。为此:

function x($a, $b=-1){}
x 1
您将得到
-1
,因为指定了一个特定的默认值。

您需要的。这是一种方便的方法,可以有条件地为cmdlet调用构建一组参数

只是给你举个例子

$parameters = @{
    Server = $hts
    Start = $Start
    End = $End
    ResultSize = $max_res_size
    EventID = $EventID
    MessageId = $MsgID
    WarningAction = 0
}

# If sender was specified then add it as a parameter
if($sender){$parameters.Sender = $sender}

# Call the cmdlets passing all the arguments we used. 
Get-MessageTrackingLog @parameters
我知道你会想让其他可选的。很容易复制我在这里向您展示的内容。基本上,在检查其他参数时,使用您定义和构建的哈希表。然后,您可以访问cmdlet
Get MessageTrackingLog

还请记住,您可以在
param
中对其中一些参数使用默认值,这样您就不必检查它们是否存在


当您有很多参数时,这也是一种保持行短的简便方法。使用splatting保存背景标记。

@Matt因此,它看起来是这样的:
Get MessageTrackingLog@MsgTrkParm$MsgTrkParm=@{'sender'=$sender;'server'=$HTS;'Start'=$Start;'end'=$end;'EventID'=$EventID}
请看我的答案,我现在在脑海里说了一句“Splat that”。