PowerShell Console Application to run useful function

0

PowerShell Console application with menu to execute recurrent commands. (E.g. PowerShell Version, Current User, Network, etc.)

There are also a few example of loading specific Snap-Ins (e.g. SharePoint PowerShell).

Version: The script was tested with PowerShell Version 5.1. Microsoft SharePoint options where tested again an on-premise Microsoft SharePoint 2013 farm.

Note: To use the Microsoft SharePoint options you have to run the script on a Microsoft Server running a SharePoint farm.

The script is just a starting point.

<#
    .SYNOPSIS
        Console application to easily execute recurrent commands
    
    .DESCRIPTION
        Console application to easily execute recurrent commands

    .PARAMETER option

    .EXAMPLE
        Execute script
        ./run.ps1

    .EXAMPLE 
        Execute script with selected command
		./run.ps1 -option 2
#>

# Parameter
$Global:value = ""

# Function Init
function Init() {
	Clear-Host
	$Global:value = ""
}

# Function Finish
function Finish() {
	Write-Host ""
	Write-Host ""
	Pause
}

# Function Bye
function Bye() {
	Write-Host ""
	Write-Host "Bye!"
	Write-Host ""
	Write-Host ""
}

# Function Show-Examples
function Show-Examples() {
	write-host 'PowerShell Basics' -ForeGroundColor Gray
	write-host 'enter-pssession -ComputerName <machine> -Credential <domain/account>' -ForeGroundColor Gray
	write-host ''
	write-host 'PowerShell SharePoint Basics' 
	Write-host 'Get-SPSite | select Url, WebApplication, ContentDatabase, CompatibilityLevel | format-table -autosize'
	write-host 'New-SPSite -OwnerAlias <domain/account> -Url https://<sharepoint>/sites/<site> -Name "Demo" -Template "CMSPUBLISHING#0" -Language 1033'
	write-host 'Copy-SPSite https://<sharepoint>/sites/<siteA> -DestinationDatabase <contentdatabasename> -TargetUrl https://<sharepoint>/sites/<siteB>'
	write-host 'Move-SPSite -Identity https://<sharepoint>/sites/<site> -DestinationDatabase <contentdatabasename>'
	write-host 'Get-SPWebTemplate | select Name, Title, CompatibilityLevel | ft -AutoSize'
	write-host 'Get-SPSite -Limit All | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | Format-Table –AutoSize'
}

# Function Load-SPPlugin
function Load-SPPlugin ()
{
    [CmdLetBinding()]
	param()

    Write-Log -Message 'Loading "Microsoft.SharePoint.PowerShell" SnapIn' -Processing
    # Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
    $ver = $host | select version
    if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
    {

        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
	    Write-Log -Message '"Microsoft.SharePoint.PowerShell" Plugin loaded successfully' -Ok -NoTimeStampPlaceholder


    }
    else
    {
        Write-Log -Message '"Microsoft.SharePoint.PowerShell" loaded already' -Info -NoTimeStampPlaceholder	    
    }
    Write-Log -Message 'Loading "Microsoft.SharePoint.PowerShell" SnapIn' -Finished
}


# Function Show-IPConfig
function Show-IPConfig() {
	try {            
    $hostname = hostname
	$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $hostname.ToUpper() -EA Stop | ? {$_.IPEnabled}            
   } catch {            
        Write-Warning "Error occurred while querying localhost."            
   }            
   foreach ($Network in $Networks) {            
    $NetworkAdapter = $Network.Description
	$IPAddress  = $Network.IpAddress[0]            
    $SubnetMask  = $Network.IPSubnet[0]            
    $DefaultGateway = $Network.DefaultIPGateway            
    $DNSServers  = $Network.DNSServerSearchOrder            
    $IsDHCPEnabled = $false            
    If($network.DHCPEnabled) {            
     $IsDHCPEnabled = $true            
    }            
    $MACAddress  = $Network.MACAddress            
    $OutputObj  = New-Object -Type PSObject            
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $hostname.ToUpper()          
    $OutputObj | Add-Member -MemberType NoteProperty -Name NetworkAdapter -Value $NetworkAdapter 
	$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress            
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask            
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway            
    $OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled            
    $OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers            
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress            
    $OutputObj            
   }            
}     


