Get-wmiObject Windows disk info powershell error

This topic contains 2 replies, has 2 voices, and was last updated by  vcloudguy 2 years, 1 month ago.

  • Author
  • #29286


    I am trying to run a powershell script to map Windows disk volumes to EC2 Volumes. My script is breaking on a Windows 2008 machine but works fine on a windows 2012 machine. This is the original script

    On a Windows 2008 machine if I run the below snippet I get an error message. it works fine on a Windows 2012 machine. Any ideas?

    You must provide a value expression on the right-hand side of the '-' operator.
    At line:9 char:98
    + $Disk = Get-WmiObject -Class Win32_LogicalDiskToPartition | Where-Object {$_.Antecedent – <<<< in $D2P.Depend ent} | %{ + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedValueExpression ************************************************************************************************** Powershell code: Get-WmiObject -Class Win32_DiskDrive | % { $Drive = $_ # Find the partitions for this drive Get-WmiObject -Class Win32_DiskDriveToDiskPartition | Where-Object {$_.Antecedent -eq $Drive.Path.Path} | %{ $D2P = $_ # Get details about each partition $Partition = Get-WmiObject -Class Win32_DiskPartition | Where-Object {$_.Path.Path -eq $D2P.Dependent} # Find the drive that this partition is linked to $Disk = Get-WmiObject -Class Win32_LogicalDiskToPartition | Where-Object {$_.Antecedent -in $D2P.Dependent} | %{ $L2P = $_ #Get the drive letter for this partition, if there is one Get-WmiObject -Class Win32_LogicalDisk | Where-Object {$_.Path.Path -in $L2P.Dependent} } $BlockDeviceMapping = $BlockDeviceMappings | Where-Object {$_.DeviceName -eq $Map[$Drive.SCSITargetId.ToString()]}

  • #29293

    Don Jones

    It would be a little easier to read this if you took a moment to format the code, as indicated in the instructions above the text box. As-is, it's all running together and I'm having trouble interpreting it.

    However, it appears that you're using the -in operator, which didn't exist in older versions of PowerShell. You might try re-writing your comparison logic to use -contains, which goes back to v1.0.

  • #29296


    Thanks for your valuable insight Don. You were right the -in operator was causing the issue. When I changed that to -contains it worked perfect.

You must be logged in to reply to this topic.