none
new-partition -disknumber variable problem RRS feed

  • Question

  • Hi,

    When I use the New-Partition -DiskNumber $disknumber

    i get an error which says the argument is invalid, however if i change it to be New-Partition -DiskNumber 3

    the command succeeds. the variable is causing the error. however i have tried to explicitly set the variable to be int32

    [int]$disknumber = Get-Disk | Where-Object {$_.BusType -eq "iSCSI" -and $_.Size -lt "5GB"} | Select -ExpandProperty Number

    but this doesnt allow the command to succeed. I have also tried New-Partition -DiskNumber $disknumber.ToInt32() but it doesnt like this either.

    can someone try and get a disk partitioned by using a variable instead of a hard coded number because I can't seem to get this to work?

    Initialize-Disk -Number $disknumber

    seems to work fine. my full code is as follows

    [int]$disknumber = Get-Disk | Where-Object {$_.BusType -eq "iSCSI" -and $_.Size -lt "5GB"} | Select -ExpandProperty Number
    
            
            Initialize-Disk –Number $disknumber –PartitionStyle MBR –PassThru
            New-Partition -DiskNumber $disknumber -AssignDriveLetter –UseMaximumSize -DriveLetter Q
            Format-Volume -DriveLetter Q -NewFileSystemLabel "NewDisk" -Confirm:$false

    any ideas much appreciated.

    Wednesday, August 6, 2014 8:12 AM

Answers

  • Try it like this to avoid var issues.  You should not be casting to int. 

    You also cannot use both AssignDriveLetter and DriveLetter.

    if($disk=Get-Disk | Where-Object{$_.BusType -eq 'iSCSI' -and $_.Size -lt 5GB}){
       if($disk.Count -eq 1){
            Write-Host "Disk is number $($disk.Number)" -fore green
            Initialize-Disk –Number $disk.Number –PartitionStyle MBR –PassThru
            New-Partition -DiskNumber $disk.Number –UseMaximumSize -DriveLetter Q
       }else{
            Write-Host 'Too many disks' -fore red
       }
    }else{
       Write-Host 'No disks found' -fore red
    }


    ¯\_(ツ)_/¯

    • Marked as answer by Milkientia Sunday, August 17, 2014 5:28 PM
    Wednesday, August 6, 2014 9:58 AM

