dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

[Bug] Warning errors on Write-DbaDBTableData

Open OwainEsau opened this issue 7 years ago • 8 comments

Environmental information

#### PowerShell version:

Name                           Value                                                                                                                                     
----                           -----                                                                                                                                     
PSVersion                      5.1.17758.1                                                                                                                               
PSEdition                      Desktop                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                   
BuildVersion                   10.0.17758.1                                                                                                                              
CLRVersion                     4.0.30319.42000                                                                                                                           
WSManStackVersion              3.0                                                                                                                                       
PSRemotingProtocolVersion      2.3                                                                                                                                       
SerializationVersion           1.1.0.1                                                                                                                                   

#### dbatools Module version:
Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.834\dbatools.psd1
Version : 0.9.834

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.804\dbatools.psd1
Version : 0.9.804

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.531\dbatools.psd1
Version : 0.9.531

Name    : dbatools
Path    : C:\Program Files\WindowsPowerShell\Modules\dbatools\0.9.487\dbatools.psd1
Version : 0.9.487

LCID             Name             DisplayName
----             ----             -----------
3081             en-AU            English (Australia)

SQL Server:

Microsoft SQL Server 2017 (RTM-GDR) (KB4494351) - 14.0.2014.14 (X64)   Apr  5 2019 09:18:51   Copyright (C) 2017 Microsoft Corporation  Developer Edition (64-bit) on Windows 10 Enterprise 10.0 <X64> (Build 17758: ) 

Report

Everything is run in powershell.exe by calling a .ps1 script. It is definitely not a permissions issue this is on localhost.

Host used

  • [x] powershell.exe
  • [ ] VS Code
  • [ ] ISE
  • [ ] Other (please specify)

If anything other than powershell.exe was used, please confirm that you can duplicate the issue with powershell.exe

Errors Received


PSMessageDetails      : 
Exception             : System.Management.Automation.ItemNotFoundException: Cannot find a variable with the name 'Default'.
TargetObject          : Default
CategoryInfo          : ObjectNotFound: (Default:String) [Get-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Write-DbaDbTableData<Begin>, C:\Program Files\WindowsPowerShell\Modules\DBATools\0.9.834\allcommands.ps1: line 108820
                        at Insert_Data, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Create-Fastrack-Database-2.ps1: line 15
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Create-Fastrack-Database-2.ps1: line 54
                        at <ScriptBlock>, <No file>: line 1
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Pre-Req-Check.ps1: line 265
                        at <ScriptBlock>, <No file>: line 1
                        at Invoke-ScriptBreak, C:\Users\owain.esau.RSPL\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\TRIS-Migration-Toolkit.ps1: line 174
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\TRIS-Migration-Toolkit.ps1: line 237
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}




PSMessageDetails      : 
Exception             : System.Management.Automation.ItemNotFoundException: Cannot find a variable with the name 'TableLock'.
TargetObject          : TableLock
CategoryInfo          : ObjectNotFound: (TableLock:String) [Get-Variable], ItemNotFoundException
FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Write-DbaDbTableData<Begin>, C:\Program Files\WindowsPowerShell\Modules\DBATools\0.9.834\allcommands.ps1: line 108820
                        at Insert_Data, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Create-Fastrack-Database-2.ps1: line 15
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Create-Fastrack-Database-2.ps1: line 54
                        at <ScriptBlock>, <No file>: line 1
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\PS\Pre-Req-Check.ps1: line 265
                        at <ScriptBlock>, <No file>: line 1
                        at Invoke-ScriptBreak, C:\Users\owain.esau.RSPL\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\TRIS-Migration-Toolkit.ps1: line 174
                        at <ScriptBlock>, C:\Users\owain.esau\OneDrive\Scripts\~Projects\TRIS-Migration-Toolkit\TRIS-Migration-Toolkit.ps1: line 237
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

I need there to be no errors as i am catching all the errors from an Invoke-Expression call:

Function Invoke-ScriptBreak {
    param ($breakable)
    Invoke-Expression $breakable -ErrorVariable e

    if ($e.count -gt 0) { 
        Set-Output "Error variable has errors: $($e.count)" -colour red -time;
        break;
    }
}

Since these errors are always there, my migration scripts always fail

Steps to Reproduce

Seems like for me, there are errors when running any DBATools module, there is always something in the error log afterwards.

Expected Behavior

I expect no errors in the error log.

Actual Behavior

There are errors on the following commands (non terminating so the script does complete):

Write-DbaDBTableData -SQLInstance $ServerInstance -InputObject $Data -Table $tableName -AutoCreateTable -confirm

