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($ignoreupdate=$false,$retrylogon)
{
    if ($retrylogon) {
        try{
            Remove-Item "C:\temp\ATWSuser.txt" -Force
            Remove-Item "C:\temp\ATWS.txt" -Force
        }catch{
            throw $Error[0]
        }

    }
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    if(($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) -eq $true)
    {
    Write-Output ('Running as Administrator')
    $Modules = "Autotask","WordDoc","PowerShellGet","ImportExcel","OCHAutotaskForm"
    foreach ($m in $Modules)
    {
        $t = (Get-InstalledModule -Name $m -ErrorAction SilentlyContinue).Version
        if ($t -eq $Null) {
            Write-Output ('Installing {0} Module' -f $m)
            Install-Module $m -Force
        }
        if($t -ne (Find-Module -Name $m).Version)
        {
            Write-Output ('Updating {0} Module' -f $m )
            Install-Module -Name $m -Force
        }
    }#foreach end

    # Skal ikke ha installasjonsfiller i modul mappen. krever admin rettigheter.
    # $imodules = $env:PSModulePath.Split(';') | ForEach-Object{Get-ChildItem $_ -ErrorAction SilentlyContinue }
    # if ('OCH' -in $imodules.Name)
    # {
    # #Write-Output 'yes'
    # $path = $imodules.Where({$_.Name -eq 'OCHAutotaskForm'}).FullName
    # Set-Location $path
    # }

    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"
    }

    # Skal ikke ha installasjonsfiller i modul mappen. krever admin rettigheter.
    # $version = (Get-InstalledModule OCHAutotaskForm).Version
    # if((test-path "$path\$version\Crypt") -eq $false)
    # {
    # Write-Output ('Creating folder {0}\Crypt' -f $path)
    # New-Item -ItemType Directory -Path "$path\$version\Crypt"
    # }
    if ((Test-Path "C:\temp\ATWSuser.txt") -ne $true)
    {
        $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.
    Write-Warning "You need to run Start-OCHAutotaskForm as a non administrator to continue"
    }#if end
    else
    {
        # if ($retrylogon -eq $true) {
        # try{
        # Remove-Item "C:\temp\ATWSuser.txt" -Force
        # $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
        # }catch{
        # throw $Error[0]
        # }
        # }#end if
        if($ignoreupdate)
        {
            $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 'C:\Temp'
        }
        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)
                    return
                }
                if((Get-InstalledModule -Name $m).Version -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 modules before continuing. Or run Start-OCHAutotaskForm -Ignoreupdate true or Start-MainDotPs1"
                exit
            }
            Write-Output "All required modules are updated"
            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 'C:\Temp'
            exit

        }
    }#else end
}#function end