PSModuleUtils/Functions/Utils.ps1
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 |
# =========================================================================== # Utils.ps1 --------------------------------------------------------------- # =========================================================================== # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function Get-TemporaryFile { <# .DESCRIPTION Return a random file name in system's temp folder. .PARAMETER Extension .PARAMETER Directory .OUTPUTS Systems.String. Random file name. #> [CmdletBinding(PositionalBinding)] [OutputType([System.String])] Param( [Parameter(HelpMessage="Extension of temporary file, to be created, e.g. '.json'")] [System.String] $Extension, [Parameter(HelpMessage="Return a temporary folder name.")] [Switch] $Directory ) Process{ $temp_folder = [System.IO.Path]::GetTempPath() $temp_file_path = [System.IO.Path]::GetTempFileName() $temp_file_name = [System.IO.Path]::GetFileNameWithoutExtension($temp_file_path) if ($Directory) { return Join-Path -Path $temp_folder -ChildPath $temp_file_name } if ($Extension){ return Join-Path -Path $temp_folder -ChildPath "$($temp_file_name)$($Extension)" } return $temp_file_path } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function New-TemporaryDirectory { <# .DESCRIPTION Creates a folder with a random name in system's temp folder. .OUTPUTS Systems.String. Absolute path of created temporary folder. #> [CmdletBinding(PositionalBinding)] [OutputType([System.String])] Param() $path = Get-TemporaryFile -Directory #if/while path already exists, generate a new path While(Test-Path $path) { $path = Get-TemporaryFile -Directory } #create directory with generated path New-Item -Path $path -ItemType Directory } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function New-TemporaryFile { <# .DESCRIPTION Creates a random file name in system's temp folder. .PARAMETER Extension .OUTPUTS Systems.String. Absolute path of created random file. #> [CmdletBinding(PositionalBinding)] [OutputType([System.String])] Param( [Parameter(HelpMessage="Extension of temporary file, to be created, e.g. '.json'")] [System.String] $Extension ) $path = Get-TemporaryFile -Extension $Extension #if/while path already exists, generate a new path While(Test-Path $path) { $path = Get-TemporaryFile -Extension $Extension } #create directory with generated path New-Item -Path $path -ItemType File } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function ConvertTo-ObjectFromHashtable { [CmdletBinding(PositionalBinding)] [OutputType([System.Object])] Param ( [Parameter(Position=1, Mandatory, ValueFromPipeline,ValueFromPipelineByPropertyName)] [System.Object[]] $Hashtable ) Begin { $i = 0; } Process { foreach ($element in $Hashtable) { if ($element.GetType().Name -eq 'Hashtable') { $object = New-Object -TypeName PSObject Add-Member -InputObject $object -MemberType ScriptMethod -Name AddNote -Value { Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1]; }; $element.Keys | Sort-Object | ForEach-Object { $object.AddNote($_, $element.$_); } } else { Write-Warning "Index $i is not of type [Hashtable]"; } $i += 1; } return $object } } # function ---------------------------------------------------------------- # --------------------------------------------------------------------------- function ConvertTo-HashtableFromObject { [CmdletBinding(PositionalBinding)] [OutputType([System.Object])] Param ( [Parameter(Position=1, Mandatory, ValueFromPipeline,ValueFromPipelineByPropertyName)] [System.Object[]] $Object ) Process { foreach ($element in $Object) { $hashtable = @{} $element | Get-Member -MemberType *Property | ForEach-Object { $hashtable.($_.name) = $element.($_.name) } return $hashtable; } } } |