Comment encoder en utf-8 un script.ps1 de powerShell ?

amaury.lecour Messages postés 1 Date d'inscription   Statut Membre Dernière intervention   -  
Jean20B Messages postés 1836 Date d'inscription   Statut Membre Dernière intervention   -
# Définit l'encodage de sortie du terminal en UTF-8
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# Définit l'emplacement par défaut
$emplacementParDefaut = "C:\wamp\www"

# Demande à l'utilisateur l'emplacement et le nom du dossier
$emplacement = Read-Host "Entrez l'emplacement du dossier (appuyez sur Entrée pour utiliser $emplacementParDefaut)"
if ([string]::IsNullOrWhiteSpace($emplacement)) {
    $emplacement = $emplacementParDefaut
}

$nomDossier = Read-Host "Entrez le nom de votre site"
if ([string]::IsNullOrWhiteSpace($nomDossier)) {
  Write-Host "Le nom du dossier ne peut pas être vide. Opération annulée." -ForegroundColor Red
  exit
}

$dossierComplet = Join-Path -Path $emplacement -ChildPath $nomDossier

# Vérifie si le dossier existe déjà
if (Test-Path -Path $dossierComplet) {
  Write-Host "Le dossier '$dossierComplet' existe déjà. Opération annulée." -ForegroundColor Red
  exit
}

# Crée les dossiers
New-Item -ItemType Directory -Path $dossierComplet | Out-Null
New-Item -ItemType Directory -Path (Join-Path -Path $dossierComplet -ChildPath "css") | Out-Null
New-Item -ItemType Directory -Path (Join-Path -Path $dossierComplet -ChildPath "js") | Out-Null

# Contenu des fichiers
$htmlContent = @"
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/style.css">
  <script src="js/date.time.js" defer></script>
  <title>Mon Site</title>
</head>
<body>
  <header>
    <div class="date"></div>
    <h1><a href="#">Titre du site</a></h1>
    <div class="time"></div>
  </header>
  <main>
    </main>
  <footer>
    &copy; Copyright modele (2023 - <span class="copy"></span>). Tous droits réservés
  </footer>
</body>
</html>
"@

$cssContent = @"
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Roboto', sans-serif;
  text-decoration: none;
}
::-webkit-scrollbar {
  display: none;
}
html {
  width: 100%;
}
body {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #222;
  color: #fff;
}
header {
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  border-bottom: 2px solid #fff;
}
header .date, header .time {
  margin-top: 5px;
  margin-left: 5px;
  font-style: italic;
}
header h1 {
  font-family: 'Operator Mono Lig Medium', monospace;
  font-style: italic;
  color: #fff;
}
header h1 a {
  font-family: inherit;
  font-style: inherit;
  font-weight: 400;
  color: inherit;
  text-align: center;
}
header h1 a:hover {
  opacity: 0.5;
}
header .time {
  margin-right: 5px;
}
main {
  margin-top: 40px;
  display: flex;
  justify-content: center;
  flex: 1;
}
footer {
  padding: 10px;
  display: flex;
  justify-content: center;
  font-family: 'Operator Mono Lig Medium', monospace;
  font-style: italic;
  color: #fff;
  border-top: 2px solid #fff;
}
footer span {
  font-family: 'Roboto', sans-serif;
  font-size: 1rem;
}
"@

$jsContent = @"
// Date, heure, copyright
const date = document.querySelector('.date')
const time = document.querySelector('.time')
const copy = document.querySelector('.copy')

// Date du jour
const toDay = new Date().toLocaleDateString('fr-FR', {
  weekday: "long",
  day: "numeric",
  month: "long",
  year: "numeric"
})

// Copyright
copy.textContent = new Date().getFullYear()

// Date
date.textContent = toDay

// Heure
const dspTime = () => {
  const now = new Date()
  let h = now.getHours()
  let m = now.getMinutes().toString().padStart(2, '0')
  let s = now.getSeconds().toString().padStart(2, '0')
  let a = h > 12 ? 'pm' : 'am'
  h = h % 12
  time.textContent = h + ':' + m + ':' + s + ' ' + a
}
dspTime()
setInterval(dspTime, 1000)
"@

# Écrit le contenu dans les fichiers avec l'encodage UTF-8
Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "index.html") -Value $htmlContent -Encoding UTF8
Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "css\style.css") -Value $cssContent -Encoding UTF8
Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "js\date.time.js") -Value $jsContent -Encoding UTF8

Write-Host "Le squelette de votre site a été créé avec succès dans le dossier '$dossierComplet' !" -ForegroundColor Green

Quand un mot comporte une lettre comme "é", dans le source index.html la lettre devient "

# Définit l'encodage de sortie du terminal en UTF-8

$OutputEncoding = [System.Text.Encoding]::UTF8

[Console]::InputEncoding = [System.Text.Encoding]::UTF8

# Définit l'emplacement par défaut

$emplacementParDefaut = "C:\wamp\www"

# Demande à l'utilisateur l'emplacement et le nom du dossier

$emplacement = Read-Host "Entrez l'emplacement du dossier (appuyez sur Entrée pour utiliser $emplacementParDefaut)"

if ([string]::IsNullOrWhiteSpace($emplacement)) {

    $emplacement = $emplacementParDefaut

}

$nomDossier = Read-Host "Entrez le nom de votre site"

if ([string]::IsNullOrWhiteSpace($nomDossier)) {

  Write-Host "Le nom du dossier ne peut pas être vide. Opération annulée." -ForegroundColor Red

  exit

}

