帮助将数据库查询结果插入到 CSV 文件中
我有一个表,其中包含指向文件服务器上存储的文件的信息。我正在尝试编写一个 powershell 脚本来验证表中的每条记录在服务器上是否都有与其关联的物理文件(如果它不写入 CSV 文件)。我对 csv 部分没有太多运气,希望你能提供帮助?
这是我到目前为止所拥有的(请让我知道是否有更好的方法来做到这一点,我是 powershell 的新手)。如果测试路径无法找到有问题的文件,我只想将记录添加到 csv。
[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 FROM FileTable WHERE Type IN (2, 5)"
$command = new-Object Oracle.DataAccess.Client.OracleCommand($queryString, $connection)
$mediaObjRS = $command.ExecuteReader()
# Loop through recordset
while ($mediaObjRS.read()) {
# Assign variables from 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 to output file
$objectKey | Export-Csv missingfiles.csv
$objectSeq | Export-Csv missingfiles.csv
$objectType | Export-Csv missingfiles.csv
$objectFileName | Export-Csv missingfiles.csv
}
}
$connection.Close()
I have a table that contains information pointing to files stored on a file server. I'm trying to write a powershell script to verify that each record in the table has a physical file associated with it on the server, if it does not write to the CSV file. I'm not having much luck wit the csv portion and was hoping you could help?
Here is what I have so far (please let me know if there are better ways to do this, Im brand new to powershell). I only want to add records to the csv if the test-path fails to find the 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 FROM FileTable WHERE Type IN (2, 5)"
$command = new-Object Oracle.DataAccess.Client.OracleCommand($queryString, $connection)
$mediaObjRS = $command.ExecuteReader()
# Loop through recordset
while ($mediaObjRS.read()) {
# Assign variables from 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 to output file
$objectKey | Export-Csv missingfiles.csv
$objectSeq | Export-Csv missingfiles.csv
$objectType | Export-Csv missingfiles.csv
$objectFileName | Export-Csv missingfiles.csv
}
}
$connection.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次写入 Missingfiles.cs 都会破坏前一个文件。另外,此 cmdlet 面向保存对象,其中每个属性代表逗号分隔值之一。
最简单的方法是手动写入(追加)到 csv 文件:
“灵活”的 PowerShell 方法是为每个文件创建一个对象,然后将它们放入一个数组中,完成后,将该对象数组导出到 CSV 文件,例如:
Each time you write to the missingfiles.cs you clobber the previous one. Plus this cmdlet is oriented towards saving objects where each property represents one of the comma separated values.
The simplest way to do this is to manually write (append) to the csv file:
The "slick" PowerShell way to do it would be to create an object for each file and then put those in an array and when you're done, export that array of object to a CSV file e.g.: