【问题标题】:Generate New Path via PowerShell通过 PowerShell 生成新路径
【发布时间】:2018-05-25 09:22:38
【问题描述】:

我目前正在研究将数据库从一台服务器迁移到另一台服务器的 PowerShell 脚本。迁移的一部分包括在新服务器上恢复数据库。我想让这种自动化尽可能地动态化,所以我的部分脚本会打开一个文件浏览器窗口,并允许用户选择在源服务器和新服务器上备份数据库的位置。

这是用户输入步骤的概述。

  1. 脚本获取关于源服务器和实例的信息。 (存储在 $server_instance 中)
  2. 脚本获取关于数据库迁移到的新服务器和实例的信息。 (存储在 $new_server_instance 中)
  3. 脚本获取源服务器上备份文件的存储路径。该脚本打开文件资源管理器并将路径存储在变量中。 (存储在 $local_backup_path
  4. 脚本获取目标服务器上备份文件的存储路径。该脚本打开文件资源管理器并将路径存储在不同的变量中。 (存储在 $target_backup_path
  5. 脚本在源服务器上执行数据库备份并将备份文件从 $local_backup_path 复制到 $target_backup_path。
  6. 脚本从 $target_backup_path 恢复新服务器上的数据库。 这是我遇到问题的步骤

这是路径示例,并引导我提出实际问题:

$local_backup_path = T:\PowerShell\Testing\backup

$target_backup_path = \\$target_server\T$\PowerShell\backups

我正在运行这些命令来从最近迁移的备份文件中恢复新服务器上的数据库:

Restore-SqlDatabase -ServerInstance $New_Server_Instance -Database $database -BackupFile "$target_backup_path\$database.bak"
Write-Host "$database has been restored on the new server"

这是有问题的,因为在 -ServerInstance 参数中已经提供了服务器时,路径中的服务器名称。那么,如何将路径从 \\$target_server\T$\PowerShell\backups 更改为 T:\Powershell\backups

我需要进行此更改,以便它适用于用作参数的任何服务器。

【问题讨论】:

    标签: sql-server database powershell migration migrate


    【解决方案1】:

    看起来这很有效,比我预期的要简单得多:

    $target_backup_path = $target_backup_path.Replace("\\$target_server\", "")
    $target_backup_path = $target_backup_path.Replace("$", ":")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 2015-12-23
      • 2016-05-10
      相关资源
      最近更新 更多