[英]Generate New Path via PowerShell
我目前正在使用PowerShell脚本将数据库从一台服务器迁移到另一台服务器。 迁移的一部分包括在新服务器上还原数据库。 我想使这种自动化尽可能地动态,因此我的脚本的一部分打开了一个文件浏览器窗口,并允许用户选择在源服务器和新服务器上备份数据库的位置。
这是用户输入步骤的概述。
这是路径的示例,将我引向我的实际问题:
$ 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 ?
我需要进行此更改,以便它适用于用作参数的任何服务器。
看起来这样有效,比我预期的简单得多:
$target_backup_path = $target_backup_path.Replace("\\$target_server\", "")
$target_backup_path = $target_backup_path.Replace("$", ":")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.