ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
198 stars 153 forks source link

win_disk_facts fails on unassigned variable $pdisk #474

Closed BZanten closed 1 year ago

BZanten commented 1 year ago
SUMMARY

On a physical server with ejectable disks the win_disk_facts fails with error: Cannot validate argument on parameter 'PhysicalDisk'

The full traceback is:
Cannot validate argument on parameter 'PhysicalDisk'. The argument is null. Provide a valid value for the argument, and then try running the command again.\r
At line:89 char:52\r
+             $vdisk = Get-VirtualDisk -PhysicalDisk $pdisk -ErrorActio ...\r
+                                                    ~~~~~~\r
    + CategoryInfo          : InvalidData: (:) [Get-VirtualDisk], ParentContainsErrorRecordException\r
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-VirtualDisk\r
\r
ScriptStackTrace:\r
at <ScriptBlock>, <No file>: line 89\r
fatal: [<hostname obscured>]: FAILED! => {
    "changed": false,
    "msg": "Unhandled exception while executing module: Cannot validate argument on parameter 'PhysicalDisk'. The argument is null. Provide a valid value for the argument, and then try running the command again."
}
ISSUE TYPE
COMPONENT NAME

win_disk_facts

ANSIBLE VERSION
2.10
COLLECTION VERSION
1.11.1
CONFIGURATION

It is a HP synergy blade server. Somehow an extra (ejectable) harddisk is visible. Note that Get-Disk shows this disk, Get-PhysicalDisk does not

Get-Disk

Number Friendly Name       Serial Number    HealthStatus OperationalStatus Total Size Partition Style
------ -------------       -------------    ------------ ----------------- ---------- ---------------
0      HPE LOGICAL VOLUME  PEYJE0BRHCU09J   Healthy      Online                120 GB GPT            
1      HPE LOGICAL VOLUME  PEYJE0BRHCU09J   Healthy      Online                 50 GB GPT            
2      HPE LOGICAL VOLUME  PEYJE0BRHCU09J   Healthy      Online                 75 GB GPT            
3      Generic- SD/MMC CRW 2012062914345300 Healthy      No Media                 0 B RAW            
Get-PhysicalDisk

Number FriendlyName       SerialNumber   MediaType CanPool OperationalStatus HealthStatus Usage         Size
------ ------------       ------------   --------- ------- ----------------- ------------ -----         ----
0      HPE LOGICAL VOLUME PEYJE0BRHCU09J SSD       False   OK                Healthy      Auto-Select 120 GB
1      HPE LOGICAL VOLUME PEYJE0BRHCU09J SSD       False   OK                Healthy      Auto-Select  50 GB
2      HPE LOGICAL VOLUME PEYJE0BRHCU09J SSD       False   OK                Healthy      Auto-Select  75 GB

This then is also the cause of the bug: There is an outer loop on output of 'Get-Disk' which loops over disks 0-3 and tries to capture physicaldisk information using Get-PhysicalDisk, but disk #3 does not exist. So the last iteration in the loop causes the variable to be undefined.

OS / ENVIRONMENT

Windows Server 2019 on hardware

STEPS TO REPRODUCE

You'll need a machine / hardware with a removable harddisk that gives above PowerShell output. (1 disk more in output than in output from get-physicaldisk)

Bug:

To SOLVE the issue (tested):

EXPECTED RESULTS

Normal disk facts

ACTUAL RESULTS

Error as described above.

benjfield commented 1 year ago

I've also found this issue, I'll have a look and try submitting a pull request if I agree with BZAnten