AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 219777
Accepted
Peter Vandivier
Peter Vandivier
Asked: 2018-10-11 07:25:36 +0800 CST2018-10-11 07:25:36 +0800 CST 2018-10-11 07:25:36 +0800 CST

DBATools - 将平面文件配置传递给代理作业命令

  • 772

我正在尝试将我的 SQL Agent 作业和计划作为.json文件进行源控制,并使用dbatools SQL Agent 命令套件部署它们。

给定$sa = Get-Credential和foo.config以下形式......

{
    "Schedule":                  "Foo",
    "Disabled":                  false,
    "FrequencyType":             "Weekly",
    "FrequencyInterval":         "EveryDay",
    "FrequencySubdayType":       "Time",
    "FrequencySubdayInterval":   0,
    "FrequencyRelativeInterval": "Unused",
    "FrequencyRecurrenceFactor": 1,
    "StartDate":                 "20180823",
    "EndDate":                   "20181023",
    "StartTime":                 "070000",
    "EndTime":                   "235959"
}

尝试foo.config在解决方案中使用失败并显示以下消息的基本解析:

~> $foo = Get-Content foo.config | ConvertFrom-Json
~> New-DbaAgentSchedule @foo -ServerInstance "." -SqlCredential $sa

警告:[15:50:04][New-DbaAgentSchedule] 未提供时间表!请提供时间表名称。

好吧,这很糟糕......特别是因为以下工作正常......

$bar = @{
    Schedule=                  "Foo"
    Disabled=                  $false
    FrequencyType=             "Weekly"
    FrequencyInterval=         "EveryDay"
    FrequencySubdayType=       "Time"
    FrequencySubdayInterval=   0
    FrequencyRelativeInterval= "Unused"
    FrequencyRecurrenceFactor= 1
    StartDate=                 "20180823"
    EndDate=                   "20181023"
    StartTime=                 "070000"
    EndTime=                   "235959"
}

New-DbaAgentSchedule @bar -ServerInstance "." -SqlCredential $sa

我真的不想打扰每个人... -Param1 $foo.Param1 -Param2 $foo.Param2 ... ,因为我很懒。我真的更愿意将我的配置文件拼成各种命令。为什么这不起作用!!?!1!

sql-server-agent powershell
  • 2 2 个回答
  • 181 Views

2 个回答

  • Voted
  1. Peter Vandivier
    2018-10-11T07:25:36+08:002018-10-11T07:25:36+08:00

    好吧,4 小时前的自己......如果你费心检查 和 的类型$foo,$bar你可能不会浪费整个下午在你的开放式办公室平面图上大声咒骂,因为你毫无结果地滚动浏览文档......

    ~> ($foo | Get-Member).TypeName[0]
    System.Management.Automation.PSCustomObject
    
    ~> ($bar | Get-Member).TypeName[0]
    System.Collections.Hashtable
    

    在这个问答网络上什至有一个方便的通用解决方案,用于将 aPSCustomObject转换为HashTable.

    $foo.psobject.properties | foreach -begin {
        $foo=@{}
    } -process {
        $foo."$($_.Name)" = $_.Value
    } -end {$foo}
    
    ~> New-DbaAgentSchedule @foo -ServerInstance "." -SqlCredential $sa
    

    简单的豌豆,柠檬汁。

    所以扑通一声,你个大疯子。当您不再觉得自己很愚蠢时,也许可以在线分享解决方法,以防对其他人有所帮助。如果您感觉非常主动,可以考虑留出一些时间来为该模块做贡献。

    • 2
  2. Best Answer
    user507
    2018-10-11T13:52:08+08:002018-10-11T13:52:08+08:00

    您可以做的一件事是,当您从 json 转换为实际将其转换为 hastable 时,有一个选项……因此它可以节省几行代码。

    (Get-Content C:\temp\foo.config | ConvertFrom-Json).GetType()
    <#
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     False    PSCustomObject                           System.Object
    #>
    (Get-Content C:\temp\foo.config | ConvertFrom-Json -AsHashtable).GetType()
    <#
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Hashtable                                System.Object
    #>
    

    我没有任何可访问的服务器来确认,但第二个命令的输出显示它是名称/值格式:

    Name                           Value
    ----                           -----
    EndTime                        235959
    StartDate                      20180823
    FrequencySubdayInterval        0
    FrequencyType                  Weekly
    Schedule                       Foo
    Disabled                       False
    FrequencyRelativeInterval      Unused
    EndDate                        20181023
    FrequencySubdayType            Time
    FrequencyInterval              EveryDay
    StartTime                      070000
    FrequencyRecurrenceFactor      1
    

    由于以上仅适用于 PowerShell Core 6.1(现已普遍可用),您将需要使用WindowsCompatibility模块在 PS Core 中使用 dbatools。您可以在 PS Core 中使用以下代码执行此操作:

    Install-Module WindowsCompatibility
    Import-WinModule dbatools
    

    第二个命令利用 WinRm 来支持隐式远程处理。以上意味着您已经在 Windows 机器上安装了 PowerShell Core。文档显示了如何在需要时为远程机器执行此操作。

    从那里您可以根据需要运行 dbatools 命令:

    在此处输入图像描述

    • 2

相关问题

  • 指定 SQL 代理计划的频率范围

  • 为什么 Sql Server Agent 的 Next Run Time 值可能不是我所期望的?这肯定是我下次工作的时候吗?

  • powershell 连接到 SQL 并在无法连接时显示友好消息

  • powershell 获取 sql server 内存计数器并显示值

  • SQL Server 代理错误

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve