azureazure-devopsazure-pipelinesazure-powershellinvoke-sqlcmd

Has there been a recent change to Invoke-Sqlcmd that break it, and if so what's the fix?


I have a script that runs in an Azure DevOps pipeline that was working up until this morning (18/6/2025).

I previous had to upgrade a MS Graph Access token breaking change here

The Azure DevOps pipeline is microsoft-hosted (not self-hosted):

pool:
  vmImage: ubuntu-latest

The yaml that executes the bicep is a two task step:

steps:
- task: AzureKeyVault@1
  inputs:
    azureSubscription: $(azureServiceConnectionName)
    KeyVaultName: $(keyVaultName)
    SecretsFilter: 'dbHost, dbName, fxaName'
  name: KeyVaultSecrets
  displayName: Get Secrets from Key Vault for AuthorizeFxaToDb
          
- task: AzureCLI@2
  inputs:
    azureSubscription: $(azureServiceConnectionName)
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      az --version
      az deployment sub create \
        --location $(location) \
        --template-file $(secureTemplate) \
        --parameters \
          location=$(location) \
          keyVaultName=$(keyVaultName) \
          resourceGroup=$(resourceGroup) \
          resourceGroupAbbrv=$(resourceGroupAbbrv) \
          sqlAdminUmiName=$(sqlAdminUmiName) \
          directoryReadersGroupId=$(directoryReadersGroupId) \
          dbHost=$(dbHost) \
          dbName=$(dbName) \
          fxaName=$(fxaName)
  name: fxAuthoriseStep

This is the DeploymentScript commands:

resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
  name: sqlAdminUmiName
}

var sqlServerHostname = environment().suffixes.sqlServerHostname
var tokenResourceUrl = 'https://${substring(sqlServerHostname, 1)}'
      
resource deploymentScriptServerGroupAssignment 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
  name: name
  location: location
  kind: 'AzurePowerShell'
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${identity.id}': {}
    }
  }
  properties: {
    azPowerShellVersion: '8.3'
    arguments: '-dbHost "${dbHost}" -tokenResourceUrl "${tokenResourceUrl}" -dbName "${dbName}" -fxaName "${fxaName}" '
    scriptContent: '''
      param(
        [string] $dbHost,
        [string] $tokenResourceUrl,
        [string] $dbName,
        [string] $fxaName,
        [string] $dbRoleNames
      )
      
      echo "Step 0: about to Install-Module Microsoft.Graph -Force"
      Install-Module -Name SqlServer -Force
      Import-Module -Name SqlServer
      $token = (Get-AzAccessToken -ResourceTypeName MSGraph).Token
      echo "Step 1: token - $token"

      $secureToken = ConvertTo-SecureString $token -AsPlainText -Force
      echo "Step 2: secureToken - $secureToken"
      
      
      $query = @()
      $query += "DROP USER IF EXISTS `"$fxaName`""
      $query += "CREATE USER `"$fxaName`" FROM external provider"
      $query += "ALTER ROLE db_owner ADD MEMBER `"$fxaName`""
      $query += "ALTER ROLE db_datareader ADD MEMBER `"$fxaName`""
      $query += "ALTER ROLE db_datawriter ADD MEMBER `"$fxaName`""
      $query += "ALTER ROLE db_ddladmin ADD MEMBER `"$fxaName`""

      $query = $query -join ";"
      Invoke-Sqlcmd -ServerInstance $dbHost -Database $dbName -AccessToken "$secureToken" -Query $query
    '''
    retentionInterval: 'PT1H'
    cleanupPreference: 'OnSuccess'
    forceUpdateTag: currentTime
  }
}

The exact line of code that triggers the error is:

Invoke-Sqlcmd -ServerInstance $dbHost -Database $dbName -AccessToken "$secureToken" -Query $query

That has worked for the past month, and it was working last night.

But now when I try to run it I get this error:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Management.Automation, Version=7.4.

Can any advise if there's been a breaking change in the past day or so please? Or any known fixes?

The full error is:

