dataplat / dbatools

🚀 SQL Server automation and instance migrations have never been safer, faster or freer
https://dbatools.io
MIT License
2.47k stars 802 forks source link

Cannot convert argument "srv" #4194

Closed ruudvandebeeten closed 6 years ago

ruudvandebeeten commented 6 years ago

Before submitting a bug report:

Steps to Reproduce

Running script below in Powershell ISE.

Import-Module dbatools;

[string] $InstanceName = "SOSQL25\SOSQL25A";
[string] $InstanceBackupPath = '\\path\to\backups\';
[string] $Query = "SELECT @@SERVERNAME;";

Get-DbaBackupInformation -SqlInstance $InstanceName -DatabaseName master -Path $InstanceBackupPath; 

Write-Host "First statement works";

Invoke-Sqlcmd -ServerInstance $InstanceName -Database master -Query $Query;

Write-Host "Second statement also works";

Get-DbaBackupInformation -SqlInstance $InstanceName -DatabaseName master -Path $InstanceBackupPath; 

Write-Host "Third statement fails";

Expected Behavior

All the statements are ran without error.

Actual Behavior

After running the Invoke-Sqlcmd the second Get-DbaBackupInformation returns a error, see script output below.

error

Cannot convert argument "srv", with value: "[SOSQL25\SOSQL25A]", for "ReadBackupHeader" to type Microsoft.SqlServer.Management.Smo.Server": "Cannot convert the "[SOSQL25\SOSQL25A]" value of type "Microsoft.SqlServer.Management.Smo.Server" to type "Microsoft.SqlServer.Management.Smo.Server".

It seems that Invoke-Sqlcmd or Get-DbaBackupInformation skrews with shared variables or objects.

When using Invoke-Sqlcmd2 the problem does not arise. But I'm not able to change all the existing Powershell code atm. Running the script the second time, even the first statement containing Get-DbaBackupInformation fails.

Environmental data

Stuart-Moore commented 6 years ago

Afraid I can't replicate that: image

Could you rerun this with $instance.GetType() after each statement so we can see if the type changes

Neither command modifies the $InstanceName in my example, and I neither command does that anyway as far as I know.

potatoqualitee commented 6 years ago

Thanks so much for looking into this, Stuart!

ruudvandebeeten commented 6 years ago

Afraid I can't replicate that: image

Could you rerun this with $instance.GetType() after each statement so we can see if the type changes

Neither command modifies the $InstanceName in my example, and I neither command does that anyway as far as I know.

Thank you for your reply.

$instance.GetType() does not return anything.

Do you mean $InstanceName.GetType()? That is returning System.String

Stuart-Moore commented 6 years ago

Sorry, was doing 3 things at once, it should have been $instancename.GetType()

On Mon, 15 Oct 2018 at 07:44 ruudvandebeeten notifications@github.com wrote:

Afraid I can't replicate that: [image: image] https://user-images.githubusercontent.com/5613670/46906705-9f561000-ceff-11e8-8f43-64efc05cc86e.png

Could you rerun this with $instance.GetType() after each statement so we can see if the type changes

Neither command modifies the $InstanceName in my example, and I neither command does that anyway as far as I know.

Thank you for your reply.

$instance.GetType() does not return anything.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/sqlcollaborative/dbatools/issues/4194#issuecomment-429726736, or mute the thread https://github.com/notifications/unsubscribe-auth/AFWoZj7aX_iluwKr438Va7HD5blwVzAxks5ulC7jgaJpZM4XZbLG .

potatoqualitee commented 6 years ago

I believe this is likely an issue with ISE and not dbatools. Can you try your command outside of ISE @ruudvandebeeten ?

potatoqualitee commented 6 years ago

Actually, I just reread and Invoke-Sqlcmd is being run which means SqlServer or SQLPS is being loaded. Unfortunately, we do not support this configuration due to issues like this, our apologies, it's not possible to fix.

ruudvandebeeten commented 6 years ago

@potatoqualitee thank you for looking into the problem I'm faceing. When running the script outside of ISE the error is not occuring.

potatoqualitee commented 6 years ago

Happy to! I do try to avoid the ISE myself except when I have to (presentations). VS Code's host causes some issues as well. I generally try to do straight console because it's reliable.

You may run into unexpected issues in the future with the console+SqlServer too, however, I've found mixing them to be inconsistent.