开发者

Filter a file based on an object of string

开发者 https://www.devze.com 2023-03-11 03:18 出处:网络
I have a SQL restore script (file on file system) that contains a list of databases to be resto开发者_高级运维red like so:

I have a SQL restore script (file on file system) that contains a list of databases to be resto开发者_高级运维red like so:

RESTORE DATABASE DB1... RESTORE DATABASE DB2... RESTORE DATABASE DB3...

I then have an object that contains a list of databases that are already online.

$onlineDatabases.Database outputs DB1 DB2

How can I return a new restore script that contains only the DB3 database?


$online_databases = new-object psobject -property @{database = @("DB1","DB2")}
$test_script = "RESTORE DATABASE DB1","RESTORE DATABASE DB2","RESTORE DATABASE DB3"

[regex]$online_regex = "RESTORE DATABASE " + '(‘ + (($online_databases.database |foreach {[regex]::escape($_)}) –join “|”) + ‘)’
$new_script = $test_script -notmatch $online_regex

$new_script

RESTORE DATABASE DB3

Replace the $test_script with get-content on your script file, and pipe $new_script to out-file to save it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号