I have a Powershell script working perfectly fine when executed from Visual Studio Code powershell terminal.
When I try to run the same script from c# code, or trying to Run as PowerShell from Windows explorer, I get the following error:
New-Object : Cannot find type [Microsoft.SqlServer.Management.Common.ServerConnection]: verify that the assembly
containing this type is loaded.
At line:32 char:18
+ return @(& $origNewObject @psBoundParameters)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
[...]
Script code is as follow:
try {
Add-Type -AssemblyName "Microsoft.SqlServer.Smo, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"
Start-Sleep -Seconds 5
}
catch {
Write-Output "Failed to load the SMO assembly. Error: $($_.Exception.Message)"
return
}
$SMOServerConn = new-object ("Microsoft.SqlServer.Management.Common.ServerConnection")
$SMOServerConn.ServerInstance="xxx.xxx.xxx.xxx"
$SMOServerConn.LoginSecure=$false
$SMOServerConn.Login='xxxxxx'
$SMOServerConn.Password='xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$SMOserver = new-object ("Microsoft.SqlServer.Management.Smo.Scripter") #-argumentlist $server
$srv = new-Object Microsoft.SqlServer.Management.Smo.Server($SMOServerConn)
$srv.ConnectionContext.ApplicationName="MySQLAuthenticationPowerShell"
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item('xxxxx')
$maxAttempts = 10
$attempt = 0
# Loop until the connection is established or the maximum number of attempts is reached
while ($db.ConnectionContext.IsOpen -eq $false -and $attempt -lt $maxAttempts) {
$attempt++
Write-Output "Attempt $($attempt): Connecting to database..."
# Wait for a short duration before attempting the next connection check
Start-Sleep -Seconds 1
}
$scripter = new-object ("$SMOserver") $srv
$line="[cccauth].[spGetTrxReconSummary]"
$line = $line.replace('[','').replace(']','')
$splitline =$line.Split(".")
$Objects = $db.storedprocedures[$splitline[1], $splitline[0]]
Write-Host "Extracting stored procedure $($line)"
Write-Host "--------------------------------"
$sp_extracted+=$Scripter.Script($Objects)
$sp_extracted=$sp_extracted.replace("SET QUOTED_IDENTIFIER ON", "SET QUOTED_IDENTIFIER ON `r`nGO`r`n")
Write-Host $sp_extracted -ForegroundColor Green
Write-Host "Scripting DB Finished... Press any key..."
[void][System.Console]::ReadKey($true)
If I implement the logic to connect to a second server/database on the script, then this second connection is working.
Does anyone know what is wrong here?
Thank you in advance.