0

我有一个可以工作的 PowerShell 脚本,它可以帮助我对多个服务器运行多个查询,并将每个输出保存在不同的 CSV 中,然后将它们合并到一个 Excel 文件中。

$Servers = get-content -Path "Servers.txt"                                                                  
$DatabaseName ="master"                                                                                                                             
#$credential = Get-Credential #Prompt for user credentials 
$secpasswd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("sa", $secpasswd)
$QueriesFolder = "Queries\"                                                                                                                         
$ResultFolder = "Results\"                                                                                                                          

ForEach($Server in $Servers)                                                                                                                        
    {
    $DateTime = (Get-Date).tostring("yyyy-MM-dd")                                                                                                   
    ForEach ($filename in get-childitem -path $QueriesFolder -filter "*.sql" | sort-object {if (($i = $_.BaseName -as [int])) {$i} else {$_}} )     
        {
        $oresults = invoke-sqlcmd -ServerInstance $Server -Database $DatabaseName -Credential $credential -InputFile $filename.fullname             
        write-host "Executing $filename on $Server"                                                                                                 
        $BaseNameOnly = Get-Item $filename.fullname | Select-Object -ExpandProperty BaseName
        $oresults | export-csv $ResultFolder$BaseNameOnly.csv -NoTypeInformation -Force
        } 

    $All_CSVs = get-childitem -path $ResultFolder -filter "*.csv" | sort-object {if (($i = $_.BaseName -as [int])) {$i} else {$_}} 
    $Count_CSVs = $All_CSVs.Count
    Write-Host "Detected the following CSV files: ($Count_CSVs)"
    Write-Host " "$All_CSVs.Name"`n"
    $ExcelApp = New-Object -ComObject Excel.Application
    $ExcelApp.SheetsInNewWorkbook = $All_CSVs.Count
    $output = "C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx"
    if (Test-Path $output) 
        {
        Remove-Item $output
        Write-Host Removing: $output because it exists already
        }
    $xlsx = $ExcelApp.Workbooks.Add()
    for($i=1;$i -le $Count_CSVs;$i++) 
        {
        $worksheet = $xlsx.Worksheets.Item($i)
        $worksheet.Name = $All_CSVs[$i-1].Name
        $file = (Import-Csv $All_CSVs[$i-1].FullName)
        $file | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Clip
        $worksheet.Cells.Item(1).PasteSpecial()|out-null
        }

    $xlsx.SaveAs($output)
    Write-Host Creating: $output 
    $ExcelApp.Quit() 
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xlsx) | Out-Null; 
    Write-Host "Closing all worksheet"
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelApp) | Out-Null; 
    Write-Host "Closing Excel"
    [System.GC]::Collect(); 
    [System.GC]::WaitForPendingFinalizers()
    Remove-Item "$ResultFolder\*" -Include *.csv
    Write-Host "Cleaning all *.csv"
    Start-Sleep -Seconds 3
    }

在此处输入图像描述

为了使这个脚本更具可移植性,我希望将其中提到的所有路径存储到一个变量中,然后连接起来。但是一旦我改变:

$output = "C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx" 

进入:

$output = $ResultFolder + $Server + " $DateTime.xlsx"

事情变得很糟糕,我收到错误:

Microsoft Excel cannot access the file 'C:\Users\FrancescoM\Documents\Results\0DC80000'.
There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
At C:\Users\FrancescoM\Desktop\CSV\QueryLauncher.ps1:50 char:2
+     $xlsx.SaveAs($output)
+     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

在此处输入图像描述

我不明白,我认为我正在连接正确的东西。

我还关注了这个StackOverflow 帖子,并在添加“C:\Windows\SysWOW64\config\systemprofile\desktop”后重新启动了我的计算机,但问题并没有解决。

可变路径如何与 Excel 混淆?

4

1 回答 1

2

因为您没有在$ResultFolder变量中定义完整路径,所以它将使用当前工作目录进行扩展。

只需查看您想要的路径:

"C:\Users\FrancescoM\Desktop\CSV\Results\" + $Server + " $DateTime.xlsx"

以及使用部分 $ResultFolder 变量的结果路径:

C:\Users\FrancescoM\Documents\Results\0DC80000

由于您希望将输出文件放在桌面上的文件夹中,$output请将

$output = Join-Path $([Environment]::GetFolderPath("Desktop")) "CSV\Results\$Server $DateTime.xlsx"


编辑

从您的上一条评论中,我了解到您希望输出位于脚本本身所在的文件夹内的名为“Results”的子文件夹中。在这种情况下,请执行以下操作:

# get the folder this script is running from
$ScriptFolder = if ($PSScriptRoot) { $PSScriptRoot } else { Split-Path $MyInvocation.MyCommand.Path }
# the following paths are relative to the path this script is in
$QueriesFolder = Join-Path -Path $ScriptFolder -ChildPath 'Queries'
$ResultFolder  = Join-Path -Path $ScriptFolder -ChildPath 'Results'
# make sure the 'Results' folder exists; create if not
if (!(Test-Path -Path $ResultFolder -PathType Container)) {
    New-Item -Path $ResultFolder -ItemType Directory | Out-Null
}

然后,当需要保存 xlsx 文件时,使用以下命令创建完整路径和文件名:

$output = Join-Path -Path $ResultFolder -ChildPath "$Server $DateTime.xlsx"
$xlsx.SaveAs($output)

PS 我建议使用Join-Pathcmdlet 来组合文件路径或使用[System.IO.Path]::Combine()而不是像使用此行一样将路径连接在一起:$oresults | export-csv $ResultFolder$BaseNameOnly.csv. 如果您忘记使用反斜杠对第一个路径部分进行后缀,则使用后者可能会导致无法预料的路径名。

PS2 Excel 在工具->选项->常规->默认文件位置中设置了自己的默认输出路径,并且不知道脚本的相对路径。这就是为什么您应该使用完整路径和文件名进行保存的原因。

于 2018-11-29T10:49:20.007 回答