我如何使用Powershell从一个SQL查询中获取输出并搜索另一个文件的输出?

问题描述 投票:0回答:1

这是我第一次使用Powershell,请原谅我的无知。

我有一个SQL查询,返回了一堆订单号。我想检查另一个文件,看看该文件中是否有一个与SQL查询返回的订单号名称相同的PDF文件。

在我的代码中,一切都正常,直到ForEach循环什么都没有返回。基于我的谷歌搜索,我认为我很接近,但我不确定我做错了什么。任何帮助将是感激的。

出于明显的原因,我已经删除了实际的文件名,而且我知道这个文件是正确的,其他命令也可以访问它,所以我知道这不是我目前的问题。我还删除了SQL查询中的敏感信息。

$statement = "SELECT A, Date FROM XXXX
WHERE STAT = 1 AND Date >= trunc(sysdate)"

    $con = New-Object System.Data.OracleClient.OracleConnection($connection_string)

    $con.Open()

    $cmd = $con.CreateCommand()
    $cmd.CommandText = $statement

    $result = $cmd.ExecuteReader()
    $list = while ($result.Read()) { $result["A"]}


    Write-Output $list​

    #########Loop through the list above here to check for matching PDF 

    ForEach ($Order in $list){
         Get-ChildItem "\\xxxxxx\" -Filter $Order -File

    #########If FALSE - notify that PDF is missing

   }

$con.close()

我还尝试了下面的代码,它让我更接近,并且实际上找到了我要找的文件,但给出了错误的答案

" Get-ChildItem : 无法找到接受参数的位置参数。

  • CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException.
    • FullQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand"。



    ForEach ($Order in $list){
         if((Get-ChildItem "\\xxxxx\" + $Order)){
            Write-Output
         } else { Write-Host "Does not exist."}

sql powershell foreach get-childitem
1个回答
0
投票

我从你的评论中了解到 $list 是一个订单号的数组.接下来,你想检查是否有一个文件夹中的文件有这个名字,正确吗?

那么我建议你使用 Test-Path 而不是 Get-ChildItem:

$folderToSearch = '\\servername\sharename\folder'
foreach ($Order in $list) {
    $fileToCheck = Join-Path -Path $folderToSearch -ChildPath ('{0}.pdf' -f $Order)
    if (Test-Path -Path $fileToCheck -PathType Leaf) {
        "File found: $fileToCheck"
    }
    else {
        "File $fileToCheck does not exist"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.