2

I am trying to run Lync / S4B test commands through an automated script, currently I set up a command array like this:

$HealthCheckCoreCmdlets = @{
    "AddressBookService" = "Test-CsAVConference -TargetFQDN $($fqdn)"
    "AddressBookWebQuery" = "Test-CSAddressBookWebQuery -TargetFQDN $($fqdn)"
    "ASConference" = "Test-CsASConference -TargetFQDN $($fqdn) -SenderSipAddress $($CTestUser1) -SenderCredential $($Cpass1) -ReceiverSipAddress $($CTestUser2) -ReceiverCredential $($Cpass2)"
    "AVConference" = "Test-CsAVConference -TargetFQDN $($fqdn) "
    "ClientAuthentication" = "Test-CsClientAuthentication -TargetFQDN $($fqdn) -UserSipAddress $($CTestUser1) -UserCredential $($Cpass1)"
    "DataConference" = "Test-CsDataConference -TargetFQDN $($fqdn)"
    "GroupExpansion" = "Test-CsGroupExpansion -TargetFQDN $($fqdn) -GroupEmailAddress $($CGroupEmail)"
    "GroupIm" = "Test-CsGroupIm -TargetFQDN $($fqdn)"
    "Im" = "Test-CsIm -TargetFQDN $($fqdn)"
    "LisConfiguration" = "Test-CsLisConfiguration -TargetFQDN $($fqdn) -Subnet $($CSubnet) -UserSipAddress $($CTestUser1) -UserCredential $($Cpass1)"
    "LocationPolicy" = "Test-CsLocationPolicy -TargetFQDN $($fqdn)"
    "P2PAV" = "Test-CsP2PAV -TargetFQDN $($fqdn)"
    "Presence" = "Test-CsPresence -TargetFQDN $($fqdn)"
    "Registration" = "Test-CsRegistration -TargetFQDN $($fqdn)"
    "Replica" = "`$testReplica = Test-CsReplica; if(`$testReplica -eq `$null){return 'Success'}else{return 'Failure'}"
    "Topology" = "`$testtopology = Test-CsTopology; if(`$testtopology -eq `$null){return 'Success'}else{return 'Failure'}"
    "UcwaConference" = "Test-CsUcwaConference -TargetFQDN $($fqdn)"
    "WebApp" = "Test-CsWebApp -TargetFQDN $($fqdn)"
}

And I use Invoke-Expression to run the command:

foreach ($PSHCmdlet in $HealthCheckCoreCmdlets.GetEnumerator() | Sort-Object Key) 
{ 
    Update-Status $PSHCmdlet.Key
    $Corearray."$($PSHCmdlet.Key)" += (Get-CMDLetResult $PSHCmdlet.Value) 
}

Get-CMDLetResult:

function Get-CMDLetResult ($Value) {
$CMDResult = (Invoke-Expression ("$($Value)"))

Return $CMDResult 
}

And most commands work other than the ones that require a Get-Credential passed to them (I'm storing them in the above commands as $Cpass1 etc.) - I've tried passing the variable as: `$Cpass1 / $($Cpass1) / and just plain $Cpass1.

Can anyone point me on how I can pass this object through with the command to be invoked?

4

1 回答 1

0

[ScriptBlock]首先,我认为将命令定义为s 而不是s会更好[String]

$HealthCheckCoreCmdlets = @{
    "AddressBookService" = { Test-CsAVConference -TargetFQDN $Using:fqdn }
    # ...
    "Replica" = {
        $testReplica = Test-CsReplica
        if ($testReplica -eq $null) {
            return 'Success'
        } else { 
            return 'Failure'
        }
    }
}

您可以使用$Using范围修饰符将当前变量值嵌入其中,您没有转义$或其他任何东西,您可以在构造它时获得语法高亮和制表符完成的优势;它总体上更好。

Using$Using还将正确嵌入复杂的对象,例如[PSCredential].

暂时我只是直接解决你的问题,但我认为 arco444 可能对你如何处理这件事有所了解。

于 2015-11-10T16:39:22.723 回答