Trying to take an object and filter down to a value as a string. And save that string to variable and then use that variable in another command as a value for a flag.
So this command will get the PNPDevice InstanceID
$x = (Get-PnpDevice -PresentOnly -Class 'Net' | Where-Object {$_.FriendlyName -EQ 'Intel(R) Ethernet Connection I217-LM'} | Select-Object -ExpandProperty InstanceId | Format-Table -AutoSize | out-string )
Here I am checking the variable X and its content and type since the next command Disable-PNPDevice and the flag instanceID has to be a string
PS C:\Temp> $x.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\Temp> echo $x
PCI\VEN_1234&DEV_543A&SUBSYS_32A39857&REV_04\3&1234839&0&C8
When I try to use $x for -InstanceId i get this error
PS C:\Temp> Disable-PnpDevice -InstanceId $x
Disable-PnpDevice : Invalid query
At line:1 char:1
+ Disable-PnpDevice -InstanceId $x
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Win32_PnPEntity:ROOT\cimv2\Win32_PnPEntity) [Disable-PnpDevice], CimException
+ FullyQualifiedErrorId : HRESULT 0x80041017,Disable-PnpDevice
However if I manually create the variable with a string it works fine
PS C:\Temp> $y = "PCI\VEN_1234&DEV_543A&SUBSYS_32A39857&REV_04\3&1234839&0&C8"
PS C:\Temp> Disable-PnpDevice -InstanceId $y
PS C:\Temp> $y.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
Thank you.