Category: Scripting

Microsoft Windows PowerShell – Get DHCP IPv4 Scope Details

The Windows PowerShell cmdlet below will query a remote Dynamic Host Configuration Protocol (DHCP) server and provide the lease duration of each IPv4 scope.

Get-DhcpServerv4Scope -ComputerName HOSTNAME | Select ScopeID, LeaseDuration


Microsoft Active Directory – Computer Object Password

You may use the following Windows PowerShell cmdlets to view the last time an Active Directory (AD) computer object reset it’s password for all of the computer objects in an AD domain or an individual computer object in an AD domain.

Get-ADComputer -Filter * -Properties PasswordLastSet | Select Name, PasswordLastSet | Sort-Object Name, PasswordLastSet | Format–List
Get-ADComputer -Filter ‘Name -EQ “<<<HOSTNAME>>>”‘ -Properties PasswordLastSet | Select Name, PasswordLastSet | Format-List


Microsoft Windows PowerShell – View Reboot and Uptime

The Microsoft Windows PowerShell script below will query a Windows hosts for the time it was rebooted and the time it completed it’s reboot.

<#
.SYNOPSIS
This script will request the hostname of a server then provide the time it was shutdown and completed it’s reboot.
.DESCRIPTION
This script will request the hostname of a server then provide the time it was shutdown and completed it’s reboot.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Friday, May 10, 2019.
#>

#Request the hostname of the server
$HOSTNAME = Read-Host “What is the hostname of the server?”

#Message
Write-Host The date and time below indicate the time the server was shutdown -ForegroundColor Green

#Provide the uptime of the server
Get-EventLog -Logname System -ComputerName $HOSTNAME | Where-Object {$_.EventID -EQ 6006} | Select-Object -First 1

#Message
Write-Host The date and time below indicate the uptime of the server -ForegroundColor Green

#Provide the uptime of the server
Get-EventLog -Logname System -ComputerName $HOSTNAME | Where-Object {$_.EventID -EQ 6005} | Select-Object -First 1

#End of script


Microsoft Windows PowerShell – Get Host Architecture

You may use the following Microsoft Windows PowerShell script below to get the architecture, physical or virtual, of a Windows host on a network.

<#
.SYNOPSIS
This script will provide the architure (physical of virtual) of a host.
.DESCRIPTION
This script will provide the architure (physical of virtual) of a host.
.EXAMPLE
systeminfo /s $HOSTNAME | findstr /c:”Model:” /c:”Host Name”
.AUTHOR
Written by Noel Enrique Alvarez on Tuesday, April 23, 2019.
#>

#Requst the hostname of the host
$HOSTNAME = Read-Host “What is the hostname of the server?”

#Provide the architure of the host
systeminfo /s $HOSTNAME | findstr /c:”Model:” /c:”Host Name”


VMware PowerCLI – Power On a Virtual Machine

You may use the VMware PowerCLI cmdlet below to power on a VMware virtual machine.

Start-VM -VM SERVER


VMware PowerCLI – Create a Snapshot

You may use the following VMware PowerCLI cmdlet to create a snapshot of a virtual machine (VM) in VMware.

New-Snapshot -VM HOSTNAME -Name “<<>>” -Description “<<>>”


VMware PowerCLI – Exit VMware ESXi Host out of Maintenance Mode

You may use the following VMware PowerCLI script to exit a VMware ESXi host out of maintenance mode.

<# .SYNOPSIS This script will exit a VMware ESXi host out of maintenance mode. .DESCRIPTION This script will exit a VMware ESXi host out of maintenance mode. .EXAMPLE Set-VMHost -VMhost HOST.DOMAIN.COM -State Connected. .AUTHOR Written by Noel Enrique Alvarez on Monday, April 15, 2019. #>

#Request the hostname of the VMware ESXi host
$HOSTNAME = Read-Host “What is the hostname of the VMware ESXi host?”

#Exit the VMware ESXi host out of maintenance mode
Set-VMHost -VMhost $HOSTNAME -State Connected

#End of script


VMware PowerCLI – Place VMware ESXi Host in Maintenance Mode

You may use the following VMware PowerCLI script to place a VMware ESXi host in maintenance mode.

<# .SYNOPSIS This script will place a VMware ESXi host in maintenance mode. .DESCRIPTION This script will place a VMware ESXi host in maintenance mode. .EXAMPLE Set-VMHost -VMhost HOST.DOMAIN.COM -State Maintenance. .AUTHOR Written by Noel Enrique Alvarez on Monday, April 15, 2019. #>

#Request the hostname of the VMware ESXi host
$HOSTNAME = Read-Host “What is the hostname of the VMware ESXi host?”

#Place the VMware ESXi host in maintenance mode
Set-VMHost -VMhost $HOSTNAME -State Maintenance

#End of script


Microsoft Windows PowerShell – Query Users on Remote Server

You may use the following Microsoft Windows PowerShell script to query a remote server and display any logged on users.

<# .SYNOPSIS This script will query a remote server and display user session information. .DESCRIPTION This script will query a remote server and display user session information. .EXAMPLE N/A. .AUTHOR Written by Noel Enrique Alvarez on Monday, April 15, 2019. #>

#Request the hostname of the server
$HOSTNAME = Read-Host “What is the hostname of the server?”

#Query the remove server
query user /server:$HOSTNAME

#End of script


Microsoft Windows PowerShell – Reboot Multiple Servers

You may use the following Microsoft Windows PowerShell script to reboot multiple Microsoft Windows hosts on a network.

<# .SYNOPSIS This script will query a list of hostnames then reboot each server. .DESCRIPTION This script will query a list of hostnames then reboot each server. .EXAMPLE Restart-Computer $SERVER .AUTHOR Written by Noel Enrique Alvarez on Friday, April 12, 2019. #>

#The file to be queried for the hostnames
$SERVERS = Get-Content “<<<INSERT PATH>>>”

#Reboot each host in the file
ForEach ($SERVER in $SERVERS)
{Restart-Computer $SERVER -Force}

#End of script