I have a PowerShell script that is supposed to read a list of computers from a text file, and update SharePoint on whether that computer has a specific file on the C drive (and handle any items already in the list). This does work, but it also doubles each subsequent item read from the text file in the SharePoint list.
The first line in text file is listed once in SharePoint.
The second line in text file is listed twice in SharePoint.
The third line in text file is listed four times in SharePoint.
The fourth line in text file is listed eight times in SharePoint... And on.
I cannot figure out why the items are doubling. Please help.
# Add SharePoint PowerShell SnapinAdd-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
# Create Variables
$webURL = "http://sharepoint/site"
$listName = "List"
# Get the SPWeb object and save it to a variable
$web = Get-SPWeb $webURL
# Get the SPList object to retrieve the list
$list = $web.Lists[$listName]
# Get all items in this list and save them to a variable
# $items = $list.items
# Import Computer List
$computerlist = Get-Content ‘\\servername\c$\scripts\online_computers.txt’
foreach ($computer in $computerlist)
{
IF ( (Test-Path "\\$computer\c$\success.flag") -eq "True")
{
$items = $list.items
# Go through all items
foreach($item in $items)
{
# If the "Computer" column value equals the computername, update the status
if($item["Computer"] -eq "$computer")
{
# Update the "Status" column
$item["Status"] = "This computer has been migrated to Windows 7"
$item.Update()
}
else
{
# Computer not listed, create it
$newItem = $list.Items.Add()
# Add properties to the list item
$newItem["Computer"] = "$computer"
$newItem["Status"] = "This computer has been migrated to Windows 7"
# Update the object so it gets saved to the list
$newItem.Update()
}
}
}
ELSE
{
$items = $list.items
# Go through all items
foreach($item in $items)
{
# If the "Computer" column value equals the computername, update the status
if(($item["Computer"] -eq "$computer") -and ($item["Status"] -eq "This computer has not been migrated to Windows 7"))
{
# Update the "Status" column
$item["Status"] = "This computer has not been migrated to Windows 7"
$item.Update()
}
else
{
# Computer not listed, create it
$newItem = $list.Items.Add()
# Add properties to the list item
$newItem["Computer"] = "$computer"
$newItem["Status"] = "This computer has not been migrated to Windows 7"
# Update the object so it gets saved to the list
$newItem.Update()
}
}
}
}