Reputation: 3695
I'm working with the below type in powershell:
Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91
I create objects, pass them around, return them from cmdlets etc. Here's an example:
Import-Module SQLPS
function New-SQLSMORelocateFile {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[string] $logicalName,
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[string] $physicalName
)
New-Object "Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" `
-ArgumentList $logicalName, $physicalName
}
I want to pass this value into another powershell cmdlet, as follows:
function Restore-SQLDatabaseFromDisk {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
[Microsoft.SqlServer.Management.Smo.RelocateFile] $overrideDataFile
)
# Do stuff
}
But when I do, I get the following helpful error:
PSInvalidCastException: Cannot convert the "Microsoft.SqlServer.Management.Smo.RelocateFile" value of type "Microsoft.SqlServer.Management.Smo.Re locateFile" to type "Microsoft.SqlServer.Management.Smo.RelocateFile". ArgumentTransformationMetadataException: Cannot convert the "Microsoft.SqlServer.Management.Smo.RelocateFile" value of type "Microsoft.SqlServer. Management.Smo.RelocateFile" to type "Microsoft.SqlServer.Management.Smo.RelocateFile". ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'overrideDataFile'. Cannot convert the "Micr osoft.SqlServer.Management.Smo.RelocateFile" value of type "Microsoft.SqlServer.Management.Smo.RelocateFile" to type "Microsoft.SqlServer.Management.Smo.RelocateFile".
I've seen this before; it's because the loaded "RelocateFile" version mismatches the one returned from my New-SQLSMORelocateFile() (which is required the SQLPS Restore-SqlDatabase
cmdlet).
Whenever I use the "RelocateFile" type in my module, it always needs to be the "Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" type, rather than the default loaded by SQLPS.
Is there a Powershell equivalent to C#'s "using" directive to override the behaviour of a type in a module, as demonstrated below?:
https://msdn.microsoft.com/en-us/library/sf0df423.aspx
As in the 3rd example: using Project = PC.MyCompany.Project;
Alternatively, is there a way I can define a specific version of a parameter type, like this:
function Restore-SQLDatabaseFromDisk {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
["Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"] $overrideDataFile
)
# Do stuff
}
The above obviously produces a syntax error, but I've not been able to come up with or find a way to do it.
Upvotes: 0
Views: 237
Reputation: 174690
The type literal syntax allows fully qualified type names, just do:
param(
[Microsoft.SqlServer.Management.Smo.RelocateFile, Microsoft.SqlServer.SmoExtended, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91]$MyParameter
)
Upvotes: 1