OwainEsau avatar Jun 20 '19 04:06 OwainEsau

@OwainEsau please update to 1.0 release and let us know if you still have problems.

However, being that your culture on your machine would you mind grabbing the details of this command and providing them:

[data.sqlclient.sqlbulkcopyoptions] | select -expand DeclaredFields | select Name

wsmelton avatar Jun 20 '19 13:06 wsmelton

On Windows 10 different computers in ru-Ru its all english names

Name
----
value__
Default
KeepIdentity
CheckConstraints
TableLock
KeepNulls
FireTriggers
UseInternalTransaction
AllowEncryptedValueModifications

olegstrutinskii avatar Jun 20 '19 13:06 olegstrutinskii

[data.sqlclient.sqlbulkcopyoptions] | select -expand DeclaredFields | select Name

@wsmelton Same issue with 1.0. The output of that command is:

Name
----
value__
Default
KeepIdentity
CheckConstraints
TableLock
KeepNulls
FireTriggers
UseInternalTransaction
AllowEncryptedValueModifications

OwainEsau avatar Jun 20 '19 23:06 OwainEsau

This issue has been automatically marked as stale because it has not had recent activity. If you would like this issue to remain open:

  • Verify that you can still reproduce the issue in the latest version of dbatools
  • Comment that the issue is still reproducible and include:
    • What version of dbatools you reproduced the issue on
    • What OS and version you reproduced the issue on
    • What steps you followed to reproduce the issue

stale[bot] avatar Aug 23 '19 20:08 stale[bot]

Ok, so after looking at this...what you are seeing is by design with PowerShell. This has nothing to do with our module directly.

The line of code you are seeing with Write-DbaDbTableData is below:

https://github.com/sqlcollaborative/dbatools/blob/de150c789285f82678cd87ba5558b0d93c9a32e5/functions/Write-DbaDbTableData.ps1#L492-L500

We set the error to SilentlyContinue because we know it will happen in some situations. All this does is hide the error from being displayed on the console. Your call to Invoke-Expression -ErrorVariable e will always capture that error, this is just how PowerShell works.

An example:

$cmd = "Get-Process -Name doesnotexist -ErrorAction SilentlyContinue"
Invoke-Expression $cmd -ErrorVariable e
$e.Count

Even though I call the command Get-Process with Error Action set to silent, I'll still capture it in the ErrorVariable. You will need to look at a different way to validate an issue in your calls has occurred.

wsmelton avatar Sep 08 '19 09:09 wsmelton

The issue is that the silentlyContinue still sets a value in the $ERROR variable. A cleaner way is to add a test-path so that $optionValue = Get-Variable $option -ValueOnly -ErrorAction SilentlyContinue becomes if (test-path variable:$option) {$optionValue = Get-Variable $option -ValueOnly}

JC-aus avatar Mar 15 '23 07:03 JC-aus

Correct. That is why the use of Invoke-Expression catches on the error. Your solution will still have the same problem because Get-Variable is what fails.

wsmelton avatar Mar 15 '23 12:03 wsmelton

SHawn.

I have to disagree. The test-path gets a false response and so the test-Variable will not execute (and so will not fail).


From: Shawn Melton @.> Sent: Wednesday, 15 March 2023 8:32 PM To: dataplat/dbatools @.> Cc: John Cuthbertson @.>; Comment @.> Subject: Re: [dataplat/dbatools] [Bug] Warning errors on Write-DbaDBTableData (#5793)

⚠️ EXTERNAL EMAIL: Do not click links or open any attachments unless you trust the sender and know the content is safe. ⚠️

Correct. That is why the use of Invoke-Expression catches on the error. Your solution will still have the same problem because Get-Variable is what fails.

— Reply to this email directly, view it on GitHubhttps://github.com/dataplat/dbatools/issues/5793#issuecomment-1469920553, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APTGBJA2NFPUQC2DBOWDT43W4GZEHANCNFSM4HZPG5HQ. You are receiving this because you commented.Message ID: @.***>

Disclaimer

The information contained in this communication from the sender is confidential. It is intended solely for use by the recipient and others authorized to receive it. If you are not the recipient, you are hereby notified that any disclosure, copying, distribution or taking action in relation of the contents of this information is strictly prohibited and may be unlawful.

This email has been scanned for viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business. Providing a safer and more useful place for your human generated data. Specializing in; Security, archiving and compliance. To find out more visit the Mimecast website.

JC-aus avatar Mar 19 '23 05:03 JC-aus