# Function Controller
function Controller() {
	
	$Global:value = Read-Host "Select "

	Write-Host ""
	Write-Host ""

	switch ($Global:value)
	{
	    "1" {
			Init
			$psversion = ($PSVersionTable).PSVersion
			write-host "PowerShell Version : " $psversion -ForeGroundColor Yellow
			Finish
		}
		"2" {
			Init
			$account = $env:Username.ToUpper()
			write-host "Current Account    : " $account -ForegroundColor Yellow
			Finish
	    }
		"3" {
			Init
			Show-IPConfig
			Finish
	    }
	    "4" {
			Init
			If ((Test-Path $profile) -eq $true)
			{
				Write-Host "Profile already exists:"
				Write-Host $profile
			}
			else
			{
				Write-Host "Profile doesn't exist."
			}
			Finish
	    }
	    "5" {
			Init
			If ((Test-Path $profile) -eq $false) {
				New-Item -path $profile -type file -force | Out-Null
				Write-Host "PowerShell startup profile successfully created"
				Write-Host $profile
				notepad $profile
			}
			else
			{
				Write-Host "Profile already exists:"
				Write-Host $profile
			}
			Finish
	    }
	    "6" {
	    	Init
			$title = Read-Host "Please specify the new console title"
			if ($title -ne "") { 
				$host.ui.RawUI.WindowTitle = $title
			}
	    }
	    "50" {
	    	Init
			Load-SPPlugin
			Finish
		}
		"51" {
			Init
			Load-SPPlugIn
			Get-SPWebTemplate | select Name, Title, CompatibilityLevel | ft -AutoSize
			Finish
		}
		"52" {
			Init
			Load-SPPlugIn
			Get-SPSite | select Url, WebApplication, ContentDatabase, CompatibilityLevel | format-table -autosize
			Finish
		}
		"53" {
			Init
			Load-SPPlugIn
			Get-SPSite -Limit All | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | Format-Table –AutoSize
			Finish
		}
	    "e" {
			Init
			Show-Examples
			Finish
		}
	    "a" {
			Init
			Write-Host ""
			Write-Host "This console application was created by Christian Unnerstall to simplify"
			Write-Host "the execution of predefined commands/tasks."
			Write-Host ""
			Write-Host "Current version :   1.0"
			Write-Host "Version date    :   2016/07/20"
			Write-Host ""
			Pause
	    }
	    "q" {
			Bye
		}
		default {
			Init
		}
	}
}

function Main() {
	
	while ($Global:value -ne "q")
	{
		Clear-Host
		Write-Host "Console Application to easily execute predefined commands "
		Write-Host ""
		Write-Host "   1  Show PowerShell Version"
		Write-Host "   2  Show current user"
		Write-Host "   3  Show IP address and hostname"
		Write-Host "   4  Check if PowerShell startup profile exists"
		Write-Host "   5  Create PowerShell startup profile"
		Write-Host "   6  Set the title of the PowerShell console"
		Write-Host ""
		Write-Host "  50  Load Microsoft SharePoint PowerShell SnapIn"
		Write-Host "  51  List SharePoint WebTemplates"
		Write-Host "  52  List SiteCollections by WebApplication and Database"
		Write-Host "  53  List SiteCollections showing size in MB"
		Write-Host ""
		Write-Host ""
		Write-host "  E  Show examples of PowerShell commands"
		Write-Host "  A  About"
		Write-Host "  Q  Quit"
		Write-Host ""
		
		Controller
	}
}

Main
Share.

About Author

Christian Unnerstall

Christian Unnerstall, founder of devshooin.com, enjoys designing and building things that look nice and work well, whether it's websites or services within the interwebs. Have questions - feel free to reach out to him. Enjoy

Leave a Reply