我在此question
上没有得到正确的帮助所以我在这里发布我想到的另一种选择:
我有以下脚本,用于更改级别1100和1400多维数据集/数据库的连接字符串
$newConnectionString = "Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password123553;Persist Security Info=True;Session Character Set=UTF8"
$AS = New-Object Microsoft.AnalysisServices.Server
$AS.connect("$Server")
$cubeName = $Analysis_Server.Databases.FindByName($Cube)
$compatibility_lvl = $cubeName.CompatibilityLevel
if ($compatibility_lvl -lt 1200) #1103
{
$cubeName.DataSources[0].ConnectionString = $newConnectionString
$cubeName.DataSources[0].Update()
$lt1200 = $($cubeName.DataSources[0].ConnectionString)
Write-Host "$lt1200`r`n" -Fore yellow
}
else
{
$TAS = new-Object Microsoft.AnalysisServices.Tabular.Server
$TAS.Connect("$Server")
$TAS.Databases[$Cube].model.datasources[0].ConnectionString = $newConnectionString
$TAS.Databases[$Cube].Update([Microsoft.AnalysisServices.UpdateOptions]::ExpandFull)
$gt1200 = $($TAS.Databases[$Cube].model.datasources[0].ConnectionString)
Write-Host "$gt1200`r`n" -Fore yellow
}
这些语句中的:
$lt1200 = $($cubeName.DataSources[0].ConnectionString)
Write-Host "$lt1200`r`n" -Fore yellow
$gt1200 = $($TAS.Databases[$Cube].model.datasources[0].ConnectionString)
Write-Host "$gt1200`r`n" -Fore yellow
这就是我得到的输出
连接超时= 120;用户ID = UID1;数据 Source = datasource.com; Password = password123553; Persist Security Info = True;会话字符集= UTF8
我应该只将其作为输出:
连接超时= 120;用户ID = UID1;数据源= datasource.com;持久安全信息=正确;会话字符集= UTF8
由于我无法找到刷新数据源的方法,除非重新连接到服务器并打印出没有密码的连接字符串,所以我希望使用以下方案将正则表达式替换为密码:
连接超时= 120;用户ID = UID1;数据 Source = datasource.com; Password = ********; Persist Security Info = True;会话字符集= UTF8
连接超时= 120;用户ID = UID1;数据 Source = datasource.com; Password = p ******* 3; Persist Security Info = True;会话字符集= UTF8
连接超时= 120;用户ID = UID1;数据 Source = datasource.com; Password = pas ********; Persist Security Info = True;会话字符集= UTF8
我知道会是这样,但是我不确定上述情况下的正则表达式是什么:
$lt1200 = $($cubeName.DataSources[0].ConnectionString) -Replace($_ "Password=*?;", "Password=********");
答案 0 :(得分:1)
以与密码相同的长度替换。不是很优雅,但是可以用
function Hide-ConnectionStringPassword {
param(
[string]$ConnectionString
)
$re = [regex]::new("Password=(.*);")
$match = $re.Match($ConnectionString)
[string]$password = $match.Groups[1].Value
[string]$stars = "*" * $password.Length
return $ConnectionString -replace 'Password=.*;', "Password=$stars;"
}
Hide-ConnectionStringPassword "Source=datasource.com;Password=password123553;"
输出:
Source=datasource.com;Password=**************;
答案 1 :(得分:1)
您可能应该研究SqlConnectionStringBuilder
.net类。它可以解析连接字符串并将其转换为对象。您将不需要正则表达式来查找密码,并且可以将密码替换为您想要的任何内容。
$builder = [System.Data.SqlClient.SqlConnectionStringBuilder]::New('Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password123553;')
$builder.Password