$global:images_drive = "unknown" #Function - Read setting from ini file #https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/ #Usage => $iniContent = Get-IniContent "path\to\ini\file" #Usage => iniContent["section"].value function Get-IniContent ($FilePath) { $ini = @{} switch -regex -file $FilePath { "^\[(.+)\]" # Section { $section = $matches[1] $ini[$section] = @{} $CommentCount = 0 } "^(;.*)$" # Comment { $value = $matches[1] $CommentCount = $CommentCount + 1 $name = "Comment" + $CommentCount $ini[$section][$name] = $value } "(.+?)\s*=(.*)" # Key { $name,$value = $matches[1..2] $ini[$section][$name] = $value } } return $ini } #Function - Read config settings from config.ini into global vars function read_share_config { $iniContent = Get-IniContent "x:\imaging\config.ini" $global:server = $iniContent["share"].server $global:share = $iniContent["share"].share $global:domain = $iniContent["share"].domain $global:username = $iniContent["share"].username $global:password = $iniContent["share"].password } #Function - Map network share to z: drive function map_network_share { read_share_config if ($server -match "prompt") { $status="invalid" do { $server=Read-Host -Prompt "Server hosting psimage share" $server=$server -replace '\s', '' if ([string]::IsNullOrWhiteSpace($server)) { Write-Output "Invalid server address. Try again." $status="invalid" } elseif (-Not (Test-Connection "$server" -Count 1 -Quiet)) { Write-Output "Cannot connect to $server. Try again." $status="invalid" } else { $status="valid" break } } while ($status -match "invalid") } if ($share -match "prompt") { net use /delete * | Out-Null $share=Read-Host -Prompt "Share" $domain=Read-Host -Prompt "Domain" $username=Read-Host -Prompt "Username" $password=Read-Host -Prompt "Password" map_network_share 2>&1 | Out-Null while (-Not (Test-Path "$images_drive\$script_folder\PSImage.ps1")) { Write-Output "Cannot connect to share with the credentials you have specified. Please try again." $share=Read-Host -Prompt "Share" $domain=Read-Host -Prompt "Domain" $username=Read-Host -Prompt "Username" $password=Read-Host -Prompt "Password" map_network_share 2>&1 | Out-Null } } else { net use /delete * | Out-Null net use z: \\$server\$share /user:$domain\$username $password } } #Function - Get drive letter that contains images folder, e.g. E: function get_drive_letter { $letter = (get-psdrive |where {$_.Root -match ":"} |% {if (Test-Path ($_.Root + "images")){$_.Root}}).Substring(0,2) return $letter } #Function - Authenticate against password.txt function authenticate { $passwd_path="X:\imaging\password.txt" $psi_password=Get-Content $passwd_path Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $width=(([system.windows.forms.screen]::primaryscreen).Bounds).Width $height=(([system.windows.forms.screen]::primaryscreen).Bounds).Height $insert_width=($width/2)-125 $insert_height=($height/2)-10 $form = New-Object System.Windows.Forms.Form $form.Text = 'PSImage Password' $form.Size = New-Object System.Drawing.Size(550,400) $form.StartPosition = 'CenterScreen' $okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(($insert_width+87),($insert_height+30)) $okButton.Size = New-Object System.Drawing.Size(75,23) $okButton.Text = 'OK' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okButton $form.Controls.Add($okButton) $label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(($insert_width),($insert_height-25)) $label.Size = New-Object System.Drawing.Size(250,20) $label.Text = 'Enter password for PSImage' $label.TextAlign = "TopCenter" $form.Controls.Add($label) $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point($insert_width,$insert_height) $textBox.Size = New-Object System.Drawing.Size(250,20) $label.TextAlign = "MiddleCenter" $form.Controls.Add($textBox) $form.Topmost = $true $form.TopLevel = $true $form.WindowState= 'Maximized' $form.MaximizeBox = $False $form.MinimizeBox = $False $form.ControlBox = $False $form.FormBorderStyle = "None" $form.Add_Shown({$textBox.Select()}) do { $textBox.Text="" $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { } } until ($textBox.Text -match "$psi_password") } if (Test-Path "X:\imaging\password.txt") {authenticate} $iniContent1 = Get-IniContent "x:\imaging\config.ini" $global:mode = $iniContent1["admin"].mode $global:script_path = $iniContent1["path"].script_path $global:script_folder = $script_path.split("\")[1] #If mode=usb then scan drives to get drive letter with images folder. #Write images.drv file with drive letter inside for use by PSimage.ps1. if ($mode -match "usb") { $images_drive = get_drive_letter Add-Content x:\imaging\images.drv "$images_drive" } else { $images_drive = "Z:" map_network_share } copy $images_drive\$script_folder\*UEFI.txt x:\imaging\ copy $images_drive\$script_folder\*BIOS.txt x:\imaging\ copy $images_drive\$script_folder\PSimage.ps1 x:\imaging\ #If mode=usb then replace original drive letter in config.ini with new drive letter from volume scan for images folder. if (Test-Path x:\imaging\images.drv) { $iniContent2 = Get-IniContent "x:\imaging\config.ini" $path = $iniContent2["path"].image_path $old_drive = ($path.Split("\")[0]) ((Get-Content -path x:\imaging\config.ini -Raw) -replace $old_drive,$images_drive) | Set-Content -Path x:\imaging\config.ini } Set-Location $PSScriptRoot .\PSImage.ps1