Hi @David Gray , welcome to Microsoft Q&A forum.
When we are reading the password from credentials, we cannot use it directly for safety reasons. However, there are multiple ways to read and use the password as mentioned below:
Code Scenario 1: Extracting the password from credential object and use it
$myCredential = Get-AutomationPSCredential -Name 'myCred'
$userName = $myCredential.UserName
$password = $myCredential.GetNetworkCredential().Password
$ServerInstance = 'yourserver'
$Database = 'yourdatabase'
$Query='select top 10 * from [SalesLT].[Product]'
Write-Output $password
Write-Output $userName
Write-Output $securePassword
invoke-sqlcmd -ServerInstance $ServerInstance -Database $Database -Username $userName -Password $password -Query $Query
Code Scenario 2: Using the credentials directly in sqlcmd
$myCredential = Get-AutomationPSCredential -Name 'myCred'
$ServerInstance = 'yourserver'
$Database = 'yourdatabase'
$Query='select top 10 * from [SalesLT].[Product]'
invoke-sqlcmd -ServerInstance $ServerInstance -Database $Database -Credential $myCredential -Query $Query
Please let me know if this helps or else we can discuss further.
----------
If answer helps, please mark it 'Accept Answer'