Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

problems with variables in foreach loop

$
0
0

Please excuse my lack of Powershell knowledge.

I am trying to write a script to convert a bunch of xml files into csv.  All of my source xml files are in a working directory. When I execute I am getting these errors. (see red text)

 

Any idea where the @ symbol in the filename  is coming from??? 

 

 

Exception calling "Open" with "1" argument(s): "'@{BaseName=FOUO_AIX_5.3_V1R7_Manual_IAVM-xccdf}.xml' could not be foun

d. Check the spelling of the file name, and verify that the file location is correct.

If you are trying to open the file from your list of most recently used files, make sure that the file has not been ren

amed, moved, or deleted."

At line:7 char:27

+     $wb=$xl.workbooks.open <<<< ($xls)

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ComMethodTargetInvocation

 

You cannot call a method on a null-valued expression.

At line:8 char:15

+     $wb.SaveAs <<<< ($csv,$xlCSV)

    + CategoryInfo          : InvalidOperation: (SaveAs:String) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

 

 

 

Here is my code

 

****************************************************************

$list = get-childitem *.xml | select basename

 

foreach ($item in $list)

{

                $xlCSV=6

                $xls="$item.xml"

                $csv="$item.xml"

                $xl=New-Object -com "Excel.Application"

                $wb=$xl.workbooks.open($xls)

                $wb.SaveAs($csv,$xlCSV)

                $xl.displayalerts=$False

                $xl.quit()

}

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles