I have a PowerShell function like this (I have simplified it here to make it more understandable):
Function QueryList
{
param(
[Parameter(Mandatory=$true,Position=1)]
[Microsoft.SharePoint.SPList] $list
[Parameter(Mandatory=$true,Position=2)]
[string] $camlQuery
)
$itemsQry = New-Object Microsoft.SharePoint.SPQuery
$itemsQry.Query = $camlQuery
$itemsQry.ViewFieldsOnly = $false
$itemsQry.RowLimit = 0
$itemsQry.ViewAttributes = "Scope='Recursive'"
return $list.GetItems($itemsQry)
}
Function MigrateList
{
param(
[Parameter(Mandatory=$true,Position=1)]
[Microsoft.SharePoint.SPList] $list,
[Parameter(Mandatory=$true,Position=2)]
[string] $matchingItemsQuery
)
foreach ($listItem in $list.Items)
{
# get items using the query (how to inject '$listItem' into query string?)
$targetItem = QueryList -list $list -camlQuery $matchingItemsQuery
# do something with matching items
...
}
}
# main script
$matchingItemsQuery = "<Where><Eq><FieldRef Name='Title' /><Value Type='TEXT'>`$(`$listItem[`"Title`"])</Value></Eq></Where>"
$targetItems = MigrateList -list $listXy -matchingItemsQuery $matchingItemsQuery
As you can see, I want to query some items from a list, matching a given criteria. As the criteria changes from list to list I want to be able to pass the query to the function, inside the query there is a reference to the variable that will only exist in the 'MigrateList' function (here: $listItem
).
As it is now, the variable will of course not be evaluated to the objects value ("Title" column value of $listItem) as it is passed as a string, as the '$' are escaped (which is needed to pass the query to the function).
I know it is maybe not the nicest construct but it would get the job done. So how could I change the script that the passed query string will be injected with the $listItem object (in this case, column value)?