MyDefaults.psm1
############################################################################### # # 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") } |