# Copyright © 2012, Microsoft Corporation. All rights reserved. function Get-MATSTemp() { $CABDirs = get-childitem "$env:temp\mats-temp\cab*.*" if ($CABDirs.count -gt 0) { $CABDirs = Sort-Object -InputObject $CABDirs -Property LastWriteTime -Descending return $CabDirs[0] } else { return $CABDirs } } # Our implementation of a .PSD1 importer function Import-LocalizedData($FileName, $BindingVariable, $BaseDirectory, $UICulture) { if ($baseDirectory -eq $null) { $base= "$pwd" } else { $base = $basedirectory } $mats = Get-MATSTemp if ($UICulture -ne $null) { $culture = $UICulture } else { $culture = (get-culture).name } $locations = "$base\$culture", "$mats\$culture", "$base", "$mats" foreach ($location in $locations) { if ([System.IO.File]::Exists("$location\$filename.psd1")) { $fullpath = "$location\$filename.psd1" break } } if (![System.IO.File]::Exists($fullpath)) { throw "File not Found: $fullpath" # Write to error output } $stringtable = "" | Select-Object StringTableFileName # Return object $sourceStrings = [System.IO.File]::ReadAllLines($fullpath) # Array of strings in .PSD1 file $stringtable.StringTableFileName = $filename # Place the filename in the stringtable for ($i = 0; $i -lt $sourceStrings.count; $i++) # Loop over all strings { if ($sourceStrings[$i].contains("=")) # Simple check for "xx=yy" pattern { $stringID = $sourceStrings[$i].Substring(0, $sourceStrings[$i].IndexOf("=")) # Get String ID $stringValue = $sourceStrings[$I].SubString($SourceStrings[$i].IndexOf("=")+1) # Get String Value add-member -inputobject $StringTable -membertype noteproperty -name $stringID -value $stringValue # Add this StringID/Value to the return object } } set-variable -Name $bindingvariable -Value $stringtable -Scope "global" # 'return' the completed string table } # Import-LocalizedData # Simple implementation of PowerShell 2.0 add-type cmdlet function Add-Type { param([string]$TypeDefinition, [Array]$ReferencedAssemblies, [switch]$passthru) # Create Provider $csprovider = new-object Microsoft.CSharp.CSharpCodeProvider # Configure the compiler $CompilerParams = new-object System.CodeDom.Compiler.CompilerParameters $CompilerParams.GenerateInMemory = $true # Add some default assemblies $CompilerParams.ReferencedAssemblies.Add("system.dll") > $null $CompilerParams.ReferencedAssemblies.Add([PSObject].assembly.location) > $null # add user-defined assemblies if ($referencedassemblies.count -gt 0) { for ($i = 0; $i -lt $referencesassemblies.count; $i++) { $CompilerParams.ReferencedAssemblies.Add($ReferencedAssemblies[$i]) } } # compile the code $Result = $csprovider.compileAssemblyFromSource($CompilerParams, $TypeDefinition) # check for success if ($Result.errors.count -gt 0) { write-error $Result } else { if ($passthru) { $Result.compiledassembly.getexportedtypes() } } } # Simple implentation of ConvertTo-XML function ConvertTo-XML { Param($InputObject) Begin { $xmlSource = "" $xmlSource += "" } Process { if ($_) { $InputObjectType = $_.GetType.TypeNameOfValue $xmlsource += "" #$xmlSource = "" #$xmlSource += "" $Properties = @() if ((Get-Member -inputobject $_ -MemberType Property) -ne $null ){ $Properties += Get-Member -inputobject $_ -MemberType Property } if ((Get-Member -inputobject $_ -MemberType NoteProperty) -ne $null ){ $Properties += Get-Member -inputobject $_ -MemberType NoteProperty } $specialChars = @{"Source"="<";Dest="<"}, @{"Source"=">";Dest=">"}, @{"Source"="&";Dest="&"} for ($PropertyCount = 0; $PropertyCount -lt $Properties.Count; $PropertyCount++) { [string]$PropName = $Properties[$PropertyCount].Name [string]$PropDef = $Properties[$PropertyCount].Definition [string]$PropType = $PropDef.Split(" ")[0] [string]$Value = $_.$PropName foreach ($entry in $specialChars) { $Value = $Value.Replace($Entry.Source, $Entry.Dest) } $xmlSource += "$Value" } #$xmlSource += "" $xmlSource += "" #$xmlSource } } End { if ($InputObject) { $InputObjectType = $InputObject.GetType.TypeNameOfValue $xmlSource = "" $xmlSource += "" if ((Get-Member -inputobject $InputObject -MemberType Property) -ne $null ){$Properties = Get-Member -inputobject $InputObject -MemberType Property } if ((Get-Member -inputobject $InputObject -MemberType NoteProperty) -ne $null ){$Properties += Get-Member -inputobject $InputObject -MemberType NoteProperty } $specialChars = @{"Source"="<";Dest="<"}, @{"Source"=">";Dest=">"}, @{"Source"="""";Dest="""}, @{"Source"="&";Dest="&"} for ($PropertyCount = 0; $PropertyCount -lt $Properties.Count; $PropertyCount++) { [string]$PropName = $Properties[$PropertyCount].Name [string]$PropDef = $Properties[$PropertyCount].Definition [string]$PropType = $PropDef.Split(" ")[0] [string]$Value = $InputObject.$PropName foreach ($entry in $specialChars) { $Value = $Value.Replace($Entry.Source, $Entry.Dest) } $xmlSource += "$Value" } $xmlSource += "" $xmlSource } else { $xmlSource += "" $xmlSource } } } ########### # Pop up window ############ function Pop-Msg { param([string]$msg ="message", [string]$ttl = "Title", [int]$type = 64) $popwin = new-object -comobject wscript.shell $null = $popwin.popup($msg,0,$ttl,$type) remove-variable popwin }