param(
   [string] $msiName,
   [string] $mstName,
   [string] $scriptName
)

#Write-Output "msiName: $msiName"
#Write-Output "mstName: $mstName"
#Write-Output "scriptName: $scriptName"

if($scriptName -like "*.vbs")
{
#Write-Host "It is a vbs configuration"
}
elseif($scriptName -like "*.ps1")
{
#Write-Host "It is a ps1 configuration"
}
else
{
Write-Host 'Invalid script to configure. this script is only for visual basic or powershell configuration. Ended!'
#Read-Host 'Press Enter key to End the process'
exit(1)
}

$error.Clear()
GET-Item .\$msiName

if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Could not found msi file, Ended!"
#Read-Host 'Press Enter key to End the process'
exit(1)
}

$error.Clear()
GET-Item .\$mstName

if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Could not found mst file, Ended"
#Read-Host 'Press Enter key to End the process'
exit(1)
}

$error.Clear()
GET-Item .\$scriptName

if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Could not found vbs/ps1 file, Ended"
#Read-Host 'Press Enter key to End the process'
exit(1)
}

$error.Clear()
$GPO_Name = Read-Host 'Enter the name of the Group Policy to be created '

$guid = ""
$gpo = New-GPO -Name $GPO_Name -Comment "Desktop Central Agent Installer (Visual Basic)"
Start-Sleep -s 5
$guid = $gpo.id.ToString().ToUpper()
$domain = Get-ADDomain
$forest = $domain.forest

$domainName = $domain -Split "," | ? {$_ -like "DC=*"}
$domainName = $domainName -join "."-replace("DC=","")

#Write-Output "domain name:$domainName"
#Write-Output "forest name:$forest"
#Write-Output "domain:$domain"

if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Ended!!!"
#Read-Host 'Press Enter key to End the process'
exit(1)
}

#Write-Host "  "
#Write-Host "Group Policy Created Successfully"
#Write-Host "GUID : $guid"
#Write-Host "group Policy Name : $GPO_Name"
#Write-Host "Domain : $domain"
#Write-Host "domainName : $domainName"

$error.Clear()
md "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup"
if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Ended!!!"
#Read-Host 'Press Enter key to End the process'
exit(1)
}
md "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Shutdown"

$error.Clear()
Copy-Item .\$scriptName "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup"
if($Error[0])
{
Write-Host "$Error[0]"
Write-Host "Ended!!!"
#Read-Host 'Press Enter key to End the process'
exit(1)
}
Copy-Item .\$msiName "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup"
Copy-Item .\$mstName "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup"

#Write-Host ".msi, .mst and .vbs copied successfylly to the sysvol location"

if($scriptName -like "*.vbs")
{
#Write-Host 'It is a vb script configuration'

$scripts = @"
[Startup]
0CmdLine=\\$domainName\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup\$scriptName
0Parameters=$msiName $mstName
"@

$vbfilename = "$env:SystemRoot\SYSVOL\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\scripts.ini"
$scripts | Out-File $vbfilename -Encoding unicode
$vbfile = Get-Item $vbfilename -force
$vbfile.attributes="Hidden"
}
elseif($scriptName -like "*.ps1")
{
#Write-Host 'It is a ps1 script configuration'

$pshellscript = @"
[Startup]
0CmdLine=\\$domainName\SysVol\$domainName\Policies\{$guid}\Machine\Scripts\Startup\PSInstallAgent.ps1
0Parameters=""$msiName" "$mstName""
"@

#0Parameters=""DesktopCentralAgent.msi" "DesktopCentralAgent.mst""

$psfilename = "C:\Windows\SYSVOL\sysvol\$domainName\Policies\{$guid}\Machine\Scripts\psscripts.ini"
$pshellscript | Out-File $psfilename -Encoding unicode
$psfile = Get-Item $psfilename -force
$psfile.attributes="Hidden"

}

$gptini = @"
[General]
Version=0
displayName=New Group Policy Object
"@

$gptinifilename = "$env:SystemRoot\SYSVOL\sysvol\$domainName\Policies\{$guid}\GPT.INI"
$gptini | Out-File $gptinifilename -Encoding utf8

#Write-Host "SYSVOL has updated successfully..."

$gPCMachineExtensionNames = "[{42B5FAAE-6536-11D2-AE5A-0000F87571E3}{40B6664F-4972-11D1-A7CA-0000F87571E3}]"

$adgpo = ([adsisearcher]"(&(objectCategory=groupPolicyContainer)(name={$guid}))").FindAll().Item(0)
$gpoentry = $adgpo.GetDirectoryEntry()
$gpoentry.Properties["gPCMachineExtensionNames"].Value = $gPCMachineExtensionNames
$gpoentry.Properties["versionNumber"].Value = "2"
$gpoentry.CommitChanges()

$Filepath = "$env:SystemRoot\SYSVOL\sysvol\$domainName\Policies\{$guid}"
(Get-Content $FilePath\GPT.INI).replace('0' , '2') | Set-Content $FilePath\GPT.INI

#Write-Host "CSE has updated syccessfully..."

Write-Host "  "
Write-Host "Group policy $GPO_Name has been created successfully..."


$GPO_MakeLink = 
{

Write-Host "  "
$distinguishedName = Read-Host 'Enter the distinguished name '

$error.Clear()

New-GPLink -Name "$GPO_Name" -Target $distinguishedName -LinkEnabled Yes

if($Error[0])
{
Write-Host "Error is $Error[0]"
}

else
{
Write-Host "Link has been created Successfully with $distinguishedName"
}

Write-Host "  "
$choice = Read-Host 'Do you want to link this GPO with another OU/Domain? <y/n> '
if($choice -eq 'y')
{
&$GPO_MakeLink
}
else
{
Write-Host "Ended!!!"
#Read-Host 'Press Enter key to End the process'
exit(1)
}

}
&$GPO_MakeLink