######################################################################################
#
# Scope: localized windows versions use different names for the Administrator group. 
# This pohibits to login into LSA on these systems, because lsa.conf is setup with the US name
#			 	full_access_groups = Administrators
#
# this script will search for the correct name of the administrator group in the system and
# replaces the name of the group "Administrators" with the correct name in lsa.conf line
# 		"full_access_groups = Administrators"
#
#
# this script must run with Administrator rights to make the changes in LSA.conf
#
# localized versions of Windows need a change in lsa.conf to login correctly in LSA.
# change access group in lsa.conf in case a localized version of Windows Server is
# installed. the script will check "net localgroup" for the right admin group and will
# replace group Administrators by this group in lsa.conf for full access rights.
# if net.exe cannot be found in line 59, then use complete path (line 57).
# Localized versions to change are
# German
# French
# Spanish
# Russian
# Polish
#
# PowerShell version 0.4
# 2023/05/04 added verification that changes are already made and so no changes in lsa.conf necessary
# 2023/05/03 added verification that LSA is installed and LSA service stopped before the change
# and restarted after the change again.
# 2023/04/30 changed procedure to exchange admin group by using IO.File to read the file with all lines and without encoding changes.
# 2023/04/27 dietmar hinze
# added LSA service stop/start
# provided as-is.
#
# localisation for different countries Windows versions
# different names are used in different localization for the administrator group
# for US
# full_access_groups = Administrators
#
# This needs to be changed for localized versions below
# changes for full_access_groups
#
# for French 
# full_access_groups = Administrateurs
#
# for German
# full_access_groups = Administratoren
#
# for Spanish
# full_access_groups = Administradores
#
# for Polish
# full_access_groups = Administratorzy 
#
# for Russian
# full_access_groups = Administratory or 
# full_access_groups = ??????????????(doesn't work so far, needs different encoding)
#
##########################################################################################
#
# setup all local variables used in script
#
#
#######################################################################################
#
# Functions
#
# verify that LSA has been installed
function LSA-installed {
	process {
		$value = -1
		$InstalledSoftware = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" 
		foreach($obj in $InstalledSoftware){ 
			if ($obj.GetValue('DisplayName') -match 'LSI Storage Authority') { 
# test			if ($obj.GetValue('DisplayName') -match 'Aragthority') { 
				$LSA_found_name = $obj.GetValue('DisplayName') 
				$LSA_found_version = $obj.GetValue('DisplayVersion')
				$value = $LSA_found_version
			} 
			else { 
				# do nothing
				# write-host "LSA not found"
				#if ( $value -neq -1) 
				# $value = -1
			}
		} 
		#write-host "Found: $LSA_found_name - $LSA_found_version"
		#write-output $LSA_found_name,$LSA_found_version
		#write-output "value: $value"
	return $value
	}
}
#


######################
#
# check if LSA is been installed, by calling function LSA-installed
# -1: LSA not installed
# any number: LSA version
$lsa = LSA-installed
if ($LSA -eq -1) {
	Write-Host "LSI Storage Authority is not installed!"
	Write-Host "exit script"
	exit
}
Write-Host "LSA version: $lsa"
##############################

############################################
#
# create list of possible admin groups for differnt localized versions of Windows.
# then verify that one of these groups are listed in the localgroup and store the group name in localmember
# 
# create list with possible names for the administrator group
$adminList=@('Administrators', 'Administrateurs', 'Administratoren', 'Administradores', 'Administratorzy', 'Administratory')
# create list of localgroup members
$localgroup = (get-localgroup) -split "`r`n"

$localmember = ""
# search for name of localised admin in localgroup
foreach ( $admin in $adminlist  ) { 
	#write-host $admin 
	ForEach ($line in $($localgroup -split "`r`n"))
	{
		#Write-host "Admin: $admin, Line: $Line"
		#if ("$admin" -like "$line") {
		if ("$admin" -eq "$line") {
			Write-host "found $admin in localgroup"
			$localmember = $admin
		}
	}
}

#DEBUG write-host "member = $localmember"

# search for "Administrators" in lsa.conf an replace it with the localmember
# lsa.conf example full_access_groups = Administrators
# search for "full_access_groups"
# then take the string ("Administrators") after "=" and compare it with "Administrators"
# if yes, nothing has to be done, otherwise replace it with the localized version found before
#

#DEBUG $searchstr = "full_access_groups"
# 'Administrators' is default value set in lsa.conf
$searchgroupname = "Administrators"
$replacegroupname = $localmember
#$replacegroupname = "Administratores"

# C:\program files\LSI\LSIStorageAuthority\conf
$lsaconffilePath="$env:programfiles\LSI\LSIStorageAuthority\conf"
$lsaconffile="$env:programfiles\LSI\LSIStorageAuthority\conf\lsa.conf"
#DEBUG $lsaconffile=".\lsa.conf"
#DEBUG $tmpfile = ".\lsaconftmp.txt"
$tmpfile = "$lsaconffile" + ".$PID"
##$tmpfile = "$lsaconfile" + '(Get-Date -Format "~yyyyMMddHHmm~"))'

 
# check if lsa.conf file exit
# if lsa.conf doesn't exist exit script
# then verify that changes are already made in lsa.conf
if ( test-path $lsaconffile -pathtype Leaf ){
	Write-Host "$lsaconffile exists"
} else {
	write-host "$lsaconffile doesn't exist" 
	Write-Host "Script stopped."
	# pause
	exit
}
# verify if changes already have been made
# then do nothing
if (select-string -Path $lsaconffile  -Pattern "$searchstr = $replacegroupname" -quiet ) { 
	#DEBUG write-host "found" 
	Write-Host "Changes are already made in $lsaconffile."
	Write-Host "Exit script without changes."
	exit
}

# check if $tmpfile exists
# if it exists, delete it
if ( test-path $tmpfile -pathtype Leaf ){
	write-host "Temp file: $tmpfile exists, deleting it"
	remove-item $tmpfile
}

# stop LSA service
# check if LSA service is running and then stop it 
Get-Service -name LSAService| Where-Object -FilterScript {$_.Status  -eq "Running" } | Stop-Service

# read file in variable and then replace 'default admin group' by localized version
# here the file structure with empty lines as well as the encoding will stay the same 
# as the original file

Write-Host "Backup file: $tmpfile will be created."
 get-childitem -path $lsaconffile | where-object {
#	[IO.file]::ReadAllText($_)
	$content = [IO.file]::ReadAllText($_)
	if ($content.contains("$searchgroupname")) {
		#Copy-Item $_ ($_.fullname + $PID)
		Copy-Item $_ $tmpfile
		$newContent = ($content -replace "$searchgroupname", "$replacegroupname")
		write-output $newContent
		[IO.File]::WriteAllText($_, $newContent)
	}
}

# DEBUG test if files have been changed
# findstr Administrator $tmpfile
# findstr Administrator $lsaconffile


# restart LSA service
Get-Service -name LSAService| Where-Object -FilterScript {$_.Status  -eq "Stopped" } | Start-Service
Get-Service -name lsaservice


