Untitled

 avatar
unknown
powershell
3 years ago
3.8 kB
60
Indexable
Set-Location $PSScriptRoot

Import-Module .\Locals.psm1

# Check if running as administrator
if (-not (Is-Administrator)) {
    Write-Output "Script must be run as administrator."
    Exit-WithError
}
Write-Output "New script run."


$errored = $false



$Cred = (Get-DomainCredentials)
$LocalCred = (Get-LocalCredentials)
$targetVpn = (Get-TargetVpn)

if (-not (Has-NamedVpn $targetVpn)) {
    Out-Error "Target VPN `"$targetVpn`" must be deployed before running script."
    Exit-WithError
}

# Join VPN
Connect-Vpn $targetVpn $Cred.UserName $Cred.Password


# Rename machine
Write-Host "Checking machine name."
$newname = Get-NewComputerName
if (-not ((hostname) -eq $newname)) {
    if (Is-Error(Rename-Computer $newname -LocalCredential $LocalCred)) {
        Write-Host "Failed to rename computer. Will try again with System privileges after restart."
    } else {
        Write-Host "Renamed computer to $(hostname)."
    }
} else {
    Write-Host "Computer already renamed. Skipping."
}

# Add to domain
Write-Host "Checking domain."
$domain = (Get-TargetDomain)
if ((Get-CurrentDomain) -eq (Get-TargetDomain)) {
    Write-Host "Computer in domain $(Get-TargetDomain)."
} else {
    if (Set-ComputerDomain $domain $Cred) {
        Write-Host "Added computer to domain $domain."
    } else {
        Write-Host "Failed to add computer to domain $domain."
        $errored = $true
    }
}

$timezone = (Get-TargetTimeZone)
Write-Host "Setting timezone to `"$timezone`"."
# Set timezone
Set-TimeZone $timezone

$folder = (Get-LocalSetupFolder)
# Set up script to run after restart
if (-not $errored) {
    try { # Copy files to local folder so they can be run 
        
        Remove-Item -Path $folder -Recurse
        Write-Output "Creating powershell files."
        New-Item -Path $folder -ItemType Directory
        ## This is for scheduled tasks
        # Copy-Item -Path ".\OnStartup.cmd" -Destination $folder
        Copy-Item -Path ".\AfterRestart.ps1" -Destination $folder
        Copy-Item -Path ".\Locals.psm1" -Destination $folder
        Write-Output "Finished copying powershell files."
        
        ## This is to use "C:\...\Start Menu\StartUp\"
        # Write-Output "Copying BeforeLogin script."
        # Copy-Item -Path ".\BeforeLogin.cmd" -Destination (Get-StartupFolder)
        # Write-Output "Finished copying BeforeLogin script."
    } catch {
        Write-Host "Could not create files required to automatically delete user account. Please run with higher privileges or do this manually."
        Remove-Item -Path $folder -Recurse
        Exit-WithError
    }

    try {
        # Register-Scheduledtask -TaskName "Delete Dummy User" -TaskPath "\Event Viewer Tasks\" -Action (New-ScheduledTaskAction -Execute "$folder\OnStartup.cmd") -RunLevel Highest -User 'NT_AUTHORITY\SYSTEM' -Trigger (New-ScheduledTaskTrigger -AtStartup)
        # schtasks.exe /create /tn 'Event Viewer Tasks\Delete Dummy User' /ru SYSTEM /Sc ONSTART /tr "$folder\OnStartup.cmd" /RL HIGHEST
        
        $trigger = New-JobTrigger -AtStartup
        $option = New-ScheduledJobOption -RunElevated -WakeToRun -ContinueIfGoingOnBattery -StartIfOnBattery
        $script = "$folder\AfterRestart.ps1"
        Register-ScheduledJob -FilePath $script -Name "Delete Dummy User" -Trigger $trigger -ScheduledJobOption $option
    } catch {
        Write-Host "Could not create scheduled task with System privileges."
        $errored = $true
    }
}

if (-not $errored) {
    Write-Host "System will now restart. Please log in as domain administrator after system boots."
    Read-Host -Prompt "Press ENTER to continue..."
    Restart-Computer
} else {
    Exit-WithError
}


Editor is loading...