How to replace middle chars with * in a string?

前端 未结 1 454
星月不相逢
星月不相逢 2021-01-23 09:47

I have the following

$builder = [System.Data.SqlClient.SqlConnectionStringBuilder]::New(\'Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Passwor         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 10:11

    function Hide-ConnectionStringPassword {
        param(
           [parameter(Mandatory,ValueFromPipeline)]
           [System.Data.SqlClient.SqlConnectionStringBuilder]$ConnectionString
        )
        [string]$FistChar = $ConnectionString.Password[0]
        [string]$LastChar = $ConnectionString.Password[($ConnectionString.Password.Length - 1)]
        [string]$Stars = '*' * ($ConnectionString.Password.Length - 2)
        $ConnectionString.Password = $FistChar + $Stars + $LastChar 
        return $ConnectionString.ConnectionString
    }
    
    Hide-ConnectionStringPassword 'Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password12!553;'
    

    outputs:

    Data Source=datasource.com;User ID=UID1;Password=p************3;Connect Timeout=120
    

    0 讨论(0)
提交回复
热议问题