Cyber_Profile.psm1
#region Cyber Module List # Prepare Cyber Module lists. Set-Variable -Option "Constant" -Name "CYBER_MODULE_LIST" -Value ( "Hyper-V.Tools", "DockerCompletion", "NtObjectManager", "posh-git" ) Set-Variable -Option "Constant" -Name "CYBER_MODULES_PRESENT" -Value ( Get-Module -ListAvailable -Name $CYBER_MODULE_LIST | Select-Object -ExpandProperty "Name" -Unique ) Set-Variable -Option "Constant" -Name "CYBER_MODULES_MISSING" -Value ( $CYBER_MODULE_LIST | Where-Object -FilterScript { $_ -notin $CYBER_MODULES_PRESENT } ) # Import modules. if ($null -ne $CYBER_MODULES_PRESENT) { $CYBER_MODULES_PRESENT | Import-Module -Global } # Notify about missing cyber modules, if there are any. if ($null -ne $CYBER_MODULES_MISSING) { Write-Warning -Message "The following cyber modules are missing: $CYBER_MODULES_MISSING.`nYou can install them using the Install-CyberModules cmdlet." } #endregion #region PowerShell settings $PSDefaultParameterValues = @{ "Format-Table:AutoSize" = $true } #endregion #region PSReadLine settings Set-PSReadLineOption -PredictionViewStyle ListView #endregion #region posh-git settings if ($null -ne (Get-Module -Name "posh-git")) { $GitPromptSettings.DefaultPromptBeforeSuffix.Text = "`n" $GitPromptSettings.BeforePath.Text ="[" $GitPromptSettings.AfterPath.Text ="]" } #endregion #region Prompt (must be after posh-git settings) $MaybeAdminString = Invoke-Command -ScriptBlock { $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = [Security.Principal.WindowsPrincipal] $identity $adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator $s = "" if ($principal.IsInRole($adminRole)) { $s += "[ADMIN] " } return $s } if ($null -ne (Get-Module -Name "posh-git")) { $GitPromptSettings.DefaultPromptBeforeSuffix.Text += $MaybeAdminString $GitPromptSettings.DefaultPromptBeforeSuffix.ForegroundColor = "Red" $MyPrompt = { & $GitPromptScriptBlock } } else { $OriginalPrompt = (Get-Command Prompt).ScriptBlock $MyPrompt = { $ESC = [char]27 $DEFAULT_COLORS = "$ESC[0m" $FG_RED = "$ESC[31m" $BG_BLACK = "$ESC[40m" $BG_BLACK + $FG_RED + $MaybeAdminString + $DEFAULT_COLORS + (Invoke-Command -ScriptBlock $OriginalPrompt) } } function prompt { & $MyPrompt } #endregion Update-FormatData -PrependPath "$PSSCriptRoot\System.IO.FileInfo.Format.ps1xml" #region dotnet.exe argument completer $dotnet_argument_completer = { param($wordToComplete, $commandAst, $cursorPosition) dotnet complete --position $cursorPosition $commandAst.ToString() | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_) } } Register-ArgumentCompleter -Native -CommandName "dotnet" -ScriptBlock $dotnet_argument_completer #endregion #region Cmdlets function New-PSCredential { [Diagnostics.CodeAnalysis.SuppressMessage("PSAvoidUsingPlainTextForPassword", "", Justification="By design")] [CmdletBinding()] param ( [Parameter(Mandatory=$true)][string]$Username, [Parameter(Mandatory=$true)][string]$Password ) $password_encrypted = ConvertTo-SecureString -String $password -AsPlainText -Force $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password_encrypted return $credential } function Install-CyberModules { Install-Module -Name $CYBER_MODULES_MISSING -Force -AllowClobber } #endregion |