$dossierComplet = Join-Path -Path $emplacement -ChildPath $nomDossier

# Vérifie si le dossier existe déjà

if (Test-Path -Path $dossierComplet) {

  Write-Host "Le dossier '$dossierComplet' existe déjà. Opération annulée." -ForegroundColor Red

  exit

}

# Crée les dossiers

New-Item -ItemType Directory -Path $dossierComplet | Out-Null

New-Item -ItemType Directory -Path (Join-Path -Path $dossierComplet -ChildPath "css") | Out-Null

New-Item -ItemType Directory -Path (Join-Path -Path $dossierComplet -ChildPath "js") | Out-Null

# Contenu des fichiers

$htmlContent = @"

<!DOCTYPE html>

<html lang="fr">

<head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="css/style.css">

  <script src="js/date.time.js" defer></script>

  <title>Mon Site</title>

</head>

<body>

  <header>

    <div class="date"></div>

    <h1><a href="#">Titre du site</a></h1>

    <div class="time"></div>

  </header>

  <main>

    </main>

  <footer>

    &copy; Copyright modele (2023 - <span class="copy"></span>). Tous droits réservés

  </footer>

</body>

</html>

"@

$cssContent = @"

* {

  margin: 0;

  padding: 0;

  box-sizing: border-box;

  font-family: 'Roboto', sans-serif;

  text-decoration: none;

}

::-webkit-scrollbar {

  display: none;

}

html {

  width: 100%;

}

body {

  width: 100%;

  min-height: 100vh;

  display: flex;

  flex-direction: column;

  background-color: #222;

  color: #fff;

}

header {

  padding: 10px;

  display: flex;

  justify-content: space-between;

  align-items: center;

  color: #fff;

  border-bottom: 2px solid #fff;

}

header .date, header .time {

  margin-top: 5px;

  margin-left: 5px;

  font-style: italic;

}

header h1 {

  font-family: 'Operator Mono Lig Medium', monospace;

  font-style: italic;

  color: #fff;

}

header h1 a {

  font-family: inherit;

  font-style: inherit;

  font-weight: 400;

  color: inherit;

  text-align: center;

}

header h1 a:hover {

  opacity: 0.5;

}

header .time {

  margin-right: 5px;

}

main {

  margin-top: 40px;

  display: flex;

  justify-content: center;

  flex: 1;

}

footer {

  padding: 10px;

  display: flex;

  justify-content: center;

  font-family: 'Operator Mono Lig Medium', monospace;

  font-style: italic;

  color: #fff;

  border-top: 2px solid #fff;

}

footer span {

  font-family: 'Roboto', sans-serif;

  font-size: 1rem;

}

"@

$jsContent = @"

// Date, heure, copyright

const date = document.querySelector('.date')

const time = document.querySelector('.time')

const copy = document.querySelector('.copy')

// Date du jour

const toDay = new Date().toLocaleDateString('fr-FR', {

  weekday: "long",

  day: "numeric",

  month: "long",

  year: "numeric"

})

// Copyright

copy.textContent = new Date().getFullYear()

// Date

date.textContent = toDay

// Heure

const dspTime = () => {

  const now = new Date()

  let h = now.getHours()

  let m = now.getMinutes().toString().padStart(2, '0')

  let s = now.getSeconds().toString().padStart(2, '0')

  let a = h > 12 ? 'pm' : 'am'

  h = h % 12

  time.textContent = h + ':' + m + ':' + s + ' ' + a

}

dspTime()

setInterval(dspTime, 1000)

"@

# Écrit le contenu dans les fichiers avec l'encodage UTF-8

Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "index.html") -Value $htmlContent -Encoding UTF8

Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "css\style.css") -Value $cssContent -Encoding UTF8

Set-Content -Path (Join-Path -Path $dossierComplet -ChildPath "js\date.time.js") -Value $jsContent -Encoding UTF8

Write-Host "Le squelette de votre site a été créé avec succès dans le dossier '$dossierComplet' !" -ForegroundColor Green

A voir également:

4 réponses

MPMP10 Messages postés 45181 Date d'inscription   Statut Membre Dernière intervention   17 369
 

Bonsoir,

Voir ici !

Forum CCM - Charte - Respect d'autrui

Respect d'autrui

Le forum du site CommentCaMarche met en relation des personnes issues de milieux différents, tendant vers un seul but : l'entraide informatique. Ainsi, toutes les personnes contribuant à cette entraide sont des utilisateurs volontaires et bénévoles.

Politesse

Il est donc indispensable d'utiliser le forum avec le plus grand respect vis-à-vis des utilisateurs. Chaque nouveau message devra donc être le plus courtois possible. La politesse se traduit tous les jours par l'utilisation de certains termes comme bonjour, au revoir, bienvenue, s'il vous plaît, ou merci, et par des attitudes spécifiques. Cette règle de conduite est valable dans la vie de chaque jour mais aussi sur CCM (CommentCaMarche).

Forum CCM - Charte - Respect d'autrui

https://www.google.com/search?client=firefox-b-e&q=Comment+encoder+en+utf-8+un+script.ps1+de+powerShell+%3F


0
PierrotLeFou
 

Est-ce que je peux donner un +10 à la réponse au lieu d'un +1 ? :)

0
PierrotLeFou
 

11

0
Jean20B Messages postés 1836 Date d'inscription   Statut Membre Dernière intervention   2 345
 

Bonjour,

Le script ps1 PowerShell doit être enregistré avec le codage de caractères UTF-8 avec BOM (UTF-8-BOM).

Par exemple dans Notepad++ :

0