DSCResources/DSC_Disk/DSC_Disk.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules' # Import the Storage Common Module. Import-Module -Name (Join-Path -Path $modulePath ` -ChildPath (Join-Path -Path 'StorageDsc.Common' ` -ChildPath 'StorageDsc.Common.psm1')) Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common') # Import Localization Strings. $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' <# .SYNOPSIS Returns the current state of the Disk and Partition. .PARAMETER DriveLetter Specifies the preferred letter to assign to the disk volume. .PARAMETER DiskId Specifies the disk identifier for the disk to modify. .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. .PARAMETER PartitionStyle Specifies the partition style of the disk. Defaults to GPT. This parameter is not used in Get-TargetResource. .PARAMETER Size Specifies the size of new volume (use all available space on disk if not provided). This parameter is not used in Get-TargetResource. .PARAMETER FSLabel Specifies the volume label to assign to the volume. This parameter is not used in Get-TargetResource. .PARAMETER AllocationUnitSize Specifies the allocation unit size to use when formatting the volume. This parameter is not used in Get-TargetResource. .PARAMETER FSFormat Specifies the file system format of the new volume. This parameter is not used in Get-TargetResource. .PARAMETER AllowDestructive Specifies if potentially destructive operations may occur. This parameter is not used in Get-TargetResource. .PARAMETER ClearDisk Specifies if the disks partition schema should be removed entirely, even if data and OEM partitions are present. Only possible with AllowDestructive enabled. This parameter is not used in Get-TargetResource. #> function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $DriveLetter, [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter()] [ValidateSet('Number', 'UniqueId', 'Guid', 'Location')] [System.String] $DiskIdType = 'Number', [Parameter()] [ValidateSet('GPT', 'MBR')] [System.String] $PartitionStyle = 'GPT', [Parameter()] [System.UInt64] $Size, [Parameter()] [System.String] $FSLabel, [Parameter()] [System.UInt32] $AllocationUnitSize, [Parameter()] [ValidateSet('NTFS', 'ReFS')] [System.String] $FSFormat = 'NTFS', [Parameter()] [System.Boolean] $AllowDestructive, [Parameter()] [System.Boolean] $ClearDisk ) Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.GettingDiskMessage -f $DiskIdType, $DiskId, $DriveLetter) ) -join '' ) # Validate the DriveLetter parameter $DriveLetter = Assert-DriveLetterValid -DriveLetter $DriveLetter # Get the Disk using the identifiers supplied $disk = Get-DiskByIdentifier ` -DiskId $DiskId ` -DiskIdType $DiskIdType $partition = Get-Partition ` -DriveLetter $DriveLetter ` -ErrorAction SilentlyContinue | Select-Object -First 1 $volume = Get-Volume ` -DriveLetter $DriveLetter ` -ErrorAction SilentlyContinue $blockSize = (Get-CimInstance ` -Query "SELECT BlockSize from Win32_Volume WHERE DriveLetter = '$($DriveLetter):'" ` -ErrorAction SilentlyContinue).BlockSize return @{ DiskId = $DiskId DiskIdType = $DiskIdType DriveLetter = $partition.DriveLetter PartitionStyle = $disk.PartitionStyle Size = $partition.Size FSLabel = $volume.FileSystemLabel AllocationUnitSize = $blockSize FSFormat = $volume.FileSystem } } # Get-TargetResource <# .SYNOPSIS Initializes the Disk and Partition and assigns the drive letter. .PARAMETER DriveLetter Specifies the preferred letter to assign to the disk volume. .PARAMETER DiskId Specifies the disk identifier for the disk to modify. .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. .PARAMETER PartitionStyle Specifies the partition style of the disk. Defaults to GPT. .PARAMETER Size Specifies the size of new volume. Leave empty to use the remaining free space. .PARAMETER FSLabel Specifies the volume label to assign to the volume. .PARAMETER AllocationUnitSize Specifies the allocation unit size to use when formatting the volume. .PARAMETER FSFormat Specifies the file system format of the new volume. .PARAMETER AllowDestructive Specifies if potentially destructive operations may occur. .PARAMETER ClearDisk Specifies if the disks partition schema should be removed entirely, even if data and OEM partitions are present. Only possible with AllowDestructive enabled. #> function Set-TargetResource { # Should process is called in a helper functions but not directly in Set-TargetResource [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')] [CmdletBinding(SupportsShouldProcess = $true)] param ( [Parameter(Mandatory = $true)] [System.String] $DriveLetter, [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter()] [ValidateSet('Number', 'UniqueId', 'Guid', 'Location')] [System.String] $DiskIdType = 'Number', [Parameter()] [ValidateSet('GPT', 'MBR')] [System.String] $PartitionStyle = 'GPT', [Parameter()] [System.UInt64] $Size, [Parameter()] [System.String] $FSLabel, [Parameter()] [System.UInt32] $AllocationUnitSize, [Parameter()] [ValidateSet('NTFS', 'ReFS')] [System.String] $FSFormat = 'NTFS', [Parameter()] [System.Boolean] $AllowDestructive, [Parameter()] [System.Boolean] $ClearDisk ) Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SettingDiskMessage -f $DiskIdType, $DiskId, $DriveLetter) ) -join '' ) # Validate the DriveLetter parameter $DriveLetter = Assert-DriveLetterValid -DriveLetter $DriveLetter # Get the Disk using the identifiers supplied $disk = Get-DiskByIdentifier ` -DiskId $DiskId ` -DiskIdType $DiskIdType if ($disk.IsOffline) { # Disk is offline, so bring it online Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SetDiskOnlineMessage -f $DiskIdType, $DiskId) ) -join '' ) $disk | Set-Disk -IsOffline $false } # if if ($disk.IsReadOnly) { # Disk is read-only, so make it read/write Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SetDiskReadWriteMessage -f $DiskIdType, $DiskId) ) -join '' ) $disk | Set-Disk -IsReadOnly $false } # if Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.CheckingDiskPartitionStyleMessage -f $DiskIdType, $DiskId) ) -join '' ) if ($AllowDestructive -and $ClearDisk -and $disk.PartitionStyle -ne 'RAW') { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.ClearingDiskMessage -f $DiskIdType, $DiskId) ) -join '' ) $disk | Clear-Disk -RemoveData -RemoveOEM -Confirm:$true # Requery the disk $disk = Get-DiskByIdentifier ` -DiskId $DiskId ` -DiskIdType $DiskIdType } if ($disk.PartitionStyle -eq 'RAW') { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.InitializingDiskMessage -f $DiskIdType, $DiskId, $PartitionStyle) ) -join '' ) $disk | Initialize-Disk -PartitionStyle $PartitionStyle } else { if ($disk.PartitionStyle -eq $PartitionStyle) { # The disk partition is already initialized with the correct partition style Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DiskAlreadyInitializedMessage ` -f $DiskIdType, $DiskId, $disk.PartitionStyle) ) -join '' ) } else { # This disk is initialized but with the incorrect partition style New-InvalidOperationException ` -Message ($script:localizedData.DiskInitializedWithWrongPartitionStyleError ` -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) } } # Get the partitions on the disk $partition = $disk | Get-Partition -ErrorAction SilentlyContinue # Check if the disk has an existing partition assigned to the drive letter $assignedPartition = $partition | Where-Object -Property DriveLetter -eq $DriveLetter # Check if existing partition already has file system on it if ($null -eq $assignedPartition) { # There is no partiton with this drive letter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriveNotFoundOnPartitionMessage ` -f $DiskIdType, $DiskId, $DriveLetter) ) -join '' ) # Are there any partitions defined on this disk? if ($partition) { # There are partitions defined - identify if one matches the size required if ($Size) { # Find the first basic partition matching the size $partition = $partition | Where-Object -FilterScript { $_.Type -eq 'Basic' -and $_.Size -eq $Size } | Select-Object -First 1 if ($partition) { # A partition matching the required size was found Write-Verbose -Message ($script:localizedData.MatchingPartitionFoundMessage ` -f $DiskIdType, $DiskId, $partition.PartitionNumber) } else { # A partition matching the required size was not found Write-Verbose -Message ($script:localizedData.MatchingPartitionNotFoundMessage ` -f $DiskIdType, $DiskId) } # if } else { <# No size specified, so see if there is a partition that has a volume matching the file system type that is not assigned to a drive letter. #> Write-Verbose -Message ($script:localizedData.MatchingPartitionNoSizeMessage ` -f $DiskIdType, $DiskId) $searchPartitions = $partition | Where-Object -FilterScript { $_.Type -eq 'Basic' -and -not [System.Char]::IsLetter($_.DriveLetter) } $partition = $null foreach ($searchPartition in $searchPartitions) { # Look for the volume in the partition. Write-Verbose -Message ($script:localizedData.SearchForVolumeMessage ` -f $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) $searchVolumes = $searchPartition | Get-Volume $volumeMatch = $searchVolumes | Where-Object -FilterScript { $_.FileSystem -eq $FSFormat } if ($volumeMatch) { <# Found a partition with a volume that matches file system type and not assigned a drive letter. #> $partition = $searchPartition Write-Verbose -Message ($script:localizedData.VolumeFoundMessage ` -f $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) break } # if } # foreach } # if } # if # Do we need to create a new partition? if (-not $partition) { # Attempt to create a new partition $partitionParams = @{ DriveLetter = $DriveLetter } if ($Size) { # Use only a specific size Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.CreatingPartitionMessage ` -f $DiskIdType, $DiskId, $DriveLetter, "$($Size/1KB) KB") ) -join '' ) $partitionParams['Size'] = $Size } else { # Use the entire disk Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.CreatingPartitionMessage ` -f $DiskIdType, $DiskId, $DriveLetter, 'all free space') ) -join '' ) $partitionParams['UseMaximumSize'] = $true } # if # Create the partition. $partition = $disk | New-Partition @partitionParams <# After creating the partition it can take a few seconds for it to become writeable Wait for up to 30 seconds for the parition to become writeable #> $timeAtStart = Get-Date $minimumTimeToWait = $timeAtStart + (New-Timespan -Second 3) $maximumTimeToWait = $timeAtStart + (New-Timespan -Second 30) while (($partitionstate.IsReadOnly -and (Get-Date) -lt $maximumTimeToWait) ` -or ((Get-Date) -lt $minimumTimeToWait)) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " ($script:localizedData.NewPartitionIsReadOnlyMessage ` -f $DiskIdType, $DiskId, $partition.PartitionNumber) ) -join '' ) Start-Sleep -Seconds 1 # Pull the partition details again to check if it is readonly $partitionstate = $partition | Get-Partition } # while } # if if ($partition.IsReadOnly) { # The partition is still readonly - throw an exception New-InvalidOperationException ` -Message ($script:localizedData.NewParitionIsReadOnlyError ` -f $DiskIdType, $DiskId, $partition.PartitionNumber) } # if $assignDriveLetter = $true } else { # The disk already has a partition on it that is assigned to the Drive Letter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.PartitionAlreadyAssignedMessage ` -f $DriveLetter, $assignedPartition.PartitionNumber) ) -join '' ) $assignDriveLetter = $false $supportedSize = $assignedPartition | Get-PartitionSupportedSize <# If the parition size was not specified then try and make the partition use all possible space on the disk. #> if (-not ($PSBoundParameters.ContainsKey('Size'))) { $Size = $supportedSize.SizeMax } if ($assignedPartition.Size -ne $Size) { # A patition resize is required if ($AllowDestructive) { if ($FSFormat -eq 'ReFS') { Write-Warning -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.ResizeRefsNotPossibleMessage ` -f $DriveLetter, $assignedPartition.Size, $Size) ) -join '' ) } else { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SizeMismatchCorrectionMessage ` -f $DriveLetter, $assignedPartition.Size, $Size) ) -join '' ) if ($Size -gt $supportedSize.SizeMax) { New-InvalidArgumentException -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.FreeSpaceViolationError ` -f $DriveLetter, $assignedPartition.Size, $Size, $supportedSize.SizeMax) ) -join '' ) -ArgumentName 'Size' -ErrorAction Stop } $assignedPartition | Resize-Partition -Size $Size } } else { # A partition resize was required but is not allowed Write-Warning -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.ResizeNotAllowedMessage ` -f $DriveLetter, $assignedPartition.Size, $Size) ) -join '' ) } } } # Get the Volume on the partition $volume = $partition | Get-Volume # Is the volume already formatted? if ($volume.FileSystem -eq '') { # The volume is not formatted $formatVolumeParameters = @{ FileSystem = $FSFormat Confirm = $false } if ($FSLabel) { # Set the File System label on the new volume $formatVolumeParameters['NewFileSystemLabel'] = $FSLabel } # if if ($AllocationUnitSize) { # Set the Allocation Unit Size on the new volume $formatVolumeParameters['AllocationUnitSize'] = $AllocationUnitSize } # if Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.FormattingVolumeMessage -f $formatVolumeParameters.FileSystem) ) -join '' ) # Format the volume $volume = $partition | Format-Volume @formatVolumeParameters } else { # The volume is already formatted if ($PSBoundParameters.ContainsKey('FSFormat')) { # Check the filesystem format $fileSystem = $volume.FileSystem if ($fileSystem -ne $FSFormat) { # The file system format does not match Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.FileSystemFormatMismatch ` -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) if ($AllowDestructive) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.VolumeFormatInProgressMessage ` -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) $formatParam = @{ FileSystem = $FSFormat Force = $true } if ($PSBoundParameters.ContainsKey('AllocationUnitSize')) { $formatParam.Add('AllocationUnitSize', $AllocationUnitSize) } $Volume | Format-Volume @formatParam } } # if } # if # Check the volume label if ($PSBoundParameters.ContainsKey('FSLabel')) { # The volume should have a label assigned if ($volume.FileSystemLabel -ne $FSLabel) { # The volume lable needs to be changed because it is different. Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.ChangingVolumeLabelMessage ` -f $DriveLetter, $FSLabel) ) -join '' ) $volume | Set-Volume -NewFileSystemLabel $FSLabel } # if } # if } # if # Assign the Drive Letter if it isn't assigned if ($assignDriveLetter -and ($partition.DriveLetter -ne $DriveLetter)) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.AssigningDriveLetterMessage -f $DriveLetter) ) -join '' ) $null = $partition | Set-Partition -NewDriveLetter $DriveLetter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SuccessfullyInitializedMessage -f $DriveLetter) ) -join '' ) } # if } # Set-TargetResource <# .SYNOPSIS Tests if the disk is initialized, the partion exists and the drive letter is assigned. .PARAMETER DriveLetter Specifies the preferred letter to assign to the disk volume. .PARAMETER DiskId Specifies the disk identifier for the disk to modify. .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. .PARAMETER PartitionStyle Specifies the partition style of the disk. Defaults to GPT. .PARAMETER Size Specifies the size of new volume. Leave empty to use the remaining free space. .PARAMETER FSLabel Specifies the volume label to assign to the volume. .PARAMETER AllocationUnitSize Specifies the allocation unit size to use when formatting the volume. .PARAMETER FSFormat Specifies the file system format of the new volume. .PARAMETER AllowDestructive Specifies if potentially destructive operations may occur. .PARAMETER ClearDisk Specifies if the disks partition schema should be removed entirely, even if data and OEM partitions are present. Only possible with AllowDestructive enabled. #> function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] $DriveLetter, [Parameter(Mandatory = $true)] [System.String] $DiskId, [Parameter()] [ValidateSet('Number', 'UniqueId', 'Guid', 'Location')] [System.String] $DiskIdType = 'Number', [Parameter()] [ValidateSet('GPT', 'MBR')] [System.String] $PartitionStyle = 'GPT', [Parameter()] [System.UInt64] $Size, [Parameter()] [System.String] $FSLabel, [Parameter()] [System.UInt32] $AllocationUnitSize, [Parameter()] [ValidateSet('NTFS', 'ReFS')] [System.String] $FSFormat = 'NTFS', [Parameter()] [System.Boolean] $AllowDestructive, [Parameter()] [System.Boolean] $ClearDisk ) Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.TestingDiskMessage -f $DiskIdType, $DiskId, $DriveLetter) ) -join '' ) # Validate the DriveLetter parameter $DriveLetter = Assert-DriveLetterValid -DriveLetter $DriveLetter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.CheckDiskInitializedMessage -f $DiskIdType, $DiskId) ) -join '' ) # Get the Disk using the identifiers supplied $disk = Get-DiskByIdentifier ` -DiskId $DiskId ` -DiskIdType $DiskIdType if (-not $disk) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DiskNotFoundMessage -f $DiskIdType, $DiskId) ) -join '' ) return $false } # if if ($disk.IsOffline) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DiskNotOnlineMessage -f $DiskIdType, $DiskId) ) -join '' ) return $false } # if if ($disk.IsReadOnly) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DiskReadOnlyMessage ` -f $DiskIdType, $DiskId) ) -join '' ) return $false } # if if ($disk.PartitionStyle -ne $PartitionStyle) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DiskPartitionStyleNotMatchMessage ` -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) ) -join '' ) if ($disk.PartitionStyle -eq 'RAW' -or ($AllowDestructive -and $ClearDisk)) { return $false } else { # This disk is initialized but with the incorrect partition style New-InvalidOperationException ` -Message ($script:localizedData.DiskInitializedWithWrongPartitionStyleError ` -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) } } # if $partition = Get-Partition ` -DriveLetter $DriveLetter ` -ErrorAction SilentlyContinue | Select-Object -First 1 if ($partition.DriveLetter -ne $DriveLetter) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriveLetterNotFoundMessage -f $DriveLetter) ) -join '' ) return $false } # if # Check the partition size if ($partition -and -not ($PSBoundParameters.ContainsKey('Size'))) { $supportedSize = ($partition | Get-PartitionSupportedSize) <# If the difference in size between the supported partition size and the current partition size is less than 1MB then set the desired partition size to the current size. This will prevent any size difference less than 1MB from trying to contiuously resize. See https://github.com/dsccommunity/StorageDsc/issues/181 #> if (($supportedSize.SizeMax - $partition.Size) -lt 1MB) { $Size = $partition.Size } else { $Size = $supportedSize.SizeMax } } if ($Size) { if ($partition.Size -ne $Size) { # The partition size mismatches if ($AllowDestructive) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SizeMismatchWithAllowDestructiveMessage ` -f $DriveLetter, $Partition.Size, $Size) ) -join '' ) return $false } else { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.SizeMismatchMessage ` -f $DriveLetter, $Partition.Size, $Size) ) -join '' ) } } # if } # if $blockSize = (Get-CimInstance ` -Query "SELECT BlockSize from Win32_Volume WHERE DriveLetter = '$($DriveLetter):'" ` -ErrorAction SilentlyContinue).BlockSize if ($blockSize -gt 0 -and $AllocationUnitSize -ne 0) { if ($AllocationUnitSize -ne $blockSize) { # The allocation unit size mismatches Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.AllocationUnitSizeMismatchMessage ` -f $DriveLetter, $($blockSize.BlockSize / 1KB), $($AllocationUnitSize / 1KB)) ) -join '' ) if ($AllowDestructive) { return $false } } # if } # if # Get the volume so the properties can be checked $volume = Get-Volume ` -DriveLetter $DriveLetter ` -ErrorAction SilentlyContinue if ($PSBoundParameters.ContainsKey('FSFormat')) { # Check the filesystem format $fileSystem = $volume.FileSystem if ($fileSystem -ne $FSFormat) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.FileSystemFormatMismatch ` -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) if ($AllowDestructive) { return $false } } # if } # if if ($PSBoundParameters.ContainsKey('FSLabel')) { # Check the volume label $label = $volume.FileSystemLabel if ($label -ne $FSLabel) { # The assigned volume label is different and needs updating Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($script:localizedData.DriveLabelMismatch ` -f $DriveLetter, $label, $FSLabel) ) -join '' ) return $false } # if } # if return $true } # Test-TargetResource Export-ModuleMember -Function *-TargetResource |