Home > other >  i hvae a problem with my variable $PhysicalDisks as it shows null when it goes trought the output of
i hvae a problem with my variable $PhysicalDisks as it shows null when it goes trought the output of

Time:02-24

my cmdlet:

Get-PhysicalDisk -CanPool $True give me 2 disk of 5G either that i want to make a pool of them so the disks are there

$PhysicalDisks = (Get-PhysicalDisk -CanPool $True) | New-StoragePool -FriendlyName "My5Gpool" -StorageSubsystemFriendlyName "Windows Storage*" -PhysicalDisks $PhysicalDisks -ResiliencySettingNameDefault Mirror

my error:

New-StoragePool : Cannot validate argument on parameter 'PhysicalDisks'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:159
  ... FriendlyName "Windows Storage*" -PhysicalDisks $PhysicalDisks -Resili ...
                                                     ~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (:) [New-StoragePool], ParameterBindingValidationException
      FullyQualifiedErrorId : ParameterArgumentValidationError,New-StoragePool

CodePudding user response:

The problem is an attempt to use a variable before it's initialised. Let's take a look at the code (linebreaks added for readability)

$PhysicalDisks = (Get-PhysicalDisk -CanPool $True) | `
   New-StoragePool -FriendlyName "My5Gpool" `
   -StorageSubsystemFriendlyName "Windows Storage*" `
   -PhysicalDisks $PhysicalDisks

So what happens here is that Get-PhysicalDisk's output is piped to New-StoragePool. Results that come after that pipe are assigned to $PhysicalDisks. So New-StoragePool rightly complains about its parameter being null - it is.

Fix: always use set-strictmode -version 'latest' to catch uninitialised variables. (I have never understood why it's off by default. Brain-dead design there.) Then break the code on two statements like so,

$PhysicalDisks = Get-PhysicalDisk -CanPool $True
New-StoragePool -FriendlyName "My5Gpool" `
   -StorageSubsystemFriendlyName "Windows Storage*" `
   -PhysicalDisks $PhysicalDisks

CodePudding user response:

well i did use the variable initialisation in one line and the cmdlet new-storagepool in another line and it does work sure,but my question is why isn't being initialised in the first part because i need the comdlet to have a pipeline that was a question in a test that i had to do it like that creating a storagepool with a pipelined cmdlet

  • Related