Home > Software engineering >  Powershell Strings from Objects
Powershell Strings from Objects

Time:09-21

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.

CodePudding user response:

Omit | Format-Table -AutoSize | out-string from your command, which is not only redundant in your case (the .InstanceId property already is a string), but causes the instance ID string to have a trailing newline, which is likely the cause of your problem.

Generally:

  • Format-* cmdlets emit output objects whose sole purpose is to provide formatting instructions to PowerShell's for-display output-formatting system. In short: only ever use Format-* cmdlets to format data for display, never for subsequent programmatic processing - see this answer for more information.

  • As an aside: That Out-String blindly appends a trailing newline to its output string is both unexpected and inconvenient: see GitHub issue #14444.

    • Note: With a single input object (such as in your case), -NoNewLine can be used to suppress the trailing newline (but Out-String is never needed for an input object that already is a string).
    • The problem is that with multiple input objects, -NoNewLine also suppresses newlines between their representations; e.g., 'one', 2 | Out-String -NoNewLine yields verbatim one2
  • Related