Untitled

mail@pastecode.io avatar
unknown
powershell
2 years ago
1.6 kB
31
Indexable
Never
cd $PSScriptRoot
Import-Module .\Locals.psm1
New-Item -Path (Get-LocalSetupFolder) -Name "STARTED" #debug to make sure the script is running at all
function Get-IsDone() {
    $needsRestart = $false

    # Check computer name.
    If (-not((hostname) -eq (Get-NewComputerName))) {
        # Retry with system privileges
        Write-Host "Hostname not set. Retrying with System privileges."
        Rename-Computer (Get-NewComputerName) -LocalCredential (Get-LocalCredentials)
        $needsRestart = $true
    }
    
    # Check joined domain.
    if (-not (Is-DomainSet)) {
        # Retry
        Write-Host "Domain not set. Retrying with System privileges."
        Set-ComputerDomain (Get-TargetDomain) (Get-DomainCredentials)
        $needsRestart = $true
    }

    if ($needsRestart) {
        New-Item "C:\Users\User\Desktop\FIRSTTIMEFAILED" #debug
        exit
    }
}

Get-IsDone

$credentials = (Get-DomainCredentials)
rasdial (Get-TargetVpn) $credentials.Username $credentials.Password

# Then delete the "User" user account
$badUsers = Get-CimInstance -ClassName win32_userprofile | Where-Object { 
    $splitPath =  $_.LocalPath.Split('\')
    ($splitPath[-2] -eq "Users") -and (-not ($splitPath[-1] -eq "Administrator"))
}
$badUsers | ForEach-Object {
    Remove-LocalUser -Name "$($_.LocalPath.split('\')[-1])" -Confirm:$false
    Remove-CimInstance
}

Remove-Item -Path ".\Locals.psm1" -Force

# Finally delete the task that launched this script.
Unregister-ScheduledJob -TaskName 'Delete Dummy User' -Confirm:$false -Force