3

i work with hyperv virtual machine and powershell and find really weird problem

when i get the type of an object and then check for it (ex: String):

PS > "str".gettype().fullname
System.String
PS > "str".gettype() -eq [System.String]
True 

but when i get the type of a vm object:

PS > (get-vm)[0].gettype().fullname
Microsoft.HyperV.PowerShell.VirtualMachine

and then i check the vm type:

PS >(get-vm)[0].GetType() -eq [Microsoft.HyperV.PowerShell.VirtualMachine]
False

does someone understand this weird comportement of powershell object type ??

there is difference between .net and PSObject ?

tks,

G.S
  • 392
  • 2
  • 13
  • 2
    No, PowerShell is .NET (although it does occasionally wrap objects in funky ways) -- but types with equal names still aren't necessarily the same type. Check the `AssemblyQualifiedName` of each type. (On my machine, it's `Microsoft.HyperV.PowerShell.VirtualMachine, Microsoft.HyperV.PowerShell.Objects, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35` for both, and the `-eq` gives `$true`.) – Jeroen Mostert May 27 '19 at 11:21
  • 3
    There is a [difference between System.Type and System.RuntimeType](https://stackoverflow.com/questions/5737840/whats-the-difference-between-system-type-and-system-runtimetype-in-c). The way you test your types can IMO be done better by comparing the `.FullName` property against the string `'Microsoft.HyperV.PowerShell.VirtualMachine'` OR you can use the `-is` operator like this: `(get-vm)[0] -is [Microsoft.HyperV.PowerShell.VirtualMachine]` – Theo May 27 '19 at 11:29
  • yes of course but weird wrapping lead to ask the question though, dont really understand the term assembly refer to, but on my machine you are right `(get-vm)[0].GetType().AssemblyQualifiedName` give `Microsoft.HyperV.PowerShell.VirtualMachine, Microsoft.HyperV.PowerShell.Objects, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35` while `[Microsoft.HyperV.PowerShell.VirtualMachine].AssemblyQualifiedName` give me `Microsoft.HyperV.PowerShell.VirtualMachine, Microsoft.HyperV.PowerShell, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35` – G.S May 27 '19 at 11:30
  • Couldn't agree more! – Theo May 27 '19 at 11:31
  • @JeroenMostert tks you are right but my problem came from Parameter Argument Binding, now the f*** is how to precise a type with his specific assembly for parameter type (for writing cmdlet) – G.S May 27 '19 at 11:41
  • 1
    Well, using the type's full, qualified name is actually allowed, but this is clumsy, and might break with new versions, so it seems to me like you probably want to look into why two assemblies are being loaded (`Microsoft.HyperV.PowerShell.Objects` and `Microsoft.HyperV.PowerShell`). The latter isn't loaded by default on my machine. Maybe there's a module or other code that imports it explicitly? I don't know how PowerShell's lookup order works, and I don't think explicitly (re-)importing the assembly will work if it's already loaded. – Jeroen Mostert May 27 '19 at 11:54
  • @JeroenMostert yess youre totally right.. if i launch a new shell and start with `get-vm`the Hyper-V module and `Microsoft.HyperV.PowerShell.Objects` assembly are loaded so the `[Microsoft.HyperV.PowerShell.VirtualMachine]` type take this assembly ... but in another shell if i start to import the Hyper-V module, it load the `Microsoft.HyperV.PowerShell` assembly for the `[Microsoft.HyperV.PowerShell.VirtualMachine]` type, and then enter in conflict with the `get-vm` type assembly... realyy weird – G.S May 27 '19 at 12:23
  • "my problem came from Parameter Argument Binding" - can you post the code you're actually having problems with? – Mathias R. Jessen May 27 '19 at 12:49
  • @mathias-r-jessen parameter type in function `function do{ param([type]$var}ˋ – G.S May 27 '19 at 13:01

0 Answers0