dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

New-DbaCredential should support no passwords

Open markallisongit opened this issue 4 years ago • 5 comments

Summarize Functionality

The best way to back up a Managed Instance database to blob storage is to use a Managed Identity. In T-SQL this is achieved with:

CREATE CREDENTIAL [https://mystorageaccount.blob.core.windows.net/mycontainer] WITH IDENTITY = 'Managed Identity'; 

The backup to URL then works with something like:

BACKUP DATABASE mydatabase to URL = 'https://mystorageaccount.blob.core.windows.net/mycontainer/mydatabase.BAK'

It would be great if New-DbaCredential could support this functionality.

Is there a command that is similiar or close to what you are looking for?

No

Technical Details

No response

markallisongit avatar Feb 21 '22 17:02 markallisongit

Thanks for the feature request. Let me copy and adopt some code from the command to see if it really works like it should:

$Name = "https://mystorageaccount.blob.core.windows.net/mycontainer"
$Identity = 'Managed Identity'
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $adminCredential
$credential = New-Object Microsoft.SqlServer.Management.Smo.Credential -ArgumentList $server, $Name
$credential.MappedClassType = 0
$credential.Create($Identity)

Please try that and see if the credential is created.

@ClaudioESSilva - If you work on this: There is a bug in line 153: $Identity must be changed to $cred

andreasjordan avatar Feb 21 '22 17:02 andreasjordan

More issues: $Name is declared as an array, but is used in line 132 as $currentCred = $server.Credentials[$Name].

andreasjordan avatar Feb 21 '22 17:02 andreasjordan

$currentCred.Drop() in line 138 should be put inside a $Pscmdlet.ShouldProcess() to support -WhatIf.

andreasjordan avatar Feb 21 '22 17:02 andreasjordan

@andreasjordan thanks for pointing out those problems.

I have fixed

There is a bug in line 153: $Identity must be changed to $cred

and

$currentCred.Drop() in line 138 should be put inside a $Pscmdlet.ShouldProcess() to support -WhatIf.

regarding

More issues: $Name is declared as an array, but is used in line 132 as $currentCred = $server.Credentials[$Name].

It's not clear to me why we are using an array (same for $Identity) and not a single value. By default, in case we don't pass the -Name it will assume the -Identity value which is ok for me.

Probably putting it as a single value and not an array would be better, but will we break someone's code? 😟

I have also mentioned @potatoqualitee on the PR to try to help on that one.

ClaudioESSilva avatar Feb 21 '22 23:02 ClaudioESSilva

Closed via #8191.

andreasjordan avatar Feb 26 '22 13:02 andreasjordan