Display Message box in powershell

On my last blog i mentioned about the Menu and now its time for a message box..

Tell me who wouldn’t love to see a message box when you do something successfully, give you a proof of completion also a satisfaction in a success scenario or opens you eye when you face an error.. Anything in a message box users now a days pay more attention.

below script will show you how to create a Message box GUI in powershell.

Function Show-Msgbox {
  Param([string]$message=$(Throw "You must specify a message"),
      [string]$button="okonly",
      [string]$icon="information",
      [string]$title="Message Box"
     )

  [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null

  [microsoft.visualbasic.interaction]::Msgbox($message,"$button,$icon",$title) 

 }

$rc=Show-Msgbox -message "Do you know what you're doing?" `
-icon "exclamation" -button "YesNoCancel" -title "Hey $env:username!!"

Switch ($rc) {
 "Yes" {"Yes."}
 "No" {"No."}
 "cancel" {"Cancel"}
}

Creating a Menu in Powershell

Hello! Menu? what you use can that be?

Well to be honest there could be a lot use if you know how to use it like you could have a massive set of functions on your script which is not in order and menu comes in place to put beautifully presentable and easy to navigate even for an end user by simply select number on the menu

saying of Menu reminded me to share the below script on how you can create a menu in powershell

Function Show-Menu {

$menu="Menu Heading `n" +`
"1 `t Title 1 `n" +`
"2 `t Title 2`n" +`
"3 `t Title 3 `n" +`
"Q `t Title 4 `n" +`
"Select a menu choice"

$rc=Read-Host $menu
write $rc

} 

Do {
    Switch (Show-Menu) {
     "1" {Write-Host "Code 1"} 
     "2" {Write-Host "Code 2"}
     "3" {Write-Host "Code 3"}
     "Q" {Write-Host "Code 4"}
    
    }
} While ($True)

Let me know if you need a custom menu script created ๐Ÿ˜‰

Start Service on a Remote server

Services are mostly to go halt time to time which makes some function or application to cause issue or not start and when you know it is most likely by a service is stopped the below script can you help sort it.

$statusofthePES = (Get-Service -ComputerName "server/machine name" -Name "servicename")

if ($statusofthePES.Status -eq "Stopped")
{
    "PES Service is stopped!!"
    Get-Service -ComputerName "Server/machine name" -Name "servicename"| Start-Service
    "Started Now!!"
}elseif($statusofthePES.Status -eq "Running"){
    "PES Service is Running!!"
    Get-Service -ComputerName "server/machine name" -Name "servicename"| Stop-Service
    "Stopped Now!!"
}

Show all the Running Services

Checking the services on a machine is a easy job only if you want to check more than 1 machine.

The script below will help you on this.

$strComputer="."
$colItems=get-wmiobject -class "Win32_Service" -namespace "root\CIMV2" `
-computername $strComputer | sort "Caption" 

foreach ($objItem in $colItems) {
	if ($objItem.State -eq "Running") {
	write-host $objItem.Caption "("$objItem.Name")"  -foregroundcolor "green" }
	else {write-host $objItem.Caption "("$objItem.Name")" -foregroundcolor "red" }
}

to make it pretty i made the Running services in Green and else in Red

Get Boot Time

Getting boot time of the machine could come in handy on most of the situation.

I normally get this request from teams saying “Hey can you get me the boot time of the machine” – i go like you know what use the script below.

$computername=$env:computername

$os=Get-WmiObject win32_operatingsystem -computername $computername

Write-Host ("Last boot: {0}" -f $os.ConvertToDateTime($os.lastbootuptime))
Write-Host ("Uptime   : {0}" -f ((get-date) - $os.ConvertToDateTime($os.lastbootuptime)).tostring())

They go like you are a Star ๐Ÿ˜‰

Restart a Machine via Powershell

As part of the System Admin life you would have used this statement at least once.
Its “Have you restarted the computer?” LOL

to help with that the script below.

$path = get-content -path "machine name list"

Foreach($Machines= in $Path){

    Restart-Computer -ComputerName $Machine -Force
    "Machine Restarted : $line"
}


Extract Members from AD Group

One of my friend use to say there is more than one way to skin a cat. I am a strong believer of this saying likewise i have created few scripts to achieve it.

Get-ADUser -Identity "username" -Properties * | select memberof
Get-ADPrincipalGroupMembership -Identity "username" | Format-Table -Property name
(get-aduser "username" -Properties *).MemberOf | Get-ADGroup | Select-Object -ExpandProperty name
Get-ADGroupMember -Identity "ad group name" | select name >> "path tosave.txt

Local Machine Profile and Registry Remove

HAHA.. this one seems to very interesting to me. One of the engineer asked me write this for him, he has to remove unwanted profiles from servers again talking about 500+ servers

I wrote the below script giving him the ability to put in account that needs to be left alone and anything else can go to bin along with the registry. which got me a lunch ๐Ÿ˜›

$machinename = Read-Host "Please Type the host Name "

$existingusers = Get-ChildItem "\\$machinename\c$\users" | select name

foreach($username in $existingusers){
$checkforaccount =$username.Name.Contains("account name that needs to remain")

            if($checkforaccount -eq "True"){
       
            }else{
                $name = $username.Name
                Get-WmiObject -class Win32_UserProfile -ComputerName "$machinename" | ? {$_.LocalPath -eq "C:\Users\$name"} | Remove-WmiObject
            }
    
}

Multi Copy

When you know scripting you also gain another extraordinary power which is laziness LOL. I am one of those lazy Admin’s who know Powershell and try to do everything using Powershell.

The Multi Copy script basically i got a set of files and folder location that need moving to different machines, servers and NAS. i was so lazy to move stuff that way (talking about 300 odd) so wrote the bad boy down.

$computers = get-content -path "machines name list" 
$source = read-host "Please type the Path and file name to Copy?" 
$destination = "destination location" 

foreach ($computer in $computers) { 

    if ((Test-Path -Path \\$computer\$destination)) { 
    Copy-Item $source -Destination \\$computer\$destination -Recurse 
    } else { 
    "\\$computer\$destination is not reachable or does not exist" 
    } 
} 

Moving User/s to an OU

This is quite fun, when i say fun in the system admin life ๐Ÿ˜›
When ever you move someone out of an OU, there is an 90% chance that they could get different set of Group Policy as OU and Group policy are together.

The script below will be help you in moving.

$data = get-content -Path "usernames that needs moving file"

foreach($username in $data) {

$targetOU = "Target OU "

    $display = Get-ADUser -Properties * -LDAPFilter "(displayName=$username)" | Select samaccountname,distinguishedName
    
    $name = $display.samaccountname
    $Path = $display.distinguishedName
        try{
            Move-ADObject -Identity $display.distinguishedName -TargetPath $targetOU
            Write-Host  $name + " - Moved" -ForegroundColor Green
        
        }catch{
       
            write-host
            write-host $name + " - Not Moved" -ForegroundColor Yellow
        }
      
}