Question 2011 Scripting Games : Advanced Event 10

Plus d'informations
il y a 14 ans 9 mois #9707 par Matthew BETTON
Bonsoir,

Ci-après le script que j'ai posté lors des Scripting Games 2011, dans le cadre de l'Advanced Event 10.

Il s'agissait du dernier évènement pour cette compétition B) (Snif !... :( Vivement l'année prochaine ! B) )

Le scénario était le suivant :

Event scenario

You find yourself frequently writing data to a text file. Often, you delete these text files later. You decide that an advanced function that will accept piped content to create a temporary text file would be useful. When added to a module or included in a profile, this function would always be available.

Design points

The function must be designed as an Advanced function, complete with comment-based Help.
The function must accept piped content.
The function should return the path to the temporary file.
The temporary file should have a temporary name, and it should reside in the temporary folder.
Design points for using an appropriate name for this function.
Additional points for adding a switch that will display the content of the temporary file in Notepad.
Additional points for supporting an encoding parameter and support ASCII and Unicode as encoding types.
Additional points for supporting the whatif, debug, and verbose parameters.


Le script que j'avais posté (une solution) :

[code:1]#
# 2011 Scripting Games : Advanced Event 10
# Script : Add-ContentToTempTextFile.ps1
# Author : Matthew BETTON (France / Basse-Normandie / Manche (50))
# Date : 04/15/2011
# Synopsis : Function to Create Temp Files
#

Function Add-ContentToTempTextFile(){

[CmdletBinding(SupportsShouldProcess=$True)]
param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[String[]]$Content,
[Parameter(Mandatory=$false)]
[Switch]$Display,
[Parameter(Mandatory=$false)]
[ValidateSet(\"Ascii\",\"BigEndianUnicode\", \"Byte\", \"String\", \"Unicode\", \"Unknown\", \"UTF8\"«»)]
[String]$Encoding=\"Unicode\"
)

begin{
Write-Verbose \"Function Set-RandomTextFile called\"
Write-Verbose \"Value of parameter '-content' : $Content\"
Write-Verbose \"Value of parameter '-Display' : $Display\"
Write-Verbose \"Value of parameter '-Encoding' : $Encoding\"

Write-Debug \"Creating temporay filename ...\"
if ($psCmdlet.shouldProcess(\"Do operation\"«»)){
$TempFileName = [System.IO.Path]::GetTempFileName()
$TempTxtFileName = $TempFileName -replace \"tmp\", \"txt\"
Rename-Item -Path $TempFileName -NewName $TempTxtFileName
Write-Verbose \"Random temporary file name : $TempFileName\"
}
else{
Write-Host \"Temporary filename has been created\"
}
}
process{
Write-Debug \"Writting Content to temporary file name ...\"
if ($PSBoundParameters) {
Write-Verbose \"'-Content' parameter has been used\"
if ($psCmdlet.shouldProcess(\"Do operation\"«»)){
foreach($c in $Content){
try{
Write-Debug \"Setting content to the temporary file name $TempFileName ...\"
Add-Content -Value $c -Path $TempTxtFileName -Encoding $Encoding -ErrorAction Stop
}
catch{
Write-Error \"An error has occured while setting content to $TempFileName : $($_.Exception.Message)\"
}
}
}
else{
Write-Host \"Content has been set to the temporary file name $TempFileName\"
}
}
else{
Write-Verbose \"Getting value(s) from pipeline ...\"
if ($psCmdlet.shouldProcess(\"Do operation\"«»)){
try{
Write-Debug \"Setting content to the temporary file name ...\"
Add-Content -Value $Content -Path $TempTxtFileName -Encoding $Encoding -ErrorAction Stop
}
catch{
Write-Error \"An error has occured while setting content to $TempFileName : $($_.Exception.Message)\"
}
}
else{
Write-Host \"Content has been set to the temporary file name $TempFileName\"
}
}
}
end{
if($Display){
Write-Verbose \"Opening temporary filename in notepad\"
if ($psCmdlet.shouldProcess(\"Do operation\"«»)){
try{
Write-Debug \"Opening temp file name $TempFileName ...\"
Invoke-Item -Path $TempTxtFileName -ErrorAction Stop
}
catch{
Write-Error \"An error has occured while opening $TempFileName : $($_.Exception.Message)\"
}
}
else{
Write-Host \"$TempFileName has been opened in notepad\"
}
}
return $TempFileName
}

<#
.SYNOPSIS
Writting data to a temporary text file.
.DESCRIPTION
The Add-ContentToTempTextFile function adds string content to a temporary random file name.
.PARAMETER Content
Specifies the string(s) on which the command runs. It is a mandatory parameter.
.PARAMETER Display
Displays the content of the temporary file in Notepad ($false by default).
.PARAMETER Encoding
Choose encoding type (Unicode by default).

Possible encoding types : \"Ascii\", \"BigEndianUnicode\", \"Byte\", \"String\", \"Unicode\", \"Unknown\", \"UTF8\".
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\"

Description
Create a random text file in the temporary folder, containing \"Test text\" string. The text file is UTF8 encoding.
.EXAMPLE
C:\PS> \"Test text\" | Add-ContentToTempTextFile

Description
Create a random text file in the temporary folder, containing \"Test text\" string. The text file is UTF8 encoding.
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\" -Display

Description
Create a random text file in the temporary folder, containing \"Test text\" string. The text file is UTF8 encoding.
Then, the temporary text file is automatically opened in Notepad.
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\" -Display -Encoding Ascii

Description
Create a random text file in the temporary folder, containing \"Test text\" string. The text file is Ascii encoding.
Then, the temporary text file is automatically opened in Notepad.
.EXAMPLE
C:\PS> \"Test 1 text\", \"Test 2 text\" | Add-ContentToTempTextFile -Display -Encoding Ascii

Description
Create a random text file in the temporary folder, containing \"Test 1 text\" and next line \"Test 2 text\" strings.
The text file is Ascii encoding.
Then, the temporary text file is automatically opened in Notepad.
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\" -Display -Encoding Ascii -Debug

Description
Create a random text file in the temporary folder, containing \"Test text\" string.
The text file is Ascii encoding.
Then, the temporary text file is automatically opened in Notepad.
The '-Debug' parameters indicates that debug messages can be wrote to the host console.
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\" -Display -Encoding Ascii -Debug -Whatif

Description
Create a random text file in the temporary folder, containing \"Test text\" string.
The text file is Ascii encoding.
Then, the temporary text file is automatically opened in Notepad.
The '-Debug' parameters displays debug messages.
The '-Whatif' parameters displays what it would normally do.
.EXAMPLE
C:\PS> Add-ContentToTempTextFile -Content \"Test text\" -Display -Encoding Ascii -Debug -Verbose -Whatif

Description
Create a random text file in the temporary folder, containing \"Test text\" string.
The text file is Ascii encoding.
Then, the temporary text file is automatically opened in Notepad.
The '-Debug' parameters displays debug messages.
The '-Whatif' parameters displays what it would normally do.
The '-Verbose' parameter displays more informations...
.INPUTS
System.String
You can pipe a string or strings that contains data.
.OUTPUTS
System.String
Temporary file name.
#>
}[/code:1]

Pour les personnes que cela intéresse, voici le lien vers une solution de l'expert .

Toutes les remarques seront les bienvenues !

;)

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 14 ans 9 mois #9708 par Matthew BETTON
Ed Wilson avait commenté mon script :

good job

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.080 secondes
Propulsé par Kunena