PSISE.psm1
#region New-PSISETab Function New-PSISETab { Param ( [string[]]$Name, [switch]$Verbose, [switch]$NoSwitch ) # Check if the host is ISE try { Get-Variable -Name psISE -ErrorAction Stop | Out-Null } catch { Throw "This cmdlet can only be executed on Powershell ISE hosts." } # Save the current tab in case we need to switch back to it $currentTab = $psISE.CurrentPowerShellTab if( $Name ) { foreach($N in $Name) { if($Verbose) { $psISE.PowerShellTabs.Add() } else { $psISE.PowerShellTabs.Add() | Out-Null } $PSISE.PowerShellTabs[-1].DisplayName = $N } } else { $psISE.PowerShellTabs.Add() | Out-Null } # Check if the user wants to remain on the new tab or switch back to the previous one if($NoSwitch) { $psISE.PowerShellTabs.SetSelectedPowerShellTab($currentTab) } } #endregion #region Rename-PSISETab Function Rename-PSISETab { Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0)] [string]$Name, [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0)] [int]$Number, [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=0)] [switch]$Current, [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=1)] [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=1)] [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=1)] [string]$NewName ) # Check if the host is ISE try { Get-Variable -Name psISE -ErrorAction Stop | Out-Null } catch { Throw "This cmdlet can only be executed on Powershell ISE hosts." } if( $Current ) { $psise.CurrentPowershellTab.DisplayName = $NewName } if( $Number ) { if( $Number -ge $psise.PowerShellTabs.Count ) { Write-Error "No such tab!" } else { $psise.PowershellTabs[$number].DisplayName = $NewName } } if( $Name ) { # Search for the tab for( $i = 0; $i -lt $psise.PowerShellTabs.Count; $i++) { if( $psise.PowerShellTabs[$i].DisplayName -eq $Name ) { $psise.PowerShellTabs[$i].DisplayName = $NewName break } } } } #endregion #region Open-PSISEFile Function Open-PSISEFile { [CmdletBinding(DefaultParametersetName='FileOnly')] Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Parameter(ParameterSetName="FileOnly", Mandatory=$True, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$Path, [Parameter(ParameterSetName="TabNumber", Mandatory=$false, Position=1)] [int]$TabNumber, [Parameter(ParameterSetName="TabName", Mandatory=$false, Position=1)] [string]$TabName, [Parameter(ParameterSetName="TabName", Mandatory=$false)] [Parameter(ParameterSetName="TabNumber", Mandatory=$false)] [Parameter(ParameterSetName="FileOnly", Mandatory=$false)] [switch]$AsCopy ) Begin { # Check if the host is ISE try { Get-Variable -Name psISE -ErrorAction Stop | Out-Null } catch { Throw "This cmdlet can only be executed on Powershell ISE hosts." } } Process { foreach($f in $Path) { # Test if file exists if( -Not (Test-Path -Path $f) ) { Write-Error ("Could not find file " + $f) continue } # Get the full path to the file $p = Resolve-Path -Path $f # Add the file using tab name if( $TabName ) { $found = 0 for($i=0; $i -lt $psISE.PowerShellTabs.Count; $i++) { if($psISE.PowerShellTabs[$i].displayname -eq $TabName) { if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.PowerShellTabs[$i].Files.Add() $newFile.Editor.Text = $content $psISE.PowerShellTabs[$i].ExpandedScript = $true $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.PowerShellTabs[$i].Files.Add($p) | Out-Null } $found ++ break } } if( $found -eq 0 ) { Write-Error "Could not find a tab named "" $TabName """ } continue } # Add the file using tab number if( $TabNumber ) { if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.PowerShellTabs[$TabNumber].Files.Add() $psISE.PowerShellTabs[$TabNumber].ExpandedScript = $true $newFile.Editor.Text = $content $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.PowerShellTabs[$TabNumber].Files.Add($p) | Out-Null } continue } if($AsCopy) { $content = Get-Content $p -Raw $newFile = $psISE.CurrentPowerShellTab.Files.Add() $newFile.Editor.Text = $content $newFile.Editor.SetCaretPosition(1,1) } else { $psISE.CurrentPowerShellTab.Files.Add($p) | Out-Null } } } End{} } #endregion #region Remove-PSISETab Function Remove-PSISETab { Param ( [Parameter(ParameterSetName="TabName", Mandatory=$True, Position=0)] [string]$Name, [Parameter(ParameterSetName="TabNumber", Mandatory=$True, Position=0)] [int]$Number, [Parameter(ParameterSetName="TabCurrent", Mandatory=$True, Position=0)] [switch]$Current, [Parameter(ParameterSetName="TabName", Mandatory=$false)] [Parameter(ParameterSetName="TabNumber", Mandatory=$false)] [Parameter(ParameterSetName="TabCurrent", Mandatory=$false)] [switch]$Force ) # Check if the host is ISE try { Get-Variable -Name psISE -ErrorAction Stop | Out-Null } catch { Throw "This cmdlet can only be executed on Powershell ISE hosts." } # Get the current tab if( $Current ) { $tab = $psise.CurrentPowerShellTab } # Get the tab using the number if( $Number ) { if( $Number -ge $psise.PowerShellTabs.Count ) { Throw "No such tab!" } else { $tab = $psise.PowershellTabs[$number] } } # Get the tab using the name if( $Name ) { $found = 0 # Search for the tab for( $i = 0; $i -lt $psise.PowerShellTabs.Count; $i++) { if( $psise.PowerShellTabs[$i].DisplayName -eq $Name ) { # Get the tab $tab = $psise.PowerShellTabs[$i] $found++ } } # Check if a tab with the specified name exists if($found -eq 0) { Throw ("Could not find a tab named " + $Name) } } # Check for unsaved files $unsavedFiles = @($tab.Files | Where-Object {$_.IsSaved -eq $false}) if($unsavedFiles.count -gt 0) { if($Force) { # Save the files to %temp% and then remove them foreach($f in $unsavedFiles) { $tempFile = [System.IO.Path]::GetTempFileName() $f.SaveAs($tempFile) Remove-Item -Path $tempFile -Force } } else { Throw ("The tab " + $tab.DisplayName + " has unsaved files.") } } # Close the tab $psise.PowerShellTabs.Remove($tab) | Out-Null # if the current powershell tab was closed, switch to the first tab if($Current) { $psISE.PowerShellTabs.SetSelectedPowerShellTab($psISE.PowerShellTabs[0]) } } #endregion #region Insert-PSISEQuotes Function Insert-PSISEQuotes { [cmdletBinding()] Param ( # Covert text to array [Parameter(ParameterSetName='Array')] [switch] $Array, # Convert text to array and assign to variable [Parameter(ParameterSetName='Variable')] [switch] $Variable, # Trim each line [Parameter(ParameterSetName='Variable')] [Parameter(ParameterSetName='Array')] [switch] [switch]$Trim ) # Check if the host is ISE try { Get-Variable -Name psISE -ErrorAction Stop | Out-Null } catch { Throw "This cmdlet can only be executed on Powershell ISE hosts." } # The variable to hold the new text $newContent = [string]::Empty # Check if there's a file opened if($psISE.CurrentFile -eq $null) { Throw "No files are open." } # Get the current file $file = $psISE.CurrentFile # Get the content $content = $file.Editor.Text # Split the content $lines = $content.split([System.Environment]::NewLine.ToCharArray()) | Where-Object {$_.length -gt 0} # Check if the desired output is array if($Array) { $newContent += '(' + "`n" } # Check if the desired output is variable if($Variable) { $newContent += '$Variable = (' + "`n" } # Process each line $counter = 0 $lines | %{ # Save the input to a variable $line = $_ # Trim if($Trim) { $line = $_.Trim() } # Add quotes [string]$newline = '"' + $line + '"' # Check if the desired output is array if($Array -or $Variable) { $newline = "`t" + $newline if($counter -lt ($lines.Count - 1)) { $newline += "," } } # Add the new line character $newline += "`n" # Increase the counter $counter++ # Append the line to the new content $newContent += $newline } # Check if the desired output is array if($Array) { $newContent += ") |`n`t" } # Check if the desired output is variable if($Variable) { $newContent += ")`n`n" } # Set the new content $file.Editor.Text = $newContent # Move the cursor to the script pane $psISE.CurrentFile.Editor.Focus() } #endregion |