powershelllync-2013skype-for-business

Passing Get-Credential through array to use with Invoke-Expression


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?


Solution

  • First, I think you'd be much better off defining your commands as [ScriptBlock]s rather than as [String]s:

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

    You can use the $Using scope modifier to embed current variable values in there, you don't have escape $ or anything else, you get the advantage of syntax highlighting and tab completion when constructing it; it's overall better.

    Using $Using will also correctly embed complex objects such as a [PSCredential].

    For the time being I'm just addressing your question directly, but I think arco444 might have a point about how you're going about this whole thing.