【问题标题】:powershell script can't find a file that existspowershell脚本找不到存在的文件
【发布时间】:2019-01-17 03:08:04
【问题描述】:

我有这个使用 bcp 创建文件的脚本。由于 BCP 不导出带有标题的数据,因此我需要将两个文件加入其中。如果我在另一个脚本中运行代码,它会创建 csv 文件,但是如果我运行这个脚本,它会失败,说文件不存在,这是下面脚本的最后一部分(#THE PROBLEM STARTS HERE)。 感谢任何帮助。

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction']='Stop'

$bcp = 'c:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe'
$delimiter = '","'
$server = 'BLABLABLA'
$database = 'DMB'
$schema = '.dbo.'
$dbschema = $database + $schema
$atresult = "@result"
$commas = " + ',','') + "
$head1 = 'Use '
$head2 = ' Declare ' # atresult
$head3 = ' varchar(max) select ' # atresult
$head4 = ' = COALESCE(' #atresult $commas
$head5 = 'COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' # tablename
$head6 = ' select ' #atresult
$tables = @('EntityTypes_20181205','FieldFieldTypeInfo')
$output_path = 'D:\Temp\DataModelBuild\'
# Deletes any file in Destination Folder
#if ( (Get-ChildItem $output_path | Measure-Object).Count -ne 0) {}
Get-ChildItem -Path $output_path -Include *.* -File -Recurse | foreach { $_.Delete()}    


foreach ($element in $tables) 
    {
        # GET COLUMN NAMES into header file
        $outputFile = $output_path + $element + ".header"
        $NQ = '"' + $head1 + $database + $head2 + $atresult + $head3 + $atresult + $head4 + $atresult + $commas + $head5 + ''' + $element + ''' + $head6 + $atresult + '"'
        #$arglist = "(" + $NQ + " queryout " + $outputFile + ", -S " + $server + " -T -t" + $delimiter + " -c -k)"
        $arglist  = @('"',
                      $head1,
                      $database,
                      $head2,
                      $atresult,
                      $head3,
                      $atresult,
                      $head4,
                      $atresult,
                      $commas,
                      $head5,
                      "'$element'",
                      $head6,
                      $atresult,
                      '"',
                      " queryout $outputFile",
                      "-S $server -T -t$delimiter -c -k")
        #write-output $arglist
        Start-Process -FilePath $bcp -ArgumentList $arglist
        #Read-Host

        # GET DATA into data file
        $outElement = $dbschema + "[" + $element + "]"
        $outputFile = $output_path + $element + ".data"
        $arglist  = @($outElement, "out $outputFile", "-S $server -T -t$delimiter -c -k")
        Start-Process -FilePath $bcp -ArgumentList $arglist
        #write-output $arglist
        #Read-Host
        # Merge header and data to csv
        #Invoke-Expression "&'$ExtPs1' -FilePath $output_path -FileName $element"
        #Write-Output "call "
        #Invoke-Expression "&'D:\idna\PedroTemp\cp2csv.ps1' $output_path $element"
        #Read-Host

        #THE PROBLEM STARTS HERE       
        $File1 = @("$output_path$element.header")
        $File2 = @("$output_path$element.data")
        $File3 = @("$output_path$element.csv")
        write-output $File1, $File2, $File3
        If (-not (Test-Path -Path $File1)) {
                    Write-Error "File DOES NOT EXIST '$File1'" -ErrorAction Stop
                    } else {
                    Write-Host "File EXIST"
                    }
        Add-Content $File3 -value (Get-Content $File1)
        Add-Content $File3 -value (Get-Content $File2)
        #write-output $File1, $File2, $File3 -ErrorAction Stop
        Read-Host
}

我的问题是为什么脚本没有检测到文件? 字符串有问题吗?

【问题讨论】:

  • 文件名中好像有[]。您需要使用-LiteralPath 才能正确查看。
  • Lee,你建议我如何使用 -LiteralPath?
  • 你会在像Get-ChildItem这样的项目中使用它。你有没有看过帮助? [皱眉] 尝试 TheFriendlyHelpSystem ... Get-Help *literalpath* 然后选择您遇到错误的命令之一... 可能类似于 Get-Help Get-ChildItem -Parameter LiteralPath ... 并阅读 TheFriendlyHelpSystem。

标签: powershell


【解决方案1】:

路径的字符串未正确构建。当您在字符串中使用成员时,您必须用$() 将变量和成员括起来。

$File1 = "$output_path$($element.header)"

【讨论】:

  • 澄清一下,.header、.data 和 .csv 是文件扩展名,我只是用来识别文件。根据您的建议,$File1 = "$output_path$($element.header)",我收到错误消息:在此对象上找不到属性 'header'。验证该属性是否存在。
  • 好的,如果它是扩展,你正确地构建了字符串。它将使用单个项目D:\Temp\DataModelBuild\EntityTypes_20181205.header 创建一个数组。你说这个文件存在吗?
  • 是的文件存在我可以看到正在创建的文件(.header 和 .data)。当我尝试将两个文件连接到 .csv 时出现错误。这就是为什么我写了 if 来检查文件是否存在。
  • 您的代码中的哪几行会引发该错误?当您执行的代码只是将它们与固定路径连接起来时会发生什么?
  • 我得到错误的代码是脚本的最后一部分: $File1 = @("$output_path$element.header") $File2 = @("$output_path$element.data" ) $File3 = @("$output_path$element.csv") 写入输出 $File1, $File2, $File3 If (-not (Test-Path -Path $File1)) { 写入错误“文件不存在 $ File1" -ErrorAction Stop } else { Write-Host "File EXIST" } Add-Content $File3 -value (Get-Content $File1) Add-Content $File3 -value (Get-Content $File2)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2020-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多