MyDefaults.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 |
############################################################################### # # Function New-MyDefaultsFile # # v1.0 by Dave Bishop (davbish) 02/26/2014 # ############################################################################### Function New-MyDefaultsFile { <# .SYNOPSIS Creates a new default file for use by the MyDefault functions. .DESCRIPTION New-MyDefaultFile creates a new file in your $profile folder to hold default values. These can then be used by functions when not overridden by specifying them as parameters. This is done by using (Get-WPSDefault 'DefaultName') as the parameter default value in a function definition. For example, if most of the time you use function ABC you want parameter Name to be "thisuser", then define the parameter in the function's code as: $UserName = (Get-MyDefault 'UserName') When you invoke the function, you can still provide UserName as a parameter, but if you do not, it looks up the value for UserName in the MyDefaults.xml file in your $Profile folder. There are no parameters - the file is automatically named MyDefaults.xml and is placed in the $Profile folder. This is required for the other functions to find the file. #> # Construct the path to the file from the $profile variable $DefaultFilePath = join-path (split-path $profile) 'MyDefaults.xml' # Does it already exist? if (test-path $DefaultFilePath) { # if so, error out write-error -message "Default file already exists at $DefaultFilePath." } else # file did not exist already, so create it { @" <MyDefaults version="1.0"> <DxDatabase>wsBlueWin8</DxDatabase> <TFSServer>vstfpg05/pg05</TFSServer> <TFSProject>wspr</TFSProject> <ProductVersion>Threshold</ProductVersion> <DLLOutputFolder>C:\Modout</DLLOutputFolder> <HelpOutputFolder>C:\Helpout</HelpOutputFolder> <PrelimInputPath>\\srvua\dxmref\main\prelim</PrelimInputPath> <DxProdBuildPath>\\srvua\builds</DxProdBuildPath> <DxReqBuildPath>\\srvuafs01\dsreq\release</DxReqBuildPath> <CabArchiveFolder>\\wpshelpserver\CabArchive\Threshold</CabArchiveFolder> <CabStagingFolder>\\wpshelpserver\cabs\threshold\staging</CabStagingFolder> </MyDefaults> "@ | Out-File $DefaultFilePath } } ############################################################################### # # Function Get-MyDefault # # v1.0 by Dave Bishop (davbish) 02/26/2014 # ############################################################################### Function Get-MyDefault { <# .SYNOPSIS Looks up the specified value from the MyDefault file in the user's profile. .DESCRIPTION Use Get-MyDefault to look up the specified value from the MyDefaults file stored in your $Profile folder. The file must be named MyDefaults.xml and can be created by running the command New-MyDefaultsFile. .PARAMETER Name Specifies the name of the default value to look up. .EXAMPLE PS C:\> Get-MyDefault DxDatabase #> [CmdletBinding()] [OutputType([String])] # this function returns a single string value Param ( [Parameter(Position=0,Mandatory=$true)] [string] $Name ) # Get the path to the file in the $Profile folder $DefaultFilePath = join-path (split-path $Profile) 'MyDefaults.xml' # if the file exists, then load it into a variable if (test-path $DefaultFilePath) { $DefaultFile = [xml](get-content $DefaultFilePath) } # look up the value and return it (or $null if it doesn't exist) if ($DefaultFile.MyDefaults.$Name) { return $DefaultFile.MyDefaults.$Name } else { return $null } } ############################################################################### # # Function Set-MyDefault # # v1.0 by Dave Bishop (davbish) 02/26/2014 # ############################################################################### Function Set-MyDefault { <# .SYNOPSIS Creates or modifies a value in the MyDefault file in the user's profile. .DESCRIPTION Set-MyDefault creates or modifies a value in the MyDefault file in the user's profile. The file must be named MyDefaults.xml and can be created by running the command New-MyDefaultsFile. .PARAMETER Name Specifies the name of the default value to create or modify. .PARAMETER Value Specifies the value to assign to the specified default. .EXAMPLE PS C:\> Set-MyDefault DxDatabase wsPowerShell #> [CmdletBinding()] Param ( [Parameter(Position=0,Mandatory=$true)] [string] $Name, [Parameter(Position=1,Mandatory=$true)] [string] $Value, [Parameter(Mandatory=$false)] [switch] $Force ) # get the path to the file in the $profile folder $DefaultFilePath = join-path (split-path $profile) 'MyDefaults.xml' # if the file exists, then load it into memory. if (test-path $DefaultFilePath) { $DefaultFile = [xml](get-content $DefaultFilePath) } else { # couldn't find it. Create it and try again New-MyDefaultsFile $DefaultFile = [xml](get-content $DefaultFilePath) } # By default, only predefined values can be put in the default file. This helps reduce errors. # Use -Force to override and add a totally new value. if (! $DefaultFile.MyDefaults.$Name) { # We didn't find the value. Should we add it anyway? if ($Force) { # Yes, add it anyway. Create a new element and add it to the list $tmp = $DefaultFile.CreateElement($Name) [void]$DefaultFile.MyDefaults.AppendChild($tmp) } else { # No, force is not enabled, so throw error. Throw "The name ($Name) is not a currently defined entry in the MyDefaults.xml file. Use -Force to force its creation. Remember to add it to the template in New-MyDefaultsFile." } } # We either found the entry, or we created a new one to get here. # So add the new value to the setting and save the change. $WPSSupport.WPSDefaults.$Name = $Value $WPSSupport.Save("$DefaultFilePath") } |