All replies

  • What is in $disknumber?  I suspect it is null or a collection.

    [int]$disknumber = Get-Disk | Where-Object {$_.BusType -eq "iSCSI" -and $_.Size -lt "5GB"} | Select -ExpandProperty Number
    $disknumber.GetType()


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 8:21 AM
  • Hi

    the variable gets a number (in this case 3). when i get the type it has shown the following

    IsPublic IsSerial Name                                     BaseType                                                                                                                            
    -------- -------- ----                                     --------                                                                                                                            
    True     True      Int32                                    System.ValueType 

    the error i get when i run the command is

    New-Partition : Invalid Parameter
    At C:\script\Untitled1.ps1:6 char:9
    +         New-Partition -DiskNumber $disknumber -AssignDriveLetter -UseMaximumSize ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (StorageWMI:ROOT/Microsoft/Windows/Storage/MSFT_Disk) [New-Partition], CimException
        + FullyQualifiedErrorId : StorageWMI 5,New-Partition

    Wednesday, August 6, 2014 9:34 AM
  • Try it like this to avoid var issues.  You should not be casting to int. 

    You also cannot use both AssignDriveLetter and DriveLetter.

    if($disk=Get-Disk | Where-Object{$_.BusType -eq 'iSCSI' -and $_.Size -lt 5GB}){
       if($disk.Count -eq 1){
            Write-Host "Disk is number $($disk.Number)" -fore green
            Initialize-Disk –Number $disk.Number –PartitionStyle MBR –PassThru
            New-Partition -DiskNumber $disk.Number –UseMaximumSize -DriveLetter Q
       }else{
            Write-Host 'Too many disks' -fore red
       }
    }else{
       Write-Host 'No disks found' -fore red
    }


    ¯\_(ツ)_/¯

    • Marked as answer by Milkientia Sunday, August 17, 2014 5:28 PM
    Wednesday, August 6, 2014 9:58 AM
  • Do not put quotes around 5Gb as it will no longer be a number.

    Try this:

    5Gb
    "5Gb"

    Notice the difference.


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 10:00 AM
  • Absolutely brilliant, it all works perfect. very strange though, i removed the -AssignDriveLetter and suddenly using my variable worked, however if i kept this here it would not work but it would work if i used a manually typed drive number in the code line - weird.

    the $disknumber.Number didnt work at all, i just got more errors with that.

    I have tried both 5GB and "5GB" in my query and both appear to work perfectly fine, perhaps powershell converts this correctly, i assumed to put it as a string because it contains the letters GB, powershell seems to figure it out for itself.

    thanks for your help, that was really bugging me and not obvious what the cause was.

    much appreciated.

    Wednesday, August 6, 2014 10:36 AM
  • You are making a lot of very bad assumptions and are misreading the errors and how they are caused.  I recommend spending more time incrementally testing.

    Do not get into the habit of putting quotes around everything you do not understand even if you get away with it occasionally.  It will cause issues in many places. 

    Never automatically trust a variable to be non-null. Test the vaariablebefore accepting it.

    Changing your version of $disknumber to $disknumber.Number is meaningless and shows a lack of understanding of objects in PowerShell. You should remedy this.


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 10:45 AM
  • You are making a lot of very bad assumptions and are misreading the errors and how they are caused.  I recommend spending more time incrementally testing.

    Do not get into the habit of putting quotes around everything you do not understand even if you get away with it occasionally.  It will cause issues in many places. 

    Never automatically trust a variable to be non-null. Test the vaariablebefore accepting it.

    Changing your version of $disknumber to $disknumber.Number is meaningless and shows a lack of understanding of objects in PowerShell. You should remedy this.


    ¯\_(ツ)_/¯

    i read the error message and tested to see what was causing it, when i used the variable the error message was produced, when i didn't the error message was not produced. the error message did not indicate it was a conflict with using two parameters and why this would work when not using a variable but didnt work when omitting it is very odd behaviour. the error was not descriptive enough

    how do you know it should be 5GB and not "5GB"? where is the documentation to tell me what this should be? when I see the tool-tip for this in PSISE it simply shows uint64 but this is the same as BusType, yet a Microsoft Blog post was the place i got the "ISCSI" but from, admittedly i guessed that Size would be "5GB" purely because it contains letters not solely numbers

    although in your example you use $disk instead of $disknumber as the variable in your replacement code, it was in fact yourself that advised to try changing it to use $disknumber.Number (or in your example $disk.Number) you're right this is meaningless but it is what you suggested.....my approach was to use the ToInt32() method but as discovered now it was not the variable at fault, it was the fact of passing both -AssignDriveLetter and -DriveLetter at the same time which caused the non-obvious error message

    Wednesday, August 6, 2014 11:49 AM
  • Where di dI ever suggest using $disknumber.number?  I don't see that anywhere in this thread.

    This is partly what I am referring to about your analysis.

    Don't worry about it.  They were suggestions which you are free to ignore.


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 1:07 PM
  • Try it like this to avoid var issues.  You should not be casting to int. 

    You also cannot use both AssignDriveLetter and DriveLetter.

    if($disk=Get-Disk | Where-Object{$_.BusType -eq 'iSCSI' -and $_.Size -lt 5GB}){
       if($disk.Count -eq 1){
            Write-Host "Disk is number $($disk.Number)" -fore green
            Initialize-Disk –Number $disk.Number –PartitionStyle MBR –PassThru
            New-Partition -DiskNumber $disk.Number –UseMaximumSize -DriveLetter Q
       }else{
            Write-Host 'Too many disks' -fore red
       }
    }else{
       Write-Host 'No disks found' -fore red
    }


    ¯\_(ツ)_/¯

    This is your reply right? you clearly write $disk.Number

    my variable is $disknumber you have just replaced my original variable for $disk instead of $disknumber but you have then added a method .Number at the end of it which is invalid.

    nothing wrong with my analysis. I appreciate your help but you can lose the arrogance. thanks

    Wednesday, August 6, 2014 3:24 PM
  • $disk.Number NOT $disknumber.Numer as you posted.

    QUOTE YOU:

    "although in your example you use $disk instead of $disknumber as the variable in your replacement code, it was in fact yourself that advised to try changing it to use $disknumber.Number (or in your example $disk.Number) you're right this is meaningless but it is what you suggested.....my approach was to use the ToInt32() method but as discovered now it was not the variable at fault, it was the fact of passing both -AssignDriveLetter and -DriveLetter at the same time which caused the non-obvious error message"

    You cannot use [int]$disknumber like that. That is what I mean about understanding the object system. An [int] has no property called "Number".  $disk=GetDisk has a property called "Number".

    Objects versus strings and numbers.


    ¯\_(ツ)_/¯

    Wednesday, August 6, 2014 3:50 PM