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([switch]$IgnoreUpdate=$false,[switch]$RetryLogon=$false,[switch]$CurrentUser,[switch]$InstallAsAdmin)
{
    #created new credentials
    if ($retrylogon) {
        try{
            New-OCHAutotaskFormLogonCredential
        }catch{
            throw $Error[0]
        }
    }#end if

    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"
    }
    if ((Test-Path "C:\temp\ATWSuser.txt") -ne $true)
    {
        Write-Warning "AtwsUser.txt not found. Run New-OCHAutotaskFormLogonCredential to create logon credentials"
        break
    }

    #checks if we are Admin or not
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    if(($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) -eq $true)
    {
        Write-Output ('Running as Administrator')
        if($InstallAsAdmin -eq $true){
            $Modules = "WordDoc","ImportExcel","OCHAutotaskForm"#,"Autotask"
            foreach ($m in $Modules)
            {
                $t = (Get-InstalledModule -Name $m -ErrorAction SilentlyContinue).Version
                if($t -ne (Find-Module -Name $m).Version)
                {
                    Write-Output ('Updating {0} Module' -f $m )
                    if($CurrentUser -eq $true){
                        Install-Module -Name $m -Force -Scope CurrentUser
                    }else{
                        Install-Module -Name $m -Force
                    }
                }
            }#foreach end
        }#end if
        if($RunasAdmin -eq $true){
            start-maindotps1 -RunningDir 'C:\Temp'
        }else{
            Write-Warning "You need to run Start-OCHAutotaskForm as a non administrator to launch the Form"

        }
    #Non Admin rights part
    }#if end
    else
    {
        if($ignoreupdate)
        {
            start-maindotps1 -RunningDir 'C:\Temp' #forøvrig samme path som er standard i Start-MainDotPs1
        }
        else
        {
            #sjekke modulversjon installerer som currentuser med mindre parameter er sagt, så skal den feile.
            $Modules = "Autotask","WordDoc","ImportExcel","OCHAutotaskForm"
            foreach ($m in $Modules)
            {
                if((Get-InstalledModule -Name $m -ErrorAction SilentlyContinue).Version -ne (Find-Module -Name $m).Version)
                {
                    if($InstallAsAdmin -ne $true){
                        Install-Module $m -Scope CurrentUser -SkipPublisherCheck -Force
                    }else{
                        Write-Warning ('Module: {0} needs updating. Run Start-OCHAutotaskForm -InstallAsAdmin as an acministrator to update' -f $m)
                    }
                }
            }

            start-maindotps1 -RunningDir 'C:\Temp'
            exit
        }
    }#else end
}#function end