Category: Scripting

Microsoft Windows PowerShell – Get Service Status and Start Service

The Microsoft Windows PowerShell script below will provide the status of a service on the specified server, start the service, then provide the status of the service, again.

<#
.SYNOPSIS
This script will a service on the specified server.
.DESCRIPTION
This script automates the process of starting a service.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Thursday, March 03, 2016.
#>

#Request the hostname of the server and the name of the service
$SERVER = Read-Host “What is the hostname of the server?”
$SERVICE = Read-Host “What is the name of the service?”

#Request the status of the service
get-service -computername $SERVER | where-object {$_.name -eq “$SERVICE”}

#Pause for (5) seconds
Start-Sleep 5

#Start the service
get-service -name $SERVICE -computername $SERVER | set-service -status running

#Pause for (5) seconds
Start-Sleep 5

#Request the status of the service
get-service -computername $SERVER | where-object {$_.name -eq “$SERVICE”}


Microsoft Windows PowerShell – Get Service Status

The Microsoft Windows PowerShell script below will provide the status of a service on the specified server.

<#
.SYNOPSIS
This script will provide the status of a service on the specified server.
.DESCRIPTION
This script automates the process for verifying the status of a service.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Tuesday, November 24, 2015.
#>
$SERVER = Read-Host “What is the hostname of the server?”
$SERVICE = Read-Host “What is the name of the service?”
get-service -computername $SERVER | where-object {$_.name -eq “$SERVICE”}


Microsoft Windows Server 2012 R2 – Remote Reboot

You may use the following command to remotely reboot a server forcefully in (180) seconds running Windows Server 2012 R2.

shutdown /r /m \\<<<hostname>>> /f /t 180


Microsoft Windows Server 2012 R2 – Configure NIC Teaming

You may use the Microsoft Windows PowerShell (.ps1) script below to configure NIC teaming in Microsoft Windows Server 2012 R2. Additionally, you may modify the script to meet the needs of your specific environment.

<#
.SYNOPSIS
This script will configure NIC1 and NIC2 to (1) Gbps Full Duplex, disable NIC3 and NIC4, create NIC Team 1 and configure NIC Team 1 TCP/IP settings.
.DESCRIPTION
This script will automate the configuration of Microsoft Windows Server 2012 R2 NIC Teaming.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Tuesday, November 10, 2015.
#>

#Request the Internet Protocol (IP) address of the server
$IP_Address = Read-Host “What is the Internet Protocol (IP) address of the server?”

#Configure NIC1 and NIC2 to 1 Gbps Full Duplex
Set-NetAdapterAdvancedProperty NIC1 -DisplayName “Speed & Duplex” -DisplayValue “1.0 Gbps Full Duplex”
Set-NetAdapterAdvancedProperty NIC2 -DisplayName “Speed & Duplex” -DisplayValue “1.0 Gbps Full Duplex”

#Disable NIC3 and NIC4
Disable-NetAdapter -Name “NIC3” -Confirm:$false
Disable-NetAdapter -Name “NIC4” -Confirm:$false

#Create NIC Team 1
New-NetLbfoTeam -Name “Team 1” -TeamMembers NIC1,NIC2 -TeamNicName “TEAM 1” -TeamingMode SwitchIndependent -LoadBalancingAlgorithm Dynamic -Confirm:$false

#Configure NIC Team 1 TCP/IP settings
Get-NetAdapter -Name “Team 1” | Set-NetIPInterface -DHCP Disabled
Start-Sleep 5
Get-NetAdapter -Name “Team 1” | New-NetIPAddress -AddressFamily IPv4 -IPAddress $IP_Address -PrefixLength “XX” -Type Unicast -DefaultGateway “X.X.X.X”
Set-DnsClientServerAddress -InterfaceAlias “Team 1” -ServerAddresses “X.X.X.X”, “X.X.X.X”

#End of script


Microsoft Windows PowerShell – Starting a Service on a Remote Server

The following (2) commands may be used to verify the status of a service on a remote computer as well as start the service on the remote computer. Additionally, you may substitute the name value with the displayname value.

 

get-service -computername <<<INSERT>>> | where-object {$_.name -eq “<<<INSERT>>>”}

get-service -name <<<INSERT>>> -computername <<<INSERT>>> | set-service -status running


Microsoft Windows PowerShell – Command Reference

The list below contains a reference list of Microsoft Windows PowerShell commands.

  • Networking
    • List Network Adapters
      • Get-NetAdapter

VMware ESXi 5.5 – Scripted Installation

The script below is a sample script that may be used to automate the installation of VMware ESXi 5.5. For more information click here.

#
# Sample scripted installation file
#
# Accept the VMware End User License Agreement
vmaccepteula
# Set the root password for the DCUI and Tech Support Mode
rootpw mypassword
# The install media is in the CD-ROM drive
install –firstdisk –overwritevmfs
# Set the network to DHCP on the first network adapater
network –bootproto=dhcp –device=vmnic0
# A sample post-install script
%post –interpreter=python –ignorefailure=true
import time
stampFile = open(‘/finished.stamp’, mode=’w’)
stampFile.write( time.asctime() )


Microsoft Windows PowerShell – Query a List of SAMAccountNames

The Microsoft PowerShell script below may be used to query a list of Microsoft Active Directory SAMAccountNames from a Notepad (.txt) file and output the Given Name and Enabled status in a CSV file.

 

#This scrip will query a list of SAMAccountNames from a notepad (.txt) file and provide the Name and Enabled status
#
#Provide the path to the Notepad (.txt) file tha contains the list of SAMAccountNames
$– USERS = Get-Content “<<File Path>>”
$– USERS | ForEach {Get-ADUser $_ -Properties * | Select SAMAccountName, Name, Enabled} | Export-CSV -Path “<<File Path>>”

 

Enjoy!


Microsoft Windows PowerShell – List Installed Applications

This script will query a remote computer’s list of installed applications and provide the output in a file and directory of your choice.

Example: gwmi win32_product -ComputerName “Computer1” | out-file -filepath “<File Path>”


Microsoft Windows PowerShell – List Installed Microsoft Updates

This script will provide a list of the Microsoft updates that have been applied to the local computer and provide the output in a file asd well as a directory of your choice.

Example: Get-HotFix | out-file -filepath “<File Path>”