You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用dbatools导出SQL对象时如何避免生成子文件夹?

解决dbatools Export-DbaScript自动创建子文件夹的问题

你当前使用Export-DbaScript时,工具默认会按对象名生成子文件夹,并在文件名中附加服务器名和时间戳。要直接将SQL脚本生成到指定根目录,无需创建子文件夹,只需添加-NoClutter参数即可,同时优化路径拼接逻辑避免潜在问题:

修改后的SQL Agent作业导出脚本

$SourceServer = "***";
$folder = "c:\temp\sqljobs\"

# 确保目标文件夹存在,避免导出失败
if (-not (Test-Path $folder)) {
    New-Item -ItemType Directory -Path $folder | Out-Null
}

Get-DbaAgentJob -SqlInstance $SourceServer | ForEach-Object {
    Export-DbaScript -InputObject $_ -Path (Join-Path $folder "$($_.Name).sql") -NoClutter
}

修改后的数据库表导出脚本

$SourceServer = "***";
$SourceDB = "***";
$folder = "c:\temp\sqltables\"

# 确保目标文件夹存在,避免导出失败
if (-not (Test-Path $folder)) {
    New-Item -ItemType Directory -Path $folder | Out-Null
}

Get-DbaDBTable -SqlInstance $SourceServer -Database $SourceDB | ForEach-Object {
    Export-DbaScript -InputObject $_ -Path (Join-Path $folder "$($_.Name).sql") -NoClutter
}

参数说明

  • -NoClutter:禁用Export-DbaScript默认的文件夹结构生成规则,直接将脚本写入你指定的完整文件路径,不会创建子文件夹,也不会自动附加服务器名、时间戳到文件名。
  • Join-Path:替代字符串拼接路径的方式,自动处理路径分隔符,避免因路径格式错误导致的问题。
  • 文件夹检查逻辑:提前创建目标目录,防止因目录不存在导致导出操作失败。

内容的提问来源于stack exchange,提问作者Ricardo Trindade

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:17:12