dataplat / dbatools

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

Access to TEMP Directory necessary when loading dbatools #9147

Open ft3411 opened 11 months ago

ft3411 commented 11 months ago

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

When using dbatools Version 2 several DLLs are loaded by dbatools.library. This requires access to the TEMP-Path. When trying to load dbatools in a script within a SQL Server Agent Job, there is no user profile loaded and the TEMP-Path points to C:\windows\temp. By default users are not allowed to write to this path. Allowing users write access to C:\WINDOWS\TEMP solves the problem.

It was quite hard to find the cause for this huge error message:

Add-Type : (0) : Source file 'C:\WINDOWS\TEMP\oxdwphck.0.cs' could not be found

(1) :             using System;

At C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1:79 char:17
+         $null = Add-Type -TypeDefinition $source
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], 
Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : (0) : Source file 'C:\WINDOWS\TEMP\oxdwphck.0.cs' could not be found

(1) :             using System;

At C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1:79 char:17
+         $null = Add-Type -TypeDefinition $source
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Except
   ion
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

WARNING: (0) : No source files specified

(1) :             using System;
>> TerminatingError(Add-Type): "Cannot add type. Compilation errors occurred."
>> TerminatingError(Add-Type): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot add type. Compilation errors occurred."
plopp
PS>TerminatingError(Add-Type): "Cannot add type. Compilation errors occurred."
PS>TerminatingError(New-Object): "Cannot find type [Redirector]: verify that the assembly containing this type is loaded."
PS>TerminatingError(Import-Module): "Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified."
C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1 : Could not import 
C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\desktop\lib\Azure.Core.dll : Import-Module : 
Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
At C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1:186 char:21
+             $null = Import-Module $assemblyPath
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,dbatools.library.psm1
PS>TerminatingError(Import-Module): "Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified."
C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1 : Could not import 
C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\desktop\lib\Azure.Identity.dll : Import-Module : 
Could not load file or assembly 'System.Memory, Version=4.0.1.1, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
At C:\Program Files\WindowsPowerShell\Modules\dbatools.library\2023.9.21\dbatools.library.psm1:186 char:21
+             $null = Import-Module $assemblyPath
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.ImportModuleCommand

    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,dbatools.library.psm1
PS>TerminatingError(getsysadmins.ps1): "The 'invoke-dbaquery' command was found in the module 'dbatools', but the module could not be loaded. For more information, run 'Import-Module dbatools'."

Steps to Reproduce

import-module dbatools
# or import-module dbatools.library

Please confirm that you are running the most recent version of dbatools

2.1.2

Other details or mentions

No response

What PowerShell host was used when producing this error

Windows PowerShell (powershell.exe)

PowerShell Host Version

PSVersion 5.1.22621.2428

SQL Server Edition and Build number

SQL Server 2019 (15.0.4335.1)

.NET Framework Version

4.7

wsmelton commented 11 months ago

I'm a bit confused on what the ask is in this issue? This has existed in this way since v1.0 release, or there abouts.

Are you asking that we document this to make it more known as an install requirement?

ft3411 commented 11 months ago

The issue appeared after upgrading to Version 2. There were no errors with version 1 on the same system. I thought that some kind of errorhandling would be great when the module cannot be loaded because the temp path is not accessible.