i have table contains information pointing files stored on file server. i'm trying write powershell script verify each record in table has physical file associated on server, if not write csv file. i'm not having luck wit csv portion , hoping help?
here have far (please let me know if there better ways this, im brand new powershell). want add records csv if test-path fails find file in question.
[reflection.assembly]::loadfile("c:\ora10g\odp.net\bin\2.x\oracle.dataaccess.dll") $connectionstring = "data source=xxxxxx;user id=xxxx;password=xxxxxxxx;" $connection = new-object oracle.dataaccess.client.oracleconnection($connectionstring) $connection.open() $querystring = "select key, sequence, type, file filetable type in (2, 5)" $command = new-object oracle.dataaccess.client.oraclecommand($querystring, $connection) $mediaobjrs = $command.executereader() # loop through recordset while ($mediaobjrs.read()) { # assign variables recordset. $objectkey = $mediaobjrs.getstring(0) $objectseq = $mediaobjrs.getdecimal(1) $objecttype = $mediaobjrs.getdecimal(2) $objectfilename = $mediaobjrs.getstring(3) # check if file exists based on filetype. if($objecttype -eq 2){ # type 2 = ole $filelocation = "\\fileserver\d$\files\" + $objectfilename }elseif($objecttype -eq 5){ # type 5 = file $filelocation = $objectfilename } $fileexists = test-path $filelocation if($fileexists -eq $false){ #write output file $objectkey | export-csv missingfiles.csv $objectseq | export-csv missingfiles.csv $objecttype | export-csv missingfiles.csv $objectfilename | export-csv missingfiles.csv } } $connection.close()
each time write missingfiles.cs clobber previous one. plus cmdlet oriented towards saving objects each property represents 1 of comma separated values.
the simplest way manually write (append) csv file:
if(!$fileexists) { #write output file "`"$objectkey`",`"$objectseq`",`"$objecttype`",`"$objectfilename`" >> foo.csv }
the "slick" powershell way create object each file , put in array , when you're done, export array of object csv file e.g.:
$files = @() # initialize array outside loop ... if (!$fileexists) { $obj = new-object -psobject -prop @{ key = $objectkey seq = $objectseq type = $objecttype filename = $objectfilename } $files += obj } ... # outside of loop, export array of objects csv $files | export-csv missingfiles.csv
Comments
Post a Comment