Question
2011 Scripting Games : Advanced Event 7
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
Ci-après le script que j'ai posté lors des Scripting Games 2011, dans le cadre de l'Advanced Event 7.
Le scénario était le suivant :
Event scenario
You are enjoying using Twitter. Unfortunately, Twitter usernames are not always easily understandable. You would like to be able to map a Twitter username with the person’s actual name. To do this, you plan to retrieve this information from a SQL Saturday networking web page, such as the one shown in the following image. Then you plan to create a comma-separated value (CSV) file with the Twitter username, and the person’s actual name as fields.
Design points
You should include a function in your script that provides the ability to perform a lookup from the CSV file that will “translate” a user’s name to a Twitter username. The function should also be able to translate from a Twitter username to a person’s real name.
For the purposes of this exercise, use the Columbia, South Carolina Networking page for the SQL Saturday #70 (March 19, 2011) event. The URL for the networking page of this event is:
www.sqlsaturday.com/70/networking.aspx
If you use a temporary file during the process of obtaining Twitter usernames from the networking page, delete the file prior to completing the script.
When you are obtaining the Twitter usernames, do not include the at sign or twitter.com. Therefore, the Twitter username is ScriptingGuys, not twitter.com/scriptingguys or @ScriptingGuys.
Extra points for reusable code.
Extra points for adding useful Help information complete with sample usage.
Although there are certain similarities with Advanced Event 6, there are significant differences. You might be better off starting from “scratch” when you approach this event (or not—depending on how you wrote the solution for the previous event).
Le script que j'avais posté (une solution) :
[code:1]#
# 2011 Scripting Games : Advanced Event 7
# Script : Get-UserTwitterNames.ps1
# Author : Matthew BETTON (France / Basse-Normandie / Manche (50))
# Date : 04/12/2011
# Synopsis : Map User's Names and Twitter Names with PowerShell
#
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[String]$WebPage = \"www.sqlsaturday.com/70/networking.aspx\",
[Parameter(Mandatory=$false)]
[String]$FilePath = \".\TwittersNames.csv\",
[Parameter(Mandatory=$false)]
[String]$Name,
[Parameter(Mandatory=$false)]
[Switch]$Refresh
)
# This function gets Twitter Names and Real Names from a Web page
Function Get-TwitterUsersInfoFromWebPage(){
param(
[Parameter(Mandatory=$true)]
[String]$URI
)
Write-Verbose \"Function Get-TwitterUsersInfoFromWebPage\"
Write-Verbose \"Parameter '-URI' value : $URI\"
Write-Debug \"Downloading web page from $URI address ...\"
try{
$WebClient = New-Object System.Net.WebClient
$output = $WebClient.DownloadString($URI)
}
catch{
Write-Error \"An error has occured while downloading '$URI' web page\"
}
Write-Debug \"Setting regular expression to find user's twitter names and real names ...\"
$pattern = [regex]\"(<\s*font\s*size\s*=\s*[`\"']\s*3\s*[`\"']\s*>(.*?))(<\s*a\s*[^>]*?href\s*=\s*[`\"']\b[^>]*twitter.com/(.*?)[`\"'])\"
$match = $pattern.Matches($output)
$Tab = $null
$Tab = @()
Write-Debug \"Creating Twitter names informations array ...\"
foreach($info in $match){
$user = \"\" | select RealName, TwitterName
$user.RealName = $info.Groups[2].Value
$user.TwitterName = $info.Groups[4].Value
$Tab += $user
}
return $Tab
}
if(-not (Test-Path $FilePath) -or $Refresh){
Write-Debug \"CSV file $FilePath does not exist or is beeing refreshed ...\"
$TwitterInfos = Get-TwitterUsersInfoFromWebPage -URI $WebPage
$TwitterInfos | Export-Csv -Path $FilePath -NoTypeInformation
}
else{
Write-Debug \"CSV file $FilePath already exists ...\"
$TwitterInfos = Import-Csv -Path $FilePath -Delimiter \",\"
}
if($Name){
Write-Debug \"Fiding corresponding names (Twitter or/and real name(s)) to '*$Name*'\"
$FoundNames = $null
$FoundNames = @()
$FoundNames += $TwitterInfos | ?{$_.RealName -like \"*$Name*\"}
$FoundNames += $TwitterInfos | ?{$_.TwitterName -like \"*$Name*\"}
Write-Output $FoundNames | select RealName, TwitterName -Unique
}
<#
.SYNOPSIS
Map User's Names and Twitter Names with PowerShell.
.DESCRIPTION
The Get-UserTwitterNames.ps1 script gets twitter names and real names to a CSV file.
Also, this script can find twitter name or person's actual name (from CSV file)
.PARAMETER Webpage
Specifies the http web page where twitter names and real names can be found.
.PARAMETER FilePath
Specifies the CSV file path that contains twitter names and real names.
.PARAMETER Name
If specified, the script will find twitter names and/or real names corresponding to the name specified.
.PARAMETER refresh
Even if the CSV file already exists, the CSV file is made again, from the url reference.
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1
Description
The script retrieves Twitter names and real names from default http web page and sets informations in the default CSV file.
If CSV file already exists : it does nothing.
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1 -refresh
Description
The script retrieves Twitter names and real names from default http web page and sets informations in the default CSV file.
If CSV file already exists : the CSV file is built from scratch, so all informations are up to date...
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1 -name obb
Description
Displays all Twitter names and real names that are matching \"obb\" string.
If CSV file does not exist, it is created.
If CSV file already exists, it is not rebuilt.
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1 -name obb -refresh
Description
Displays all Twitter names and real names that are matching \"obb\" string.
If CSV file does not exist, it is created.
If CSV file already exists, it is refreshed from current HTTP web page.
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1 -FilePath D:\Temp\Twitters.csv -name obb
Description
If CSV file does not exist, it is created.
If CSV file already exists, it is not refreshed.
Then displays all Twitter names and real names that are matching \"obb\" string.
.EXAMPLE
C:\PS> .\Get-UserTwitterNames.ps1 -FilePath D:\Temp\Twitters.csv -name obb -refresh
Description
If CSV file does not exist, it is created.
If CSV file already exists, it is refreshed.
Then displays all Twitter names and real names that are matching \"obb\" string.
.OUTPUTS
Selected.System.Management.Automation.PSCustomObject
#>[/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.
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
avoid using aliases in a script. Especially one with regular expression ? and then the ? for Where-object is confusing. I like your error handling, your comment based help, and your parameters.
Connexion ou Créer un compte pour participer à la conversation.
- MAttew18
- Hors Ligne
- Membre junior
-
- Messages : 22
- Remerciements reçus 0
Connexion ou Créer un compte pour participer à la conversation.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Discussions générales
- 2011 Scripting Games : Advanced Event 7