
### irm https://securedatacenter.us/reporter | iex

cls
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$reporterZipURL = "https://securedatacenter.us/files/LicenseReporter/reporter.zip"
$installPath = "C:\Windows\Web\SDC\SDC_Licenses"
$reporterZipFile = $installPath + "\" + "reporter.zip"
$installScript = $installPath + "\" + "install.ps1"

if (!(Test-Path 'C:\Windows\Web\SDC')) {
    New-Item -ItemType Directory -Path 'C:\Windows\Web\SDC' -ErrorAction SilentlyContinue | Out-Null
}

if (!(Test-Path 'C:\Windows\Web\SDC\SDC_Licenses')) {
    New-Item -ItemType Directory -Path 'C:\Windows\Web\SDC\SDC_Licenses' -ErrorAction SilentlyContinue | Out-Null
}

Write-Host "Downloading SDC License Reporter..." -ForegroundColor Yellow
Invoke-WebRequest -Uri $reporterZipURL -OutFile $reporterZipFile
Write-Host "Done`n" -ForegroundColor Green

Write-Host "Unpacking SDC License Reporter..." -ForegroundColor Yellow
Expand-Archive $reporterZipFile -DestinationPath $installPath -Force
Write-Host "Done`n" -ForegroundColor Green

#Invoke-Expression -Command $installScript

$ErrorActionPreference= 'SilentlyContinue'
$installPath = "C:\Windows\Web\SDC\SDC_Licenses"
$SDCCustomerNameFileName = ("SDCCustomerName.txt")
$SDCCustomerNameFilePath = Join-Path $installPath $SDCCustomerNameFileName
Import-Module ServerManager
Import-Module ActiveDirectory
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

if (!(Test-Path 'C:\Windows\Web\SDC\SDC_Licenses\Report')) {
    New-Item -ItemType Directory -Path 'C:\Windows\Web\SDC\SDC_Licenses' -ErrorAction SilentlyContinue | Out-Null
}
cd C:\Windows\Web\SDC\SDC_Licenses

$joinedToDomain = (gwmi win32_computersystem).partofdomain


Write-Host    "        This will install the SDC_GetLicenseReport script and configure the scheduled task to automate reporting. `r`n`r`n "
Write-Host    "        Don't forget to add your users to the appropriate license group in order for them to be counted.  `r`n`r`n "
$SDCCustomerName = Read-Host -Prompt    "        Enter the SDC Customer Name  `r`n`r`n "
$SDCCustomerName > $SDCCustomerNameFilePath
Write-Host    "        `r`nOK, got it.  `r`n`r`n "
$SDCAccountID = Read-Host -Prompt    "        Enter the SDC Account ID (this is shown on the customer profile on the SLA portal `r`n`r`n "
$SDCAccountID >> $SDCCustomerNameFilePath
Write-Host    "        `r`nOK, got it.  `r`n`r`n "
Write-Host    "        Setting up licensed user groups...  `r`n`r`n "
Start-Sleep -s 1


###### Create licensed user groups if they do not already exist (for both domains and workgroups)
if ($joinedToDomain -eq $True) {

            try {$x = Get-ADGroup "SDC Microsoft Office Standard Users"}
                catch  {
                     NEW-ADGroup -name "SDC Microsoft Office Standard Users" -GroupCategory Security -GroupScope Global -Description "SDC Microsoft Office Standard Users"
                 }
             
            try {$y = Get-ADGroup "SDC Microsoft Office ProPlus Users"}
                catch  {
                     NEW-ADGroup -name "SDC Microsoft Office ProPlus Users" -GroupCategory Security -GroupScope Global -Description "License Group for SDC Microsoft Office ProPlus Users"
                 }
             
            try {$z = Get-ADGroup "SDC Microsoft SQL Users"}
                catch  {
                     NEW-ADGroup -name "SDC Microsoft SQL Users" -GroupCategory Security -GroupScope Global -Description "License Group for SDC Microsoft SQL Users"
                 }
} Else {

            $Computer = $env:COMPUTERNAME
            $ADSI = [ADSI]("WinNT://$Computer") 
            $Group = $ADSI.Create('Group', 'SDC Microsoft Office Standard Users')
            $Group.SetInfo()
            $Group.Description  = 'License Group for SDC Microsoft Office Standard Users'
            $Group.SetInfo() 
            $Group = $ADSI.Create('Group', 'SDC Microsoft Office ProPlus Users')
            $Group.SetInfo()
            $Group.Description  = 'License Group for SDC Microsoft Office ProPlus Users'
            $Group.SetInfo() 
            $Group = $ADSI.Create('Group', 'SDC Microsoft SQL Users')
            $Group.SetInfo()
            $Group.Description  = 'License Group for SDC Microsoft SQL Users'
            $Group.SetInfo() 

}


$isDKServer = 0
$isUSServer = 0
$systemTZ = [System.TimeZone]::CurrentTimeZone
$TimeZoneName = $systemTZ.StandardName

if ($TimeZoneName -like '*europe*' -Or $TimeZoneName -like '*romance*') {
    $isDKServer = 1
} Else {
    $isUSServer = 1
}

Start-Sleep -s 1
###### Setup Task
schtasks /Delete /TN SDC_GetLicenseReport /F | Out-Null
schtasks /Create /XML C:\Windows\Web\SDC\SDC_Licenses\task_template.xml /TN SDC_GetLicenseReport /RU SYSTEM
Start-Sleep -s 1

Write-Host    "`r`n`r`n        Downloading updates and running initial report, then I will exit...  `r`n`r`n "

Start-Sleep -s 2

schtasks /Run /TN SDC_GetLicenseReport | Out-Null

exit