|
1 | | -# dbatools.library |
| 1 | +# dbatools.library |
| 2 | + |
| 3 | +[](https://www.powershellgallery.com/packages/dbatools.library) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +The library that powers [dbatools](https://dbatools.io), the community module for SQL Server professionals. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +dbatools.library is a .NET library that provides the core functionality for the dbatools PowerShell module. It includes: |
| 11 | + |
| 12 | +- SQL Server Management Objects (SMO) integration |
| 13 | +- Microsoft.Data.SqlClient for SQL Server connectivity |
| 14 | +- DacFx for database deployment operations |
| 15 | +- Extended Events (XEvent) processing capabilities |
| 16 | +- Multi-framework support (.NET Framework 4.7.2 and .NET 8.0) |
| 17 | + |
| 18 | +This library enables dbatools to work seamlessly across Windows PowerShell 5.1 and PowerShell 7+ on Windows, macOS, and Linux. |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +Install from the PowerShell Gallery: |
| 23 | + |
| 24 | +```powershell |
| 25 | +# Recommended: Install for all users (requires admin/sudo) |
| 26 | +Install-Module dbatools.library -Scope AllUsers |
| 27 | +
|
| 28 | +# Or install for current user only |
| 29 | +Install-Module dbatools.library -Scope CurrentUser |
| 30 | +``` |
| 31 | + |
| 32 | +### ⚠️ Important: PowerShell Core + Credentials Issue |
| 33 | + |
| 34 | +**If you plan to use SQL Server credentials with PowerShell Core (pwsh), you MUST install to AllUsers scope or grant appropriate permissions.** |
| 35 | + |
| 36 | +#### The Issue |
| 37 | + |
| 38 | +When using `-SqlCredential` with PowerShell Core, you may encounter this error: |
| 39 | + |
| 40 | +``` |
| 41 | +unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll' |
| 42 | +``` |
| 43 | + |
| 44 | +#### Root Cause |
| 45 | + |
| 46 | +This is a **PowerShell Core + Microsoft.Data.SqlClient architectural limitation**, not a bug in dbatools.library: |
| 47 | + |
| 48 | +1. **Credential Impersonation**: When credentials are passed to SQL Server connections, the .NET runtime performs thread-level impersonation using those credentials. |
| 49 | + |
| 50 | +2. **DLL Access Under Impersonation**: During impersonation, `Microsoft.Data.SqlClient` tries to load its native dependency `Microsoft.Data.SqlClient.SNI.dll`, but file system access occurs under the **impersonated credential's security context**, not your current user's context. |
| 51 | + |
| 52 | +3. **Permission Denied**: The impersonated account often lacks read permissions to the module files in your local profile directory (e.g., `C:\Users\<user>\Documents\PowerShell\Modules\`), causing the DLL load to fail. |
| 53 | + |
| 54 | +#### Why Windows PowerShell 5.1 Works |
| 55 | + |
| 56 | +- Uses `System.Data.SqlClient` which is available in the Global Assembly Cache (GAC) |
| 57 | +- Different assembly loading behavior that doesn't trigger the same impersonation/access pattern |
| 58 | +- The native SNI.dll is already loaded system-wide |
| 59 | + |
| 60 | +#### Solutions |
| 61 | + |
| 62 | +**Option 1: Install to AllUsers Scope (Recommended)** |
| 63 | + |
| 64 | +```powershell |
| 65 | +# Uninstall any existing CurrentUser installation first |
| 66 | +Uninstall-Module dbatools.library -Force |
| 67 | +Uninstall-Module dbatools -Force |
| 68 | +
|
| 69 | +# Install to AllUsers scope (requires admin on Windows, sudo on Linux/macOS) |
| 70 | +Install-Module dbatools.library -Scope AllUsers |
| 71 | +Install-Module dbatools -Scope AllUsers |
| 72 | +``` |
| 73 | + |
| 74 | +This places the module in `C:\Program Files\PowerShell\Modules\` (Windows) or `/usr/local/share/powershell/Modules` (Linux/macOS), which typically has broader read permissions. |
| 75 | + |
| 76 | +**Option 2: Grant Permissions (Windows)** |
| 77 | + |
| 78 | +If you cannot use AllUsers scope, grant the credential account read access to your PowerShell modules folder: |
| 79 | + |
| 80 | +```powershell |
| 81 | +$modulePath = "$env:USERPROFILE\Documents\PowerShell\Modules" |
| 82 | +icacls $modulePath /grant "DOMAIN\User:(OI)(CI)R" /T |
| 83 | +``` |
| 84 | + |
| 85 | +**Option 3: Use Windows PowerShell 5.1** |
| 86 | + |
| 87 | +If neither option above works for your environment, use Windows PowerShell instead of PowerShell Core for credential-based connections: |
| 88 | + |
| 89 | +```powershell |
| 90 | +# From Windows PowerShell 5.1 |
| 91 | +Import-Module dbatools |
| 92 | +Connect-DbaInstance -SqlInstance server -SqlCredential $cred |
| 93 | +``` |
| 94 | + |
| 95 | +#### Additional Notes |
| 96 | + |
| 97 | +- Commands like `Get-DbaDiskSpace -Credential` work fine because they use WinRM/PowerShell Remoting, not SQL Server authentication |
| 98 | +- This issue affects any PowerShell Core module using Microsoft.Data.SqlClient with credentials |
| 99 | +- Related to [PowerShell Issue #11616](https://github.com/PowerShell/PowerShell/issues/11616) |
| 100 | + |
| 101 | +For more details, see [Issue #28](https://github.com/dataplat/dbatools.library/issues/28). |
| 102 | + |
| 103 | +## Development |
| 104 | + |
| 105 | +### Prerequisites |
| 106 | + |
| 107 | +- .NET SDK 8.0 or later |
| 108 | +- .NET Framework 4.7.2 Developer Pack (Windows only) |
| 109 | +- PowerShell 7.2+ or Windows PowerShell 5.1 |
| 110 | +- Git |
| 111 | + |
| 112 | +### Building |
| 113 | + |
| 114 | +```bash |
| 115 | +# Clone the repository |
| 116 | +git clone https://github.com/dataplat/dbatools.library.git |
| 117 | +cd dbatools.library |
| 118 | + |
| 119 | +# Build the library |
| 120 | +dotnet build project/dbatools.sln |
| 121 | + |
| 122 | +# Or build for specific configuration |
| 123 | +dotnet build project/dbatools.sln -c Release |
| 124 | +``` |
| 125 | + |
| 126 | +The compiled assemblies will be output to `artifacts/lib/`. |
| 127 | + |
| 128 | +### Multi-Framework Support |
| 129 | + |
| 130 | +The library targets both: |
| 131 | +- **.NET Framework 4.7.2**: For Windows PowerShell 5.1 compatibility |
| 132 | +- **.NET 8.0**: For PowerShell 7+ cross-platform support |
| 133 | + |
| 134 | +### Project Structure |
| 135 | + |
| 136 | +``` |
| 137 | +dbatools.library/ |
| 138 | +├── project/ |
| 139 | +│ ├── dbatools/ # Main C# library project |
| 140 | +│ ├── dbatools.Tests/ # Unit tests |
| 141 | +│ └── dbatools.sln # Solution file |
| 142 | +├── build/ # Build scripts |
| 143 | +├── var/ # Runtime dependencies |
| 144 | +├── dbatools.library.psm1 # PowerShell module script |
| 145 | +├── dbatools.library.psd1 # PowerShell module manifest |
| 146 | +└── README.md |
| 147 | +``` |
| 148 | + |
| 149 | +### Testing |
| 150 | + |
| 151 | +```bash |
| 152 | +# Run tests |
| 153 | +dotnet test project/dbatools.Tests/dbatools.Tests.csproj |
| 154 | +``` |
| 155 | + |
| 156 | +### Importing for Development |
| 157 | + |
| 158 | +To use your local development version: |
| 159 | + |
| 160 | +```powershell |
| 161 | +# Import the module from your local clone |
| 162 | +Import-Module /path/to/dbatools.library/dbatools.library.psd1 -Force |
| 163 | +
|
| 164 | +# Verify the version and path |
| 165 | +Get-Module dbatools.library | Select-Object Name, Version, Path |
| 166 | +``` |
| 167 | + |
| 168 | +## Key Dependencies |
| 169 | + |
| 170 | +This library includes several major SQL Server components: |
| 171 | + |
| 172 | +| Package | Version | Purpose | |
| 173 | +|---------|---------|---------| |
| 174 | +| Microsoft.Data.SqlClient | 6.0.2 | SQL Server connectivity | |
| 175 | +| Microsoft.SqlServer.SqlManagementObjects | 172.76.0 | SQL Server Management Objects (SMO) | |
| 176 | +| Microsoft.SqlServer.DacFx | 170.0.94 | Data-tier Application Framework | |
| 177 | +| Microsoft.AnalysisServices | 19.101.1 | Analysis Services management | |
| 178 | +| Microsoft.SqlServer.XEvent.XELite | 2024.2.5.1 | Extended Events processing | |
| 179 | + |
| 180 | +## Contributing |
| 181 | + |
| 182 | +Contributions are welcome! This library is primarily maintained by the dbatools team. |
| 183 | + |
| 184 | +### Reporting Issues |
| 185 | + |
| 186 | +- For bugs specific to this library, open an issue in this repository |
| 187 | +- For dbatools-related issues, use the [dbatools repository](https://github.com/dataplat/dbatools) |
| 188 | +- For the DLL loading issue with credentials, see [Issue #28](https://github.com/dataplat/dbatools.library/issues/28) |
| 189 | + |
| 190 | +### Pull Requests |
| 191 | + |
| 192 | +1. Fork the repository |
| 193 | +2. Create a feature branch (`git checkout -b feature/your-feature`) |
| 194 | +3. Make your changes |
| 195 | +4. Add tests if applicable |
| 196 | +5. Ensure the build succeeds |
| 197 | +6. Commit your changes (`git commit -m 'Add some feature'`) |
| 198 | +7. Push to the branch (`git push origin feature/your-feature`) |
| 199 | +8. Open a Pull Request |
| 200 | + |
| 201 | +## Resources |
| 202 | + |
| 203 | +- **dbatools Website**: https://dbatools.io |
| 204 | +- **dbatools Documentation**: https://docs.dbatools.io |
| 205 | +- **dbatools Repository**: https://github.com/dataplat/dbatools |
| 206 | +- **Community Slack**: https://dbatools.io/slack |
| 207 | +- **Twitter**: [@psdbatools](https://twitter.com/psdbatools) |
| 208 | + |
| 209 | +## License |
| 210 | + |
| 211 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 212 | + |
| 213 | +## Authors |
| 214 | + |
| 215 | +Created and maintained by the [dbatools team](https://dbatools.io/team) and community contributors. |
| 216 | + |
| 217 | +Special thanks to: |
| 218 | +- Chrissy LeMaire ([@cl](https://github.com/potatoqualitee)) |
| 219 | +- All [contributors](https://github.com/dataplat/dbatools.library/graphs/contributors) |
0 commit comments