Type Conversion

Welcome Forums General PowerShell Q&A Type Conversion

Viewing 4 reply threads
  • Author
    Posts
    • #194183
      Participant
      Topics: 4
      Replies: 2
      Points: 33
      Rank: Member

      Hi ,

      I am facing the type conversion error in my script

      $DiskInfo =get-WmiObject win32_logicaldisk -ComputerName itsusrawsp06501|Where-Object DeviceId -In(“C:”) | Select-object DeviceId,@{n=”Size”;e={[math]::Round($_.Size/1GB,2)}},@{n=”FreeSpace”;e={[math]::Round($_.FreeSpace/1GB,2)}}

      $CDriveSize= $drive.Size
      $CFreeSpace=$drive.FreeSpace
      $C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) * 100)
      $C_FreeSpacePercent

      Error : Cannot convert value “C” to type “System.Int32”. Error: “Input string was not in a correct
      format.”
      At G:\SqlScript\powershell\Untitled5.ps1:6 char:9
      + $C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDrive) * 100)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvalidCastFromStringToInteger

      can anyone help me sort the issues  am facing

       

    • #194210
      Participant
      Topics: 0
      Replies: 8
      Points: 104
      Rank: Participant

      I recommend formatting your code by enclosing it using pre /pre. I wasn’t able to replicate your error, however, you are referencing your variables incorrectly. This is the adjusted version:

      $DiskInfo =get-WmiObject win32_logicaldisk -ComputerName localhost|Where-Object DeviceId -In(“C:”) | Select-object DeviceId,@{n=”Size”;e={[math]::Round($_.Size/1GB,2)}},@{n=”FreeSpace”;e={[math]::Round($_.FreeSpace/1GB,2)}}
      
      [int]$CDriveSize= $diskinfo.Size
      [int]$CFreeSpace= $diskinfo.FreeSpace
      $C_FreeSpacePercent=[Math]::Round(($CFreespace / $CDriveSize) * 100)
      $C_FreeSpacePercent
      
      

       

      Try it again and see if you get the same error.

    • #194228
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      Use the WMI filter to filter left and only return what you want from Win32_LogicalDisk:

      $DiskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceId = "C:"' | 
                  Select-object DeviceId,
                                @{Name="Size";Expression={[math]::Round($_.Size/1GB,2)}},
                                @{Name="FreeSpace";Expression={[math]::Round($_.FreeSpace/1GB,2)}}
      
      $DiskInfo
      
    • #194240
      Senior Moderator
      Topics: 5
      Replies: 133
      Points: 706
      Helping Hand
      Rank: Major Contributor

      As BenT said, it looks like you aren’t referencing your variables correctly. Specifically, in the division on line 6, $CDrive probably should be $CDriveSize.

      Here’s an adjusted version that doesn’t change your process but makes it a little cleaner and more flexible:

      ## input variables
      $drive = 'C:'
      $computer = "localhost"
      
      ## get drive information
      $AllDrives = Get-WmiObject win32_logicaldisk -ComputerName "$computer"
      $SelectedDrive = $AllDrives | Where-Object DeviceId -In("$drive")
      $SelectedDriveData = $SelectedDrive | Select-object DeviceId,@{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
      
      ## calculate free space percentage
      $SelectedDriveSize = $SelectedDriveData.Size
      $SelectedDriveFreeSpace = $SelectedDriveData.FreeSpace
      $SelectedDriveFreePercent = [Math]::Round(($SelectedDriveFreeSpace / $SelectedDriveSize) * 100)
      
      ## show the result
      $SelectedDriveFreePercent
    • #194258
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      You can do all of these calculations within calculated expressions without breaking things into separate variables. Additionally, you can use string format to make it a percentage:

      $DiskInfo = Get-CimInstance -ClassName Win32_LogicalDisk -Filter 'DeviceId = "C:"' | 
                  Select-object DeviceId,
                                @{Name="Size";Expression={[math]::Round($_.Size/1GB,2)}},
                                @{Name="FreeSpace";Expression={[math]::Round($_.FreeSpace/1GB,2)}},
                                @{Name="UsedPct";Expression={($_.FreeSpace / $_.Size).ToString('P')}},
                                @{Name="UsedPctv2";Expression={'{0:P}' -f ($_.FreeSpace / $_.Size)}}
      
      
      
      $DiskInfo
      

      Output:

      DeviceId  : C:
      Size      : 475.69
      FreeSpace : 278.64
      UsedPct   : 58.58%
      UsedPctv2 : 58.58%
      
Viewing 4 reply threads
  • The topic ‘Type Conversion’ is closed to new replies.