Pages

Wednesday, 26 July 2017

PowerShell PowerShots-1.0

Anyone who works in IT for any amount of time has used PowerShell. It provides an exceptional way of configuring workstations and servers through scripts and automation.
One of the great things about PowerShell is seemingly unlimited ability to create unique solutions, either through a few lines of code or creative functions.

Here is a collection of what I call PowerShell PowerShots, definitely a work in in progress.

Try them out, play with them for a

# Creates Windows input box 
   [Reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
   [Microsoft.VisualBasic.interaction]::Msgbox("My Msg", "OkCancel,Critical", "My Title")

# Function Show-Msgbox 
# Returns user selected value from developer selected options
   Function Show-Msgbox ($Msg,$Button,$Icon,$Title)
{
          <#
          Button Values->OkOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel
         Icon Values   ->Critical, Question, Exclamation, Information
#>
[reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
$Result=[microsoft.visualbasic.interaction]::Msgbox($Msg, "$Button,$Icon", $Title)
     Return $Result
}

# Usage
   $MyResult=Show-Msgbox "My Msg" "OkCancel" "Critical" "My Title"

# If Computer variable is null, set it to localhost
   If (! $Computer) {$Computer= $env:COMPUTERNAME}

# Show-Ram in GB
   $TotalRam=(((Get-WmiObject -Class win32_physicalmemory -ComputerName $Computer | Measure-Object -Property capacity -Sum).Sum) / 1GB)

# Find DHCPServer 
  $DHCPServer=(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -eq $true -and $_.DHCPServer -ne $null}).DHCPServer

# Get-DHCPLeases. Returns false on error
   Function Get-DHCPLeases ($Computer)
{
     Try
{
$DHCPScope=Get-DhcpServerv4Scope -ComputerName $Computer -ErrorAction Stop
$DHCPLeases=Get-DhcpServerv4Lease -ComputerName $Computer  -ScopeId ($DHCPScope.ScopeID.IPAddressToString) -ErrorAction Stop
}
Catch
{
         $DhcpLeases=$false
}
    Return $DhcpLeases
}
# Useage-Call Get-DHCP function and pass the Find DHCPServer
  Get-DHCPLeases ((Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.DHCPEnabled -eq $true -and $_.DHCPServer -ne $null}).DHCPServer)

No comments:

Post a Comment