scans.ps1

using module PowerShellGet
using module PackageManagement
using module SmbShare

# Setup variables and defaults
param (
    [string]$scanUser = 'scans',
    [string]$folderPath = 'C:\Scans',
    [string]$shareName = 'scans',
    [string]$description = 'Scanning Setup by PSP.'
)
$ENV:details = 'Loading...'
$domainJoined = (Get-CimInstance -Class Win32_ComputerSystem).PartOfDomain
$ProgressPreference = 'SilentlyContinue'
$VerbosePreference = 'SilentlyContinue'
$ErrorActionPreference = 'Continue'
$extras = @{}
if ($VerbosePreference -eq 'Continue' -and $Verbose -ne $true) {
    $extras['Verbose'] = $true
}
$extras['ErrorAction'] = $ErrorActionPreference

# Add required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Web

#Create a form for the loading text
$loadingForm = New-Object System.Windows.Forms.Form
$loadingForm.Text = "Loading..."
$loadingForm.Size = New-Object System.Drawing.Size(300,100)
$loadingForm.StartPosition = "CenterScreen"
$loadingForm.Topmost = $true
$loadingForm.FormBorderStyle = "FixedDialog"
$loadingForm.ControlBox = $false
$loadingForm.MaximizeBox = $false
$loadingForm.MinimizeBox = $false

$loadingLabel = New-Object System.Windows.Forms.Label
$loadingLabel.Location = New-Object System.Drawing.Size(10,10)
$loadingLabel.Size = New-Object System.Drawing.Size(280,20)
$loadingLabel.Text = "Loading, please wait..."
$loadingForm.Controls.Add($loadingLabel)

$loadingForm.Add_Shown({$loadingForm.Activate()})
$loadingForm.Show()

# Make sure nuget is installed without user intervention
Write-Debug "Checking for required modules..."
if (-not (Get-PackageProvider -Name NuGet @extras)) {
    Write-Debug "Installing NuGet package provider."
    Install-PackageProvider -Name NuGet -Force -Confirm:$false @extras
}

# Trust the PowerShell Gallery repository
Write-Debug "Checking for the PSGallery repository."
if (-not (Get-PSRepository -Name PSGallery @extras)) {
    Write-Debug "Registering the PSGallery repository."
    Register-PSRepository -Name PSGallery -SourceLocation https://www.powershellgallery.com/api/v2 -InstallationPolicy Trusted @extras
}
if ((Get-PSRepository -Name PSGallery).InstallationPolicy -ne 'Trusted') {
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted @extras
}

# Import the required modules
if (-not (Get-Module -Name PackageManagement -ListAvailable)) {
    Install-Module -Name PackageManagement -Force -Confirm:$false @extras
}
Import-Module -Name PackageManagement -Force @extras

if (-not (Get-Module -Name PowerShellGet -ListAvailable)) {
    Install-Module PowerShellGet -Force -Confirm:$false @extras
}
Import-Module -Name PowerShellGet -Scope Local -Force @extras


if (-not (Get-Module -Name WindowsCompatibility -ListAvailable)) {
    Install-Module WindowsCompatibility -Force -AllowClobber @extras
}
Import-Module WindowsCompatibility -Scope Local -Force @extras

Import-Module -Name SmbShare -Force @extras

# Import the Scans module
$moduleName = "scans"
$latestRelease = (Find-Module -Name $moduleName @extras | Sort-Object -Property Version -Descending | Select-Object -First 1).Version
$installedModule = Get-InstalledModule -Name $moduleName @extras
    
if ($installedModule -and $installedModule.Version -eq $latestRelease) {
    Write-Debug "Module '$moduleName' is already $latestRelease."
} else {
    if ($installedModule) {
        Write-Debug "Removing installed Module '$moduleName' current version $($installedModule.Version)."
        Uninstall-Module -Name $moduleName -Force @extras
    }
    Write-Debug "Installing Module '$moduleName' version $latestRelease."
    Install-Module -Name $moduleName -AllowClobber -Force -SkipPublisherCheck -Scope CurrentUser -RequiredVersion $latestRelease -Confirm:$false @extras
    Import-Module $moduleName -Force @extras
}

# Close the loading form
Write-Debug "Closing the loading form..."
$loadingForm.Close() | Out-Null

# Get user details
Write-Debug "Getting user details..."
$userDetails = Show-ScanningSetupForm -scanUser $scanUser -folderPath $folderPath -shareName $shareName -description $description

# Extract user details
Write-Debug "Extracting user details..."
$scanUser = $userDetails.scanUser
$scanPass = $userDetails.scanPass
$folderPath = $userDetails.folderPath
$shareName = $userDetails.shareName
$description = $userDetails.description

# Show the loading screen
Write-Debug "Showing the loading screen..."
$loadingScreen = New-LoadingForm
$loadingScreen.Form.Show()
$loadingScreen.Form.Topmost = $true

# Setup the scanning user
Write-Debug "Setting up the scanning user..."
Set-ScanUser -scanUser $scanUser -scanPass $scanPass -description $description
Hide-ScanUserFromLoginScreen -scanUser $scanUser

# Setup the scanning folder
Write-Debug "Setting up the scanning folder..."
New-ScanFolder -folderPath $folderPath

# Gathering the users and groups who will be given permission
Write-Debug "Gathering the users and groups who will be given permission..."
$users = @($Env:UserName, $scanUser, "Everyone")
if ($domainJoined) {
    $users += "Domain Users"
}

# Set the permissions on the scanning folder for each uer
Write-Debug "Setting the permissions on the scanning folder for each user..."
foreach ($user in $users) {
    Write-Debug "Setting permissions for $user..."
    Set-ScanFolderPermissions -folderPath $folderPath -username $user -setPermissions $true
}

# Setup the SMB share
Write-Debug "Setting up the SMB share..."
Set-ScansSmbShare -shareName $shareName -folderPath $folderPath -scanUser $scanUser

# Create a desktop shortcut
Write-Debug "Creating a desktop shortcut..."
New-DesktopShortcut -shortcutPath "C:\Users\Public\Desktop\Scans.lnk"

# Set the network settings
Write-Debug "Setting the network settings..."
Set-NetworkSettings -domainJoined $domainJoined -enableFileAndPrinterSharing $true -enablePasswordProtectedSharing $true

# Close the loading screen
Update-ProgressBar $loadingScreen "Finished!" 100
$loadingScreen.Form.Close() | Out-Null

$finished = New-LoadingForm $true $ENV:details
$finished.Form.ShowDialog() | Out-Null
if ($finished.Form -eq [System.Windows.Forms.DialogResult]::OK) {
    $finished.Form.Close() | Out-Null
    Exit
}