|
param( |
|
[Parameter(Mandatory=$true)] |
|
[ValidateNotNullOrEmpty()] |
|
[string]$targetComputer = $(throw "targetComputer is mandatory, please provide a value."), |
|
[string]$PythonInstallPath = “$env:LOCALAPPDATA\Programs\Python\Python35”, |
|
[string]$tz = "Eastern Standard Time" |
|
) |
|
|
|
Write-Progress -Activity "Deploying Home Assistant" -Status 'Connecting to remote computer..'; |
|
|
|
$s = New-PSSession -ComputerName $targetComputer |
|
if(-not($s)) |
|
{ |
|
Write-Warning "$targetComputer inaccessible!" |
|
$trustedHosts = Get-Item wsman:\localhost\Client\TrustedHosts |
|
$trustedHosts = $trustedHosts.Value -split ',' |
|
if($trustedHosts -notcontains $targetComputer) { |
|
$user = [Security.Principal.WindowsIdentity]::GetCurrent(); |
|
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) |
|
if($isAdmin){ |
|
Set-Item WSMan:\LocalHost\Client\TrustedHosts $targetComputer -Concatenate |
|
} else { |
|
Write-Warning "Could not add ${targetComputer} to TrustedHosts`nRun this script as administrator to auto-configure TrustedHosts" |
|
Write-Warning "Alternatively run the following in an administrator powershell shell: Set-Item WSMan:\LocalHost\Client\TrustedHosts $targetComputer -Concatenate" |
|
write-host -nonewline "Continue anyway? (Y/N) " |
|
$response = read-host |
|
if ( $response -ne "Y" ) { exit } |
|
} |
|
} |
|
$s = New-PSSession -ComputerName $targetComputer -Credential ~\Administrator |
|
if(-not($s)) { |
|
Write-Warning "$targetComputer still inaccessible!" |
|
Write-Warning "Is winrm configured? Run: winrm quickconfig" |
|
exit |
|
} |
|
} |
|
|
|
#Copy-Item -ToSession $s -Path $PythonInstallPath -Destination “C:\” -Force -Recurse # This takes a while |
|
$tmp = New-TemporaryFile | Rename-Item -NewName { $_ -replace 'tmp$', 'zip' } –PassThru; Compress-Archive -Path $PythonInstallPath\* -CompressionLevel Fastest -Destination $tmp.FullName -Force |
|
Copy-Item -ToSession $s -Path $tmp.FullName -Destination “C:\Windows\Temp\pythonInstall.zip” -Force # This takes a while |
|
Invoke-Command -Session $s -Scriptblock {Expand-Archive -Path C:\Windows\Temp\pythonInstall.zip -DestinationPath C:\Python\Python35 -Force} |
|
Invoke-Command -Session $s -Scriptblock {Remove-Item C:\Windows\Temp\pythonInstall.zip} |
|
|
|
Write-Progress -Activity "Deploying Home Assistant" -CurrentOperation 'Configuring...' -Status 'Configure Python Install'; |
|
|
|
# Add the Python folders to the path environment variable and persist: |
|
Invoke-Command -Session $s -Scriptblock {$env:Path += “;C:\Python\Python35;C:\Python\Python35\Scripts”; setx PATH $env:Path /M} |
|
|
|
$pTest = Invoke-Command -Session $s -Scriptblock {python --version} |
|
if($pTest -notlike "Python 3.5.*") {Write-Warning "Something went wrong, Python not found"; exit} |
|
|
|
# Upgrade pip |
|
Invoke-Command -Session $s -Scriptblock {python -m pip install --upgrade pip} |
|
|
|
# Install HA |
|
Write-Progress -Activity "Deploying Home Assistant" -CurrentOperation 'Installing..' -Status 'Installing Home Assistant'; |
|
Invoke-Command -Session $s -Scriptblock {pip3 install homeassistant} |
|
|
|
# Allow HA through firewall |
|
Write-Progress -Activity "Deploying Home Assistant" -CurrentOperation 'Configurating..' -Status 'Configure Firewall'; |
|
Invoke-Command -Session $s -Scriptblock {netsh advfirewall firewall add rule name=”HA Web” dir=in action=allow protocol=TCP localport=8123} |
|
|
|
# Allow access to Files & Remote Managment & Time Zone |
|
Invoke-Command -Session $s -Scriptblock {Import-Module NetSecurity} |
|
Invoke-Command -Session $s -Scriptblock {Set-NetFirewallRule -DisplayGroup “File And Printer Sharing” -Enabled True} |
|
Invoke-Command -Session $s -Scriptblock {Set-NetFirewallRule -DisplayGroup “Remote Service Management” -Enabled True} |
|
Invoke-Command -Session $s -Scriptblock {Set-NetFirewallRule -DisplayGroup “Remote Event Log Management” -Enabled True} |
|
Invoke-Command -Session $s -Scriptblock {tzutil /s $tz} -ArgumentList $tz |
|
|
|
# Create config directory (HA won't do it for you) |
|
Invoke-Command -Session $s -Scriptblock {New-Item -ItemType Directory -Force -Path C:\ProgramData\.homeassistant | Out-Null} |
|
|
|
|
|
Write-Progress -Activity "Deploying HA" -CurrentOperation 'Starting..' -Status 'Start Home Assistant'; |
|
# Start HA via Scheduled Task |
|
|
|
function Invoke-PrepareScheduledTask |
|
{ |
|
$taskName = "HomeAssistant" |
|
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue |
|
if ($task -ne $null) |
|
{ |
|
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false |
|
} |
|
|
|
# Argument could be '-m homeassistant --config C:\ProgramData\.homeassistant' but it prevents Stop-ScheduledTask from working |
|
$action = New-ScheduledTaskAction -Execute 'C:\Python\Python35\python.exe' -Argument 'C:\Python\Python35\lib\site-packages\homeassistant\__main__.py --config C:\ProgramData\.homeassistant --runner' |
|
$trigger = New-ScheduledTaskTrigger -AtStartup |
|
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8 -ExecutionTimeLimit 0 |
|
$settings.CimInstanceProperties['MultipleInstances'].Value=3 |
|
|
|
$principal = New-ScheduledTaskPrincipal -UserId "LOCALSERVICE" -LogonType ServiceAccount -RunLevel Limited |
|
|
|
$definition = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Description "Run $($taskName) at startup" |
|
|
|
Register-ScheduledTask -TaskName $taskName -InputObject $definition |
|
|
|
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue |
|
|
|
if ($task -ne $null) |
|
{ |
|
Write-Output "Created scheduled task: '$($task.ToString())'." |
|
} |
|
else |
|
{ |
|
Write-Output "Created scheduled task: FAILED." |
|
exit |
|
} |
|
} |
|
|
|
# Create Scheduled Task |
|
Invoke-Command -Session $s -Scriptblock ${function:Invoke-PrepareScheduledTask} |
|
|
|
Invoke-Command -Session $s -Scriptblock {Start-ScheduledTask HomeAssistant} |
|
|
|
Write-Host "`n`nHome Assistant deployed!` Point your broswer to:" |
|
Write-Host "http://${targetComputer}:8123" -foregroundcolor "green" |
|
Write-Host "`nConfiguration files are located:" |
|
Write-Host "\\${targetComputer}\C$\ProgramData\.homeassistant" -foregroundcolor "green" |
|
Write-Host "`n First run make take a few minutes to respond" -foregroundcolor "Yellow" |
|
|
|
Remove-PSSession -Session $s |