I'm trying to put together a script that should iterate through all webs in a site collection, then iterate through all lists in those webs, then add a column where a list has a certain name.
This is what I have so far:
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True,Position=1)]
[string]$sc
)
$spsite = Get-SPsite $sc
foreach ($web in $spsite.AllWebs)
{
foreach ($list in $spsite.AllWebs)
{
$splist = $list.Lists["PSList"]
$splist.Fields.Add("Calc", "Calculated", 0)
$spfield = $splist.Fields.Getfield("Calc")
$spfield.formula = "=TEXT([Start Date],`"mm/dd/yyyy`")`&`" - `"`&TEXT([End Date],`"mm/dd/yyyy`")"
$spfield.outputtype = "Text"
$spfield.update()
}
}
$spsite.dispose()
This is the error I get:
You cannot call a method on a null-valued expression. At C:\scripts\addcalc.ps1:24 char:22 + $splist.Fields.Add <<<< ("Calc", "Calculated", 0) + CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
I'm sure it's because I'm not iterating through the webs and then through the lists property. What's the correct way to achieve what I'm trying to do?