Passer au contenu principal

Nommage du poste

<#
	.NOTES
	===========================================================================
	 Created on:   	10/06/2020 12:00
	 Created by:   	LeNuageux
	 Organization: 	LeNuageux-SARL
	 Filename:     	01_Name.ps1
	===========================================================================
	.DESCRIPTION
		Donne un nouveau nom au poste
#>
# - Nettoyage terminal
	Clear-Host

# - Initialisation des variables

# - Recuperer l'adresse IP du poste
	# On selectionne l'adresse IP sur l'interface réseau ayant un accès à internet
	$ip4 = $(Get-NetIPConfiguration | Where-Object { $_.NetProfile.IPv4Connectivity -eq 'Internet' }).IPV4Address[0].IPAddress
	Write-Host "IP machine: $ip4" -ForegroundColor "Green"


# - Recuperer le nom actuel du poste
	$Nom_Machine = $Env:COMPUTERNAME
	if ($Nom_Machine -like "LNG*")
	{
		Write-Host "le nom du poste est $NomPC" -ForegroundColor "Green"
		# - Activation du lancement automatique du script au démarrage du poste
		New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "LNG_Configuration_Systeme" -PropertyType String -Value "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy Unrestricted -File C:\Setup\02_domain.ps1" -Force

		# - Appliquer le nom du poste et redémarrer
		Rename-Computer -NewName "$Nom_Machine" -Force
		Restart-Computer -Force
	}
	else
	{
		Write-Host "Determination du nom du poste via DNS, PHP ou par entrée du technicien..." -ForegroundColor "Red"
	}



# - Récupère le nom du poste par recherche inverse via DNS
	if ($Nom_Machine -notlike "LNG*")
	{
		$Nom_Machine_DNS = Resolve-DnsName -Name $IP4 | Select-Object -ExpandProperty "NameHost"
		if ($Nom_Machine_DNS -notlike "LNG*")
		{
			Write-Host "Nous n'avons pas trouvé de correspondance DNS pour ce poste !" -ForegroundColor "Red"
		}
		else
		{
			$Nom_Machine = $Nom_Machine_DNS
		}

	}

# - Fichier PHP sur serveur fog
	if ($Nom_Machine -notlike "LNG*")
		{
			Write-Host "Recherche du nom du poste via le serveur web de déploiement" -ForegroundColor "Red"
			[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
			$wc = New-Object system.Net.WebClient;
			$wget_Name = $wc.downloadString("http://10.31.7.11/Name.php");
			$Nom_Machine_WEB = $wget_Name.Trim();
			$Nom_Machine = $Nom_Machine_WEB

		}
    
# - Demande au technicien si nom non trouvé
	while (($Nom_Machine -notlike "LNG*") -and ($Nom_Machine -notlike "PREP*"))
		{
			Write-Host "Entrez un nom de poste LNG/PREP valide" -ForegroundColor Red
			$Nouveau_Nom_Manuel= Read-Host "Nom du poste non trouvé, Entrez un nom de machine"
			$Nom_Machine = $Nouveau_Nom_Manuel
        }

# - Annulation de mise en domaine si c'est pour preparer un poste
	if ($Nom_Machine -like "PREP*")
	{
		Write-Host "le nom du poste est $NomPC" -ForegroundColor "Green"
	}
	else
	{
		Write-Host "Ce pc ne sera pas dans le domaine car c'est une préparation de poste !" -ForegroundColor "Green"
		# - Appliquer le nom du poste et redémarrer
		Rename-Computer -NewName "$Nom_Machine" -Force
		Restart-Computer -Force
	}

# - Activation du lancement automatique du script au démarrage du poste
    New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "IUT_Configuration_Systeme" -PropertyType String -Value "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy Unrestricted -File C:\Setup\02_domain.ps1" -Force

# - Appliquer le nom du poste et redémarrer
	Rename-Computer -NewName "$Nom_Machine" -Force
	Restart-Computer -Force