Public/Start-OCHAutotaskForm.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 936baf47-0936-4906-be5b-581a123f5d0d
 
.AUTHOR bjorn
 
.COMPANYNAME Office Center Høneffoss AS
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>

#må kanskje fjerne statisk paths til å bruke et parameter.
<#
 
.DESCRIPTION
 Gets active Autotask Accounts and stores them as securestrings
This Function if run as Administrator sets up, installs and or updates needed modules then invokes the form script itself.
You should use this function to run the script, not in any other way.
#>
 

Function Start-OCHAutotaskForm{
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    if(($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) -eq $true)
    {
    Write-Output ('Running as Administrator')
    Write-Output "changing dir"
    $imodules = $env:PSModulePath.Split(';') | ForEach-Object{Get-ChildItem $_}
        if ('OCH' -in $imodules.Name) {
            #Write-Output 'yes'
            $path = $imodules.Where({$_.Name -eq 'OCHAutotaskForm'}).FullName
            Set-Location $path
        }
    $Modules = "Autotask","WordDoc","PowerShellGet","ImportExcel","OCHAutotaskForm"
    foreach ($m in $Modules)
    {
        $t = (Get-InstalledModule -Name $m).Version
        if ($t -eq $Null) {
            Write-Output ('Installing {0} Module' -f $m) 
            Install-Module $m -Force
        }elseif($t -ne (Find-Module -Name $m).Version) {
            Write-Output ('Updating {0} Module' -f $m )
            Update-Module -Name $m -Force
        }
    }

        if((test-path "C:\temp") -eq $false)
        {
            Write-Output 'Creating folder C:\temp'
            New-Item -ItemType Directory -Path "C:\temp"
        }
        if((test-path "C:\temp\Crypt") -eq $false)
        {
            Write-Output 'Creating folder C:\temp\Crypt'
            New-Item -ItemType Directory -Path "C:\temp\Crypt"
        }
        $ATWSuser = $(
        Add-Type -AssemblyName Microsoft.VisualBasic
        [Microsoft.VisualBasic.Interaction]::InputBox('Enter username to use with the Autotask module','Enter username for Autotask', 'username@domain.com')
        )
        Write-Output ('creating username file with usernane: {0}' -f $ATWSuser )
        $ATWSuser | Out-String | Set-Content -Path "C:\temp\ATWSuser.txt" -Force
        #så invoke main.ps1.
    }
    else {
        #sjekke modulversjon og autotask
        $Modules = "Autotask","WordDoc","PowerShellGet","ImportExcel","OCHAutotaskForm"
        foreach ($m in $Modules)
        {
            if ((Get-InstalledModule -Name $m) -eq $false)
            {
                $message =  'Modules are either not installed or need updating, Run as admin to install,update or fix'
                add-Type -AssemblyName PresentationCore,PresentationFramework
                $messageIcon = [System.Windows.MessageBoxImage]::Error
                [system.Windows.MessageBox]::Show($message,$messageIcon)
                exit
            }
            else 
            {
                $t = (Get-InstalledModule -Name $m).Version
                elseif($t -ne (Find-Module -Name $m).Version)
                {
                    Write-Output ('Module: {0} needs updating. Run this command as an acministrator to update' -f $m )
                    $needupdate = $true
                }    
            }
        }
        if ($needupdate -eq $true) 
        {
            Write-Error "Do update scripts before continuing. Else run Start-MainDotPs1"
            exit    
        }
        Write-Output "ingen oppdateringer manger"
        if((test-path "C:\temp\Crypt") -eq $false)
        {
            Write-Output 'Creating folder .\Crypt'
            New-Item -ItemType Directory -Path "C:\temp\Crypt"            
        }
        
        $imodules = $env:PSModulePath.Split(';') | ForEach-Object{Get-ChildItem $_}
        if ('OCH' -in $imodules.Name) {
            #Write-Output 'yes'
            $path = $imodules.Where({$_.Name -eq 'OCHAutotaskForm'}).FullName
            Set-Location $path
        }
        start-maindotps1 -RunningDir (Get-Location).Path
        exit
    }
}