Help with removing items.


hello, i'm trying create script takes 3 *. xlsx files looks "id" matches , updates or adds items needed.

i'm hung process have in place remove items have been updated second part of script doesn't add duplicate values or overwrites anything. i've tried few variations of results have been inconsistent. bulk of script works fine removing items in $remove equal items in $updatelist seems problematic. 

so isnt whole thing basic load 3 .xlsx documents loop through values in $updatelist -eq in $master , update fields. save updated id's in $remove array , loop through "trying" remove equal items in update list. here failure if remove , updatelist same error generated think needs break if updatelist = 0 somewhere. in 2nd half removes items shouldnt ne.. not entirely sure how happens does. i've confirmed in error log bogus data in updatefile array.

maybe there better , cleaner way accomplish dont know, appreciated.

$remove = @() foreach ($u in $updatelist) { foreach ($r in $($master | ? { $_.templatekey -eq ($u.key) })) { $t = $r.template $t2 = $u.'t#' $c = $r.comments $r.template = ($u.'t#') $r.templatename = ($u.resolution) $r.comments = ("$c`n updated $t $t2 via -script $date") $item = new-object psobject $item | add-member -type noteproperty -name 'key' -value ($u.key) $remove += $item } } ###remove items updated updatelist foreach ($i in $remove) { if ($i.key -eq $updatelist.key) { $rm = $updatelist.key.indexof($i.key) } $updatelist.removeat($rm) } $remove = @() foreach ($u in $updatelist) { foreach ($e in $($ear | ? { $_.tempkey -eq ($u.key) } | sort-object -property tempkey -unique)) { ##do $item = new-object psobject $item | add-member -type noteproperty -name 'processlevel' -value ($e.company) $item | add-member -type noteproperty -name 'companydescription' -value ($e.companydescr) $item | add-member -type noteproperty -name 'deptid' -value ($e.deptid) $item | add-member -type noteproperty -name 'deptname' -value ($e.deptname) $item | add-member -type noteproperty -name 'jobcode' -value ($e.jobcode) $item | add-member -type noteproperty -name 'jobtitle' -value ($e.jobtitle) $item | add-member -type noteproperty -name 'template' -value ($u.'t#') $item | add-member -type noteproperty -name 'templatename' -value ($u.resolution) $item | add-member -type noteproperty -name 'templatekey' -value ($e.tempkey) $item | add-member -type noteproperty -name 'comments' -value "added per template validations $date -script" $master += $item $remove += $item.templatekey } }

###remove items updated updatelist foreach ($i in $remove) { if ($i.key -eq $updatelist.key) { $rm = $updatelist.key.indexof($i.key) } $updatelist.removeat($rm) } ###anything left not found or updated , error if ($updatelist.count -gt 0){$updatelist | export-csv -path $pathupdate\errorlog.csv -notypeinformation; write-host "contains errors see errorlog in $pathupdate\errorlog.csv"} ###saves new master document $master | export-excel -path $pathmaster\"master."$date.xlsx


looks problem in comparison operator

if ($i.key -eq $updatelist.key) {

in case $i.key getting property of single object , seeing if equal $updatelist.key which is single property on collection of objects.  if key 3 digit number and there 3 object in $updatelist, comparison like:

ps env:\> 123 -eq 123, 234, 345 false

which 123 not equal multiple values of collection.  should use -in operator see if single value found in multiple values

ps env:\> 123 -in 123, 234, 345 true

or if using powershell 2.0 or older have use -contains operator since -in did not exist

ps env:\> 123, 234, 345 -ccontains 123 true




Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client