ReverseDSC.Core.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 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
$Global:CredsRepo = @() function Get-DSCParamType { <# .SYNOPSIS Retrieves the data type of a specific parameter from the associated DSC resource. .DESCRIPTION This function scans the specified module (or in this case DSC resource), checks for the specified parameter inside the .schema.mof file associated with that module and properly assesses and returns the Data Type assigned to the parameter. .PARAMETER ModulePath Full file path to the .psm1 module we are looking for the property inside of. In most cases this will be the full path to the .psm1 file of the DSC resource. .PARAMETER ParamName Name of the parameter in the module we want to determine the Data Type for. #> [CmdletBinding()] [OutputType([System.String])] param( [parameter(Mandatory = $true)] [System.String] $ModulePath, [parameter(Mandatory = $true)] [System.String] $ParamName ) $tokens = $null $errors = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile($ModulePath, [ref] $tokens, [ref] $errors) $functions = $ast.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true) ForEach($function in $functions){ if ($function.Name -eq "Set-TargetResource") { $functionAst = [System.Management.Automation.Language.Parser]::ParseInput($function.Body, [ref] $tokens, [ref] $errors) $parameters = $functionAst.FindAll( {$args[0] -is [System.Management.Automation.Language.ParameterAst]}, $true) ForEach($parameter in $parameters){ if ($parameter.Name.Extent.Text -eq $ParamName) { $attributes = $parameter.Attributes ForEach($attribute in $attributes){ if ($attribute.TypeName.FullName -like "System.*") { return $attribute.TypeName.FullName } elseif ($attribute.TypeName.FullName.ToLower() -eq "microsoft.management.infrastructure.ciminstance") { return "System.Collections.Hashtable" } elseif ($attribute.TypeName.FullName.ToLower() -eq "string") { return "System.String" } elseif ($attribute.TypeName.FullName.ToLower() -eq "boolean") { return "System.Boolean" } elseif ($attribute.TypeName.FullName.ToLower() -eq "bool") { return "System.Boolean" } elseif ($attribute.TypeName.FullName.ToLower() -eq "string[]") { return "System.String[]" } elseif ($attribute.TypeName.FullName.ToLower() -eq "microsoft.management.infrastructure.ciminstance[]") { return "Microsoft.Management.Infrastructure.CimInstance[]" } } } } } } } function Get-DSCBlock { <# .SYNOPSIS Generate the DSC string representing the resource's instance. .DESCRIPTION This function is really the core of ReverseDSC. It takes in an array of parameters and returns the DSC string that represents the given instance of the specified resource. .PARAMETER ModulePath Full file path to the .psm1 module we are looking to get an instance of. In most cases this will be the full path to the .psm1 file of the DSC resource. .PARAMETER Params Hashtable that contains the list of Key properties and their values. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.String] $ModulePath, [Parameter(Mandatory = $true)] [System.Collections.Hashtable] $Params ) $Sorted = $Params.GetEnumerator() | Sort-Object -Property Name $NewParams = [Ordered]@{} foreach($entry in $Sorted) { if ($null -ne $entry.Value) { $NewParams.Add($entry.Key, $entry.Value) } } # Figure out what parameter has the longuest name, and get its Length; $maxParamNameLength = 0 foreach ($param in $NewParams.Keys) { if ($param.Length -gt $maxParamNameLength) { $maxParamNameLength = $param.Length } } # PSDscRunAsCredential is 20 characters and in most case the longuest. if ($maxParamNameLength -lt 20) { $maxParamNameLength = 20 } $dscBlock = "" $NewParams.Keys | ForEach-Object { if ($null -ne $NewParams[$_]) { $paramType = $NewParams[$_].GetType().Name } else { $paramType = Get-DSCParamType -ModulePath $ModulePath -ParamName "`$$_" } $value = $null if ($paramType -eq "System.String" -or $paramType -eq "String" -or $paramType -eq "Guid" -or $paramType -eq 'TimeSpan') { if (!$null -eq $NewParams.Item($_)) { $value = "`"" + $NewParams.Item($_).ToString().Replace("`"", "```"") + "`"" } else { $value = "`"" + $NewParams.Item($_) + "`"" } } elseif ($paramType -eq "System.Boolean" -or $paramType -eq "Boolean") { $value = "`$" + $NewParams.Item($_) } elseif ($paramType -eq "System.Management.Automation.PSCredential") { if ($null -ne $NewParams.Item($_)) { if ($NewParams.Item($_).ToString() -like "`$Creds*") { $value = $NewParams.Item($_).Replace("-", "_").Replace(".", "_") } else { if ($null -eq $NewParams.Item($_).UserName) { $value = "`$Creds" + ($NewParams.Item($_).Split('\'))[1].Replace("-", "_").Replace(".", "_") } else { if ($NewParams.Item($_).UserName.Contains("@") -and !$NewParams.Item($_).UserName.COntains("\")) { $value = "`$Creds" + ($NewParams.Item($_).UserName.Split('@'))[0] } else { $value = "`$Creds" + ($NewParams.Item($_).UserName.Split('\'))[1].Replace("-", "_").Replace(".", "_") } } } } else { $value = "Get-Credential -Message " + $_ } } elseif ($paramType -eq "System.Collections.Hashtable" -or $paramType -eq "Hashtable") { $value = "@{" $hash = $NewParams.Item($_) $hash.Keys | foreach-object { try { $value += $_ + " = `"" + $hash.Item($_) + "`"; " } catch { $value = $hash } } $value += "}" } elseif ($paramType -eq "System.String[]" -or $paramType -eq "String[]" -or $paramType -eq "ArrayList" -or $paramType -eq "List``1") { $hash = $NewParams.Item($_) if ($hash -and !$hash.ToString().StartsWith("`$ConfigurationData.")) { $value = "@(" $hash| ForEach-Object { $value += "`"" + $_ + "`"," } if($value.Length -gt 2) { $value = $value.Substring(0,$value.Length -1) } $value += ")" } else { if ($hash) { $value = $hash } else { $value = "@()" } } } elseif ($paramType -eq "System.UInt32[]") { $hash = $NewParams.Item($_) if ($hash) { $value = "@(" $hash| ForEach-Object { $value += $_.ToString() + "," } if($value.Length -gt 2) { $value = $value.Substring(0,$value.Length -1) } $value += ")" } else { if ($hash) { $value = $hash } else { $value = "@()" } } } elseif ($paramType -eq "Object[]" -or $paramType -eq "Microsoft.Management.Infrastructure.CimInstance[]") { $array = $hash = $NewParams.Item($_) if ($array.Length -gt 0 -and ($array[0].GetType().Name -eq "String" -and $paramType -ne "Microsoft.Management.Infrastructure.CimInstance[]")) { $value = "@(" $hash| ForEach-Object { $value += "`"" + $_ + "`"," } if($value.Length -gt 2) { $value = $value.Substring(0,$value.Length -1) } $value += ")" } else { $value = "@(" $array | ForEach-Object{ $value += $_ } $value += ")" } } elseif ($paramType -eq "CimInstance") { $value = $NewParams[$_] } else { if ($null -eq $NewParams[$_]) { $value = "`$null" } else { if($NewParams[$_].GetType().BaseType.Name -eq "Enum") { $value = "`"" + $NewParams.Item($_) + "`"" } else { $value = $NewParams.Item($_) } } } # Determine the number of additional spaces we need to add before the '=' to make sure the values are all aligned. This number # is obtained by substracting the length of the current parameter's name to the maximum length found. $numberOfAdditionalSpaces = $maxParamNameLength - $_.Length $additionalSpaces = "" for ($i = 0; $i -lt $numberOfAdditionalSpaces; $i++) { $additionalSpaces += " " } $dscBlock += " " + $_ + $additionalSpaces + " = " + $value + ";`r`n" } return $dscBlock } function Get-DSCFakeParameters { <# .SYNOPSIS Generates a hashtable containing all the properties exposed by the specified DSC resource but with fake values. .DESCRIPTION This function scans the specified resources, create a hashtable with all the properties it exposes and generates fake values for each property based on the Data Type assigned to it. .PARAMETER ModulePath Full file path to the .psm1 module we are looking to get an instance of. In most cases this will be the full path to the .psm1 file of the DSC resource. #> [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param( [Parameter(Mandatory = $true)] [System.String] $ModulePath ) $params = @{} $tokens = $null $errors = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile($ModulePath, [ref] $tokens, [ref] $errors) $functions = $ast.FindAll( {$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst]}, $true) $functions | ForEach-Object{ if ($_.Name -eq "Get-TargetResource") { $functionAst = [System.Management.Automation.Language.Parser]::ParseInput($_.Body, [ref] $tokens, [ref] $errors) $parameters = $functionAst.FindAll( {$args[0] -is [System.Management.Automation.Language.ParameterAst]}, $true) $parameters | ForEach-Object{ $paramName = $_.Name.Extent.Text $attributes = $_.Attributes $found = $false <# Loop once to figure out if there is a validate Set to use. #> $attributes | ForEach-Object{ if ($_.TypeName.FullName -eq "ValidateSet") { $params.Add($paramName.Replace("`$", ""), $_.PositionalArguments[0].ToString().Replace("`"", "").Replace("'","")) $found = $true } elseif ($_.TypeName.FullName -eq "ValidateRange") { $params.Add($paramName.Replace("`$", ""), $_.PositionalArguments[0].ToString()) $found = $true } } $attributes | ForEach-Object{ if (!$found) { if ($_.TypeName.FullName -eq "System.String" -or $_.TypeName.FullName -eq "String") { $params.Add($paramName.Replace("`$", ""), "*") $found = $true } elseif ($_.TypeName.FullName -eq "System.UInt32" -or $_.TypeName.FullName -eq "Int32") { $params.Add($paramName.Replace("`$", ""), 0) $found = $true } elseif ($_.TypeName.FullName -eq "System.Management.Automation.PSCredential") { $params.Add($paramName.Replace("`$", ""), $null) $found = $true } elseif ($_.TypeName.FullName -eq "System.Management.Automation.Boolean" -or $_.TypeName.FullName -eq "System.Boolean" -or $_.TypeName.FullName -eq "Boolean") { $params.Add($paramName.Replace("`$", ""), $true) $found = $true } elseif ($_.TypeName.FullName -eq "System.String[]" -or $_.TypeName.FullName -eq "String[]") { $params.Add($paramName.Replace("`$", ""),[string]@("1","2")) $found = $true } } } } } } return $params } function Get-DSCDependsOnBlock { <# .SYNOPSIS Generates a string that represents the DependsOn clause based on the received list of dependencies. .DESCRIPTION This function receives an array of string that represents the list of DSC resource dependencies for the current DSC block and generates a string that represents the associated DependsOn DSC string. .PARAMETER DependsOnItems Array of string values that represent the list of depdencies for the current DSC block. Object in the array are expected to be in the form of: [<DSCResourceName>]<InstanceName>. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.Object[]] $DependsOnItems ) $dependsOnClause = "@(" foreach ($clause in $DependsOnItems) { $dependsOnClause += "`"" + $clause + "`"," } $dependsOnClause = $dependsOnClause.Substring(0, $dependsOnClause.Length -1) $dependsOnClause += ");" return $dependsOnClause } <# Region Helper Methods #> function Get-Credentials { <# .SYNOPSIS Returns the full username of (<domain>\<username>) of the specified user if it is already stroed in our credentials hashtable. .DESCRIPTION This function checks in the hashtable that stores all the required credentials (service account, etc.) for our configuration and returns the fully formatted username. .PARAMETER UserName Name of the user we wish to check to see if it is already stored in our credentials hashtable. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.String] $UserName ) if ($Global:CredsRepo.Contains($UserName.ToLower())) { return $UserName.ToLower() } return $null } function Resolve-Credentials { <# .SYNOPSIS Returns a string representing the name of the PSCredential variable associated with the specific username. .DESCRIPTION This function takes in a specified user name and returns what the standardized variable name for that user should be inside of our extracted DSC configuration. Credentials variables will always be named $Creds<username> as a standard for ReverseDSC. This function makes sure that the variable name doesn't contain character that are invalid in variable names bu might be valid in Usernames. .PARAMETER UserName Name of the user we wish to get the associated variable name from. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.String] $UserName ) $userNameParts = $UserName.ToLower().Split('\') if ($userNameParts.Length -gt 1) { return "`$Creds" + $userNameParts[1].Replace("-","_").Replace(".", "_").Replace(" ", "").Replace("@","") } return "`$Creds" + $UserName.Replace("-","_").Replace(".", "_").Replace(" ", "").Replace("@","") } function Save-Credentials { <# .SYNOPSIS Adds the specified username to our central list of required credentials. .DESCRIPTION This function checks to see if the specified user is already stored in our central required credentials list, and if not simply adds it to it. .PARAMETER UserName Username to add to the central list of required credentials. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [System.String] $UserName ) if (!$Global:CredsRepo.Contains($UserName.ToLower())) { $Global:CredsRepo += $UserName.ToLower() } } function Test-Credentials { <# .SYNOPSIS Checks to see if the specified username if already in our central list of required credentials. .DESCRIPTION This function checks the central list of required credentials to see if the specified user is already part of it. If it finds it, it returns $true, otherwise it returns false. .PARAMETER UserName Username to check for existence in the central list of required users. #> [CmdletBinding()] [OutputType([System.Boolean])] param( [Parameter(Mandatory = $true)] [System.String] $UserName ) if ($Global:CredsRepo.Contains($UserName.ToLower())) { return $true } return $false } function Convert-DSCStringParamToVariable { <# .SYNOPSIS Removes quotes around a parameter in the resulting DSC config, effectively converting it to a variable instead of a string value. .DESCRIPTION This function will scan the content of the current DSC block for the resource, find the specified parameter and remove quotes around its value so that it becomes a variable instead of a string value. .PARAMETER DSCBlock The string representation of the current DSC resource instance we are extracting along with all of its parameters and values. .PARAMETER ParameterName The name of the parameter we wish to convert the value as a variable instead of a string value for. .PARAMETER IsCIMArray Represents whether or not the parameter to convert to a variable is an array of CIM instances or not. We need to differentiate by explicitely passing in this parameter because to the function a CIMArray is nothing but a System.Object[] and will threat it as it. CIMArray differ in that we should not have commas in between items it contains. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.String] $DSCBlock, [Parameter(Mandatory = $true)] [System.String] $ParameterName, [Parameter()] [System.Boolean] $IsCIMArray = $false ) # There is a possibility that a parameter's value will contain the name of the property # or that another property will contain a subset of it. $startPosition = $DSCBlock.IndexOf($ParameterName) # If quotes appear before an equal sign, when starting from the assumed start position, # then the start position is invalid, searhc for another instance of the Parameter; $startPosition = -1 do { $startPosition = $DSCBlock.IndexOf($ParameterName, $startPosition + 1) $testValidStartPositionEqual = $DSCBlock.IndexOf("=", $startPosition) $testValidStartPositionQuotes = $DSCBlock.IndexOf("`"", $startPosition) } while ($testValidStartPositionEqual -gt $testValidStartPositionQuotes -and $startPosition -ne -1) $endOfLinePosition = $DSCBlock.IndexOf(";`r`n", $startPosition) if ($endOfLinePosition -eq -1) { $endOfLinePosition = $DSCBlock.Length } $startPosition = $DSCBlock.IndexOf("`"", $startPosition) while ($startPosition -ge 0 -and $startPosition -lt $endOfLinePosition) { if ($endOfLinePosition -gt $startPosition) { if ($startPosition -ge 0) { $endPosition = $DSCBlock.IndexOf("`"", $startPosition + 1) if ($endPosition -lt 0) { $endPosition = $DSCBlock.IndexOf("'", $startPosition + 1) } if ($endPosition -ge 0 -and $endPosition -le $endofLinePosition) { $removeBeginQuotes = $true $removeEndQuotes = $true $NewStartPosition = $startPosition if ($IsCIMArray) { $previousEqualSignPosition = $DSCBlock.IndexOf("=", $startPosition -2) # If we have a CIMArray, and the current quote we are looking at # is exactly 2 positions before it, we skip remove it because it # actually is the quotes surrounding a value of an entry of the # CIMArray. If it was the principal quotes we were looking at removing # the previous equal sign would be further before due to CIMArray being # declared as ' = @("MSFT_....'; if (($previousEqualSignPosition - $startPosition -2) -lt 0) { $removeBeginQuotes = $false } $previousEqualSignPosition = $DSCBlock.IndexOf("=", $endPosition -2) $nextNewLinePosition = $DSCBLock.IndexOf("`r`n", $endPosition + 1) if (($previousEqualSignPosition - $endPosition -2) -lt 0 -or $nextNewLinePosition -eq ($endPosition +1)) { $removeEndQuotes = $false $newStartPosition = $DSCBlock.IndexOf("`r`n", $endPosition) } } if ($removeBeginQuotes) { $DSCBlock = $DSCBlock.Remove($startPosition, 1) } if ($removeEndQuotes) { $DSCBlock = $DSCBlock.Remove($endPosition-1, 1) } $startPosition = $newStartPosition } else { $startPosition = -1 } } } $startPosition = $DSCBlock.IndexOf("`"", $startPosition) } if($IsCIMArray) { $DSCBlock = $DSCBlock.Replace("}`r`n,", "`}`r`n") } return $DSCBlock } <# Region ConfigurationData Methods #> $ConfigurationDataContent = @{} function Add-ConfigurationDataEntry { <# .SYNOPSIS Adds a property to the resulting ConfigurationData file from the extract. .DESCRIPTION This function helps build the hashtable that will eventually result in the ConfigurationData .psd1 file generated by the extraction of the configuration. It allows you to speficy what section to add it to inside the hashtable, and allows you to speficy a description for each one. These description will eventually become comments that will appear on top of the property in the ConfigurationData file. .PARAMETER Node Specifies the node entry under which we want to add this parameter under. You can also specify NonNodeData names to have the property added under custom non-node specific section. .PARAMETER Key The name of the parameter to add. .PARAMETER Value The value of the parameter to add. .PARAMETER Description Description of the parameter to add. This will ultimately appear in the generated ConfigurationData .psd1 file as a comment appearing on top of the parameter. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [System.String] $Node, [Parameter(Mandatory = $true)] [System.String] $Key, [Parameter(Mandatory = $true)] [System.String] $Value, [Parameter()] [System.String] $Description ) if ($null -eq $ConfigurationDataContent[$Node]) { $ConfigurationDataContent.Add($Node, @{}) $ConfigurationDataContent[$Node].Add("Entries", @{}) } if (!$ConfigurationDataContent[$Node].Entries.ContainsKey($Key)) { $ConfigurationDataContent[$Node].Entries.Add($Key, @{Value = $Value; Description = $Description}) } } function Get-ConfigurationDataEntry { <# .SYNOPSIS Retrieves the value of a given property in the specified node/section from the hashtable that is being dynamically built. .DESCRIPTION This function will return the value of the specified parameter from the hash table being dynamically built and which will ultimately become the content of the ConfigurationData .psd1 file being generated. .PARAMETER Node The name of the node or section in the Hashtable we want to look for the key in. .PARAMETER Key The name of the parameter to retrieve the value from. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.String] $Node, [Parameter(Mandatory = $true)] [System.String] $Key ) <# If node is null, then search in all nodes and return first result found. #> if ($null -eq $Node) { foreach ($Node in $ConfigurationDataContent.Keys) { if ($ConfigurationDataContent[$Node].Entries.ContainsKey($Key)) { return $ConfigurationDataContent[$Node].Entries[$Key] } } } else { if ($ConfigurationDataContent[$Node].Entries.ContainsKey($Key)) { return $ConfigurationDataContent[$Node].Entries[$Key] } } } function Get-ConfigurationDataContent { <# .SYNOPSIS Retrieves the entire content of the ConfigurationData file being dynamically generated. .DESCRIPTION This function will return the content of the dynamically built hashtable for the ConfigurationData content as a formatted string. #> [CmdletBinding()] [OutputType([System.String])] param() $psd1Content = "@{`r`n" $psd1Content += " AllNodes = @(`r`n" foreach ($node in $ConfigurationDataContent.Keys.Where{$_.ToLower() -ne "nonnodedata"}) { $psd1Content += " @{`r`n" $psd1Content += " NodeName = `"" + $node + "`"`r`n" $psd1Content += " PSDscAllowPlainTextPassword = `$true;`r`n" $psd1Content += " PSDscAllowDomainUser = `$true;`r`n" $psd1Content += " # CertificateFile = `"\\<location>\<file>.cer`";`r`n" $psd1Content += " # Thumbprint = `xxxxxxxxxxxxxxxxxxxxxxx`r`n`r`n" $psd1Content += " #region Parameters`r`n" $keyValuePair = $ConfigurationDataContent[$node].Entries foreach ($key in $keyValuePair.Keys) { if ($null -ne $keyValuePair[$key].Description) { $psd1Content += " # " + $keyValuePair[$key].Description + "`r`n" } if ($keyValuePair[$key].Value.ToString().StartsWith("@(") -or $keyValuePair[$key].Value.ToString().StartsWith("`$")) { $psd1Content += " " + $key + " = " + $keyValuePair[$key].Value + "`r`n`r`n" } elseif ($keyValuePair[$key].Value.GetType().FullName -eq "System.Object[]") { $psd1Content += " " + $key + " = " + (ConvertTo-ConfigurationDataString $keyValuePair[$key].Value) } else { $psd1Content += " " + $key + " = `"" + $keyValuePair[$key].Value + "`"`r`n`r`n" } } $psd1Content += " },`r`n" } if ($psd1Content.EndsWith(",`r`n")) { $psd1Content = $psd1Content.Remove($psd1Content.Length-3, 1) } $psd1Content += " )`r`n" $psd1Content += " NonNodeData = @(`r`n" foreach ($node in $ConfigurationDataContent.Keys.Where{$_.ToLower() -eq "nonnodedata"}) { $psd1Content += " @{`r`n" $keyValuePair = $ConfigurationDataContent[$node].Entries foreach ($key in $keyValuePair.Keys) { try { $value = $keyValuePair[$key].Value $valType = $value.GetType().FullName if ($valType -eq "System.Object[]") { $newValue = "@(" foreach ($item in $value) { $newValue += "`"" + $item + "`"," } $newValue = $newValue.Substring(0,$newValue.Length -1) $newValue += ")" $value = $newValue } if ($null -ne $keyValuePair[$key].Description) { $psd1Content += " # " + $keyValuePair[$key].Description + "`r`n" } if ($value.StartsWith("@(") -or $value.StartsWith("`$")) { $psd1Content += " " + $key + " = " + $value + "`r`n`r`n" } else { $psd1Content += " " + $key + " = `"" + $value + "`"`r`n`r`n" } } catch { Write-Host "Warning: Could not obtain value for key $key" -ForegroundColor Yellow } } $psd1Content += " }`r`n" } if($psd1Content.EndsWith(",`r`n")) { $psd1Content = $psd1Content.Remove($psd1Content.Length-3, 1) } $psd1Content += " )`r`n" $psd1Content += "}" return $psd1Content } function New-ConfigurationDataDocument { <# .SYNOPSIS Generates a new ConfigurationData .psd1 file. .DESCRIPTION This function will create the ConfigurationData .psd1 file and store the content of the converted hashtable in it. .PARAMETER Path Full file path of the the resulting file will be located. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [System.String] $Path ) Get-ConfigurationDataContent | Out-File -FilePath $Path } function ConvertTo-ConfigurationDataString { <# .SYNOPSIS Converts items from the content of the dynamic hashtable to be used as the content of the ConfigurationData .psd1 file into their proper string representation. .DESCRIPTION This function will loop through all items inside the dynamic hashtable used for the resulting ConfigurationData .psd1 file's content and converts each one to the proper string representation based on their data type. .PARAMETER PSObject The hashtable object we are building and which is to be used to drive the content of the ConfigurationData .psd1 file. #> [CmdletBinding()] [OutputType([System.String])] param( [Parameter(Mandatory = $true)] [System.Management.Automation.PSObject] $PSObject ) $configDataContent = "" $objectType = $PSObject.GetType().FullName switch ($objectType) { "System.String" { $configDataContent += "`"" + $PSObject + "`";`r`n" } "System.Object[]" { $configDataContent += " @(`r`n" foreach ($entry in $PSObject) { $configDataContent += ConvertTo-ConfigurationDataString $entry } if ($configDataContent.EndsWith(",`r`n")) { $configDataContent = $configDataContent.Remove($configDataContent.Length-3, 1) } $configDataContent += " )`r`n" } "System.Collections.Hashtable" { $configDataContent += " @{`r`n" foreach ($key in $PSObject.Keys) { $configDataContent += " " + $key + " = " $configDataContent += ConvertTo-ConfigurationDataString $PSObject[$key] } $configDataContent += " },`r`n" } } return $configDataContent } <# Region User based Methods #> $Global:AllUsers = @() function Add-ReverseDSCUserName { <# .SYNOPSIS Adds the provided username to the list of required users for the destination environment. .DESCRIPTION ReverseDSC allows you to keep track of all user credentials encountered during various stages of the extraction process. By keeping a central list of all users account required by the source environment we can easily generate a script that will automatically create new user place holders in a destination environment's Active Directory. This function checks to see if the specified user was already encountered, and if not adds it to the central list of all required users. .PARAMETER UserName Name of the user to add to the central list of required users. #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [System.String] $UserName ) if (!$Global:AllUsers.Contains($UserName)) { $Global:AllUsers += $UserName } } |