Question 2011 Scripting Games : Advanced Event 9

Plus d'informations
il y a 14 ans 9 mois #9705 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 9.

Le scénario était le suivant :

You need to add a logging routine to a script that runs daily on your workstation. For troubleshooting purposes, you would like to keep all of the log files in a single folder named HSGLogFiles. The exact location of this folder is up to you, but it should be stored on the local machine, and it should be in a location that is accessible to all users of the computer. Because the script runs once per day on a per user basis, the log files should be comprised of the year, month, day, and a logged on user name. You are not worried about concurrent users, or remote users—for the purposes of this scenario assume a single interactive user. If the script runs multiple times for the same user on the same day, do not append or overwrite the original log file (which is created during the first run of the day). In other words, if a file exists, the script should exit gracefully and silently. When you create the file, create it silently by not displaying return information to the Windows PowerShell console. An example of such a log file directory is shown in the following image .



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

[code:1]#
# 2011 Scripting Games : Advanced Event 9
# Script : Set-UserLog.ps1
# Author : Matthew BETTON (France / Basse-Normandie / Manche (50))
# Date : 04/17/2011
# Synopsis : Create a File Name Based on Date and Username.
#

Function Set-UserLog(){

param(
[Parameter(Mandatory=$False)]
[String[]]$LogContent = \"\"
)

$Date = (Get-Date).ToString(\"yyyyMMdd\"«»)
$UserLogin = $env:USERNAME

$logName = \"$Date`_$UserLogin.log\"
$logFolder = \"$($env:«»PUBLIC)\Documents\HSGLogFiles\"
$logFilePath = \"$logFolder\$logName\"


if(-not (Test-Path $logFolder)){
Write-Debug \"$logFolder does not exist. Creating it ...\"
$f = New-Item -Path $logFolder -Type directory
}

if(-not (Test-Path $logFilePath)){
Write-Debug \"$logFilePath does not exist. Creating it ...\"
Set-Content -Value $LogContent -Path $logFilePath -Encoding UTF8
}
else{
Write-Debug \"$logFilePath already exists. Exiting script silently ...\"
exit
}

<#
.SYNOPSIS
Create a File Name Based on Date and Username.
.DESCRIPTION
The Set-UserLog function should be used in a user logon script.
If it does not exist, the 'HSGLogFiles' folder is created under 'C:\Users

\Public\Documents' folder, so all computer's users should see it under their 'Documents'

folder.
If log file already exists, the script exits gracefully and silently.
The log file is named with the date and the current login username.
You can use $DebugPreference variable in order to get DEBUG messages

($DebugPreference = \"Continue\"«»).
.PARAMETER LogContent
Specifies the content of the log file.
By default, if not specified, the log file is empty.
.EXAMPLE
Set-UserLog

Description
Creates a \"yyyyMMdd_[USERNAME].log\" file in 'C:\Users\Public\Documents

\HSGLogFiles' folder, if it does not already exist.
Otherwise, exit the script.

.EXAMPLE
Set-UserLog -logContent \"$(Get-Date) : $($env:USERDOMAIN)\$($env:USERNAME)

connected on $($env:COMPUTERNAME)\"

Description
Creates a \"yyyyMMdd_[USERNAME].log\" in 'C:\Users\Public\Documents\HSGLogFiles'

folder, if it does not already exist. Otherwise, exit the script.
If created, the log file contains one line. For example : 'sunday 17 april 2011

22:15:05 : CONTOSO\mbetton connected on DESKTOP01'
.EXAMPLE
Set-UserLog -logContent \"$(Get-Date)\", \"$($env:USERDOMAIN)\",

\"$($env:USERNAME)\", \"$($env:COMPUTERNAME)\"

Description
Creates a \"yyyyMMdd_[USERNAME].log\" in 'C:\Users\Public\Documents\HSGLogFiles'

folder, if it does not already exist. Otherwise, exit the script.
If created, the log file contains 4 lines. For example : 'sunday 17 april 2011

22:15:05', 'CONTOSO', 'mbetton' and last line 'DESKTOP01'.
#>

}

Set-UserLog[/code:1]


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

... Tiens Tiens... Ce nom me dit quelque chose : Arnaud Pettijean ! ;)

Toutes les remarques seront les bienvenues !<br><br>Message édité par: Matthew BETTON, à: 25/05/11 21:36

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

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

I like your use of the public folder, and the way you create your file name. I also like your use of test-path, and help. Good job.

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

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