0

我有一个函数(最初是一个 ScriptBlock),我希望能够将我的输入参数用于存储配置,但是我单独传递它们中的每一个,这很糟糕,因为我宁愿只是当我想使用运行脚本块时传递哈希图Invoke-Command -Command ${function:sb} -ArgumentList $service_host_download_11_1_1

或者

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

但到目前为止,我一直无法做到这一点;看下面的代码,有没有办法做到这一点?

# 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
4

0 回答 0