Powershell script error on hangfire install

30 Views Asked by At

I am trying to install hangfire db on my azure managed instance using powershell script through my devops pipeline.

Here is the script:

param(
    [Parameter(Mandatory=$true, Position=1)][string]$SchemaName,
    [Parameter(Mandatory=$true, Position=2)][string]$ServerName,
    [Parameter(Mandatory=$true, Position=3)][string]$DatabaseName    
)

$repo = Get-PSRepository PSGallery -ErrorAction SilentlyContinue
if($repo -eq $null)
{
    Register-PSRepository -Default
    Register-PSRepository -Name PSGallery -InstallationPolicy Trusted -SourceLocation "https://www.powershellgallery.com/api/v2/" -Verbose
}

$module = Get-InstalledModule -Name SQLServer -ErrorAction SilentlyContinue
Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force

if($module -eq $null)
{
    Install-Module SQLServer -Scope CurrentUser -Force
}
else
{
    Import-Module SqlServer -Force
}

Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=17.100.18.0, Culture=neutral, PublicKeyToken=89845dc8080cc91"

$Server = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
if($Server.Databases -eq $null)
{
    Write-Output " Cannot retrieve databases on $ServerName."
    break
}
$db = $Server.Databases[$DatabaseName]
if($db -eq $null)
{
    Write-Host "Database does not exists yet, creating.."
    $db = New-Object Microsoft.SqlServer.Management.Smo.Database($Server, $DatabaseName)
    $db.Create()
    Write-Host "Database created."
}
else
{
    Write-Host "Database already exists, skipping database creation."
}

Yaml task:

parameters:
//all required params

jobs:
- deployment: Install_Hanfire
  pool:
    name: Default
  environment:
    name: 
    resourceType: VirtualMachine
    tags: 
  strategy:
    runOnce:
      deploy:
        steps:
        - download: current
          artifact: drop
          
        - task: PowerShell@2
          displayName: 'Install hangfire'
          inputs:
            targetType: filePath
            filePath: '$(Pipeline.Workspace)/drop/xxx/myscript.ps1'
            arguments: '-SchemaName "${{ parameters.schemaName }}" -ServerName "${{ parameters.serverName }}" -DatabaseName "${{ parameters.databaseName }}"'

But I am getting the error while this executes:

Add-Type : Could not load file or assembly 'Microsoft.SqlServer.Smo, Version=17.100.18.0, Culture=neutral, PublicKeyToken=89845dc8080cc91' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) At C:\azagent\A1_work\3\drop\xxx\myscript.ps1:26 char:1

  • Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=17.100.18.0,

I tried running the script in powershell ise on the vm where we have the agent and there I see an error on the below check

$Server.Databases -eq $null

It says failing to connect to managed instance but if I run the below command, it returns true

Test-NetConnection -ComputerName mymanagedinstance -Port 1433
0

There are 0 best solutions below