ERROR: {"status":"Failed","error":{"code":"DeploymentFailed","target":"/subscriptions/xxx-xxxx-xxxx-xxxx-xxx/providers/Microsoft.Resources/deployments/stage-4-secure","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":[{"code":"ResourceDeploymentFailure","target":"/subscriptions/56578465-7fdc-47fb-adc3-ce0dc14924ac/resourceGroups/cashikoi-viewdu-core-dev-aue/providers/Microsoft.Resources/deployments/koi-vdu-cor-dev-aue-script-sql-user-authorize","message":"The resource write operation failed to complete successfully, because it reached terminal provisioning state 'Failed'.","details":[{"code":"DeploymentFailed","target":"/subscriptions/56578465-7fdc-47fb-adc3-ce0dc14924ac/resourceGroups/cashikoi-viewdu-core-dev-aue/providers/Microsoft.Resources/deployments/koi-vdu-cor-dev-aue-script-sql-user-authorize","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":[{"code":"ResourceDeploymentFailure","target":"/subscriptions/56578465-7fdc-47fb-adc3-ce0dc14924ac/resourceGroups/cashikoi-viewdu-core-dev-aue/providers/Microsoft.Resources/deploymentScripts/koi-vdu-cor-dev-aue-exec-sql-user-authorize","message":"The resource write operation failed to complete successfully, because it reached terminal provisioning state 'failed'.","details":[{"code":"DeploymentScriptError","message":"The provided script failed with multiple errors. First error:\r\nSystem.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.\n\nFile name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\n at System.Reflection.Assembly.get_ExportedTypes()\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)\n at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(String fileName, String moduleBase, String prefix, SessionState ss, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 61\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/userscript.ps1: line 11\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321. Please refer to https://aka.ms/DeploymentScriptsTroubleshoot for more deployment script information.","details":[{"code":"DeploymentScriptError","message":"System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.\n\nFile name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\n at System.Reflection.Assembly.get_ExportedTypes()\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)\n at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(String fileName, String moduleBase, String prefix, SessionState ss, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 61\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/userscript.ps1: line 11\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.Management.Automation.PSInvalidOperationException: The module to process 'SqlServer.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest '/root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psd1' was not processed because no valid module was found in any module directory.\n ---> System.IO.FileNotFoundException: The module to process 'SqlServer.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest '/root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psd1' was not processed because no valid module was found in any module directory.\n --- End of inner exception stack trace ---\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hashtable localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable1 requiredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(ExternalScriptInfo scriptInfo, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable1 requiredModuleGuid, ImportModuleOptions& options)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingExtensions(PSModuleInfo parentModule, String moduleName, String fileBaseName, String extension, String moduleBase, String prefix, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingMultiVersionModuleBase(String moduleBase, ManifestProcessingFlags manifestProcessingFlags, ImportModuleOptions importModuleOptions, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(PSModuleInfo parentModule, Boolean found, IEnumerable1 modulePath, String name, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, PSModuleInfo& module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadUsingModulePath(Boolean found, IEnumerable1 modulePath, String name, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, PSModuleInfo& module)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/userscript.ps1: line 11\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.\n\nFile name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\n at System.Reflection.Assembly.get_ExportedTypes()\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)\n at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(String fileName, String moduleBase, String prefix, SessionState ss, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 61\r\nat <ScriptBlock>, /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/userscript.ps1: line 29\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.Management.Automation.RuntimeException: The variable '$binaryModule' cannot be retrieved because it has not been set.\n at System.Management.Automation.VariableOps.GetVariableValue(VariablePath variablePath, ExecutionContext executionContext, VariableExpressionAst varAst)\n at System.Management.Automation.Interpreter.FuncCallInstruction4.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 62\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat , /mnt/azscripts/azscriptinput/userscript.ps1: line 29\r\nat , : line 1\r\nat , /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.\n\nFile name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\n at System.Reflection.Assembly.get_ExportedTypes()\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)\n at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(String fileName, String moduleBase, String prefix, SessionState ss, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 61\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat , /mnt/azscripts/azscriptinput/userscript.ps1: line 29\r\nat , : line 1\r\nat , /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.IO.FileNotFoundException: Could not load file or assembly 'System.Management.Automation, Version=7.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.\n\nFile name: 'System.Management.Automation, Version=7.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\n at System.Reflection.Assembly.get_ExportedTypes()\n at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers)\n at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile)\n at System.Management.Automation.Runspaces.InitialSessionState.ImportCmdletsFromAssembly(Assembly assembly, PSModuleInfo module)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(PSModuleInfo parentModule, String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found, String shortModuleName, Boolean disableFormatUpdates)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadBinaryModule(String moduleName, String fileName, Assembly assemblyToLoad, String moduleBase, SessionState ss, ImportModuleOptions options, ManifestProcessingFlags manifestProcessingFlags, String prefix, Boolean loadTypes, Boolean loadFormats, Boolean& found)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(PSModuleInfo parentModule, String fileName, String moduleBase, String prefix, SessionState ss, Object privateData, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found, Boolean& moduleFileFound)\n at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModule(String fileName, String moduleBase, String prefix, SessionState ss, ImportModuleOptions& options, ManifestProcessingFlags manifestProcessingFlags, Boolean& found)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ImportModule_LocallyViaName_WithTelemetry(ImportModuleOptions importModuleOptions, String name)\n at Microsoft.PowerShell.Commands.ImportModuleCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 61\r\nat , /root/.local/share/powershell/Modules/SqlServer/22.4.5.1/SqlServer.psm1: line 58\r\nat , /mnt/azscripts/azscriptinput/userscript.ps1: line 29\r\nat , : line 1\r\nat , /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"},{"code":"DeploymentScriptError","message":"System.Management.Automation.CommandNotFoundException: The term 'Invoke-Sqlcmd' is not recognized as a name of a cmdlet, function, script file, or executable program.\nCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.\n at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)\n at System.Management.Automation.Interpreter.ActionCallInstruction2.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)\n at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)\n at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary2 functionsToDefine, List1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)\n at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(Boolean createLocalScope, Dictionary2 functionsToDefine, List1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)\n at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List1 variablesToDefine, Dictionary2 functionsToDefine, Object[] args)\n at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)\n at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord()\n at System.Management.Automation.Cmdlet.DoProcessRecord()\n at System.Management.Automation.CommandProcessor.ProcessRecord()\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/userscript.ps1: line 29\r\nat <ScriptBlock>, <No file>: line 1\r\nat <ScriptBlock>, /mnt/azscripts/azscriptinput/DeploymentScript.ps1: line 321"}]}]}]}]}]}}


Solution

  • There's been a recent change to PowerShell module SqlServer - if you don't have the specific version pinned and you don't have access to the .NET 8 dependencies you can see this issue.

    Try using a specific version:

    $requiredVersion = '22.3.0'
    if (-not (Get-Module -ListAvailable -Name SqlServer | Where-Object { $_.Version -eq $requiredVersion })) {
      Install-Module -Name SqlServer -RequiredVersion $requiredVersion -Force -Scope CurrentUser
    }
    Import-Module SqlServer -RequiredVersion $requiredVersion