How to splat to a function in powershell?

284 Views Asked by At

I have a function (which started out as a ScriptBlock) and I'd like to be able to splat my input parameters to store configurations, but instead I'm passing each of them individually and this is bad, because I'd rather just pass the hashmap when I want to run the script block using Invoke-Command -Command ${function:sb} -ArgumentList $service_host_download_11_1_1

or

Invoke-Command -Command ${function:sb} -ArgumentList $service_host_11_1_1

But thus far I have been unable to achieve this; see the code below, is there a way to do this?

# I'd like to be able to splat these to my sb function
$service_host_download_11_1_1 = @{
    packageName = 'ServiceHostDownload'
    searchPattern = 'VERSION,'
    host_arr = $(Get-ADComputer -LDAPFilter '(&(name=MCDONALDS*)(!(name=MCDONALDSTAB*)))' -SearchBase "OU=Simphony,OU=MCDONALDS,OU=Florda,OU=Places,DC=mckeedees,DC=com" | Select-Object -ExpandProperty Name)
}

# And this also
$service_host_11_1_1 = @{
    packageName = 'ServiceHost'
    searchPattern = 'VERSION,|REBOOT'
    host_arr = $(Get-ADComputer -LDAPFilter '(&(name=MCDONALDS*)(!(name=MCDONALDSTAB*)))' -SearchBase "OU=Simphony,OU=MCDONALDS,OU=Florda,OU=Places,DC=mckeedees,DC=com" | Select-Object -ExpandProperty Name)
}


function sb($host_arr,
            $packageName,
            $searchPattern) {
    


    $host_arr | % {

         Invoke-Command -ComputerName $_ -AsJob -ArgumentList @($packageName, $searchPattern) -ScriptBlock {param($pn, $sp)  
            $result = gc -path "C:\Micros\Simphony\CALTemp\Packages\$($pn)\Setup_log.txt" | Select-String -Pattern $sp
            Write-Host "$($ENV:COMPUTERNAME),$($pn),$($result)"
        } 
    }
}

# I'd rather be splitting with Invoke-Command here.
sb $service_host_download_11_1_1['host_arr'] $service_host_download_11_1_1['packageName'] $service_host_download_11_1_1['searchPattern']

# Wait for completion
Start-Sleep -Seconds 5000

Get-Job | ? { $_.State -eq 'Completed'} | Receive-Job 

# Write out failed
#Get-Job | ? { $_.State -eq 'Failed'} | Receive-Job

# Set us up for next time...
Get-Job | Remove-Job
0

There are 0 best solutions below