Category: Scripting

Microsoft Active Directory – Verify the Status of User Accounts

The Microsoft Windows PowerShell script below will verify the status, enabled or disabled, of Microsoft Active Directory user accounts.

<#
.SYNOPSIS
This scrip will query a list of SAMAccountNames from a notepad (.txt) file and provide the Name and Enabled status.
.DESCRIPTION
This script will automate the process of querying a list of SAMAccountNames and provide the Name and Enabled status.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Thursday, March 24, 2016.
#>

#Import the Microsoft Active Directory module
Import-Module ActiveDirectory

#Provide the path to the Notepad (.txt) file that contains the list of SAMAccountNames
$– USERS = Get-Content “<<<INSERT PATH>>>\Users.txt”

#Query the list of users and provide the output in the specified comma seperate value (.csv) file
$– USERS | ForEach {Get-ADUser $_ -Properties * | Select SAMAccountName, Name, Enabled} | Export-CSV -Path “<<<INSERT PATH>>>\Users.csv”


Scripting – List Remote Hard Disk Space

You may use the script below to query a list of remote servers for their drive letters, drive sizes, and the amount of free space on each drive in gigabytes (GB).

<#
.SYNOPSIS
This script will provide the drive letter, drive size and free space of a list of servers.
.DESCRIPTION
This script will automate the process of providing the drive letter, drive size and free space of a list of servers.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Wednesday, March 23, 2016.
#>

#Provide the hard drive information for the servers in the Remote_Servers.txt file
$Computers = Get-Content “<<<INSERT PATH>>>\Remote_Servers.txt”
gwmi Win32_LogicalDisk -Computer $Computers -Filter ‘DriveType = 3′ `
| select SystemName, DeviceID,
@{n=’Size’;e={[int]($_.Size/1GB)}},
@{n=’FreeSpace’;e={[int]($_.FreeSpace/1GB)}}


Scripting – List Local Hard Disk Space

You may use the script below to query the drive letters, drive sizes, and the amount of free space on each drive in gigabytes (GB) for the local host.

<#
.SYNOPSIS
This script will provide the drive letter, drive size and free space of the local server.
.DESCRIPTION
This script will automate the process of providing the drive letter, drive size and free space of the local server.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Wednesday, March 23, 2016.
#>

#Provide the hard drive information for the local server
Get-WmiObject Win32_LogicalDisk -Filter ‘DriveType = 3′ `
| select SystemName, DeviceID,
@{n=’Size’;e={[int]($_.Size/1GB)}},
@{n=’FreeSpace’;e={[int]($_.FreeSpace/1GB)}}


Scripting – Dell iDRAC 8 User

You may use the script below to delete a user account from an Integrated Dell Remote Access Controller 8 (iDRAC8). I would like to acknowledge that I did not write this script and the original link may be found here.

<#
.SYNOPSIS
Removes a DRAC user.
.DESCRIPTION
Removes a DRAC user. Requires Dell’s RACADM.EXE utility, which is distributed as part of the Dell DRAC Tools kit.
.PARAMETER TargetSystem
The IP address or hostname of the target system.
.PARAMETER Index
The numerical index of the user to remove.
.EXAMPLE
Remove-DRACUser -TargetSystem 192.168.100.20 -Index 4
#>

[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=”High”)]
param(
[Parameter(Mandatory=$true, HelpMessage=”Enter the hostname or IP address of the target”)]
[String]$TargetSystem,

[Parameter(Mandatory=$true, HelpMessage=”Enter the index of the user to remove”)]
[ValidateRange(3, 16)]
[Int32]$Index
)

BEGIN
{
New-Variable -Name RACADMExe -Value “C:\Program Files\Dell\SysMgt\rac5\racadm.exe”
New-Variable -Name RACRootUser -Value “root” -Option Constant
New-Variable -Name RACRootUserPass -Value “7Re`$rupU” -Option Constant
$UserToRemove = “”
}
PROCESS
{
$Error.Clear()
Write-Verbose “Checking for existence of racadm.exe”
if(!(Test-Path -Path $RACADMExe))
{
Write-Error “Run this script from the folder that contains racadm.exe”
return
}

Write-Verbose “Attempting to connect to $TargetSystem”
[String[]]$RawResult = & $RACADMExe -r $TargetSystem -u $RACRootUser -p $RACRootUserPass getconfig -g cfgUserAdmin -i $Index 2>&1

foreach($RawLine in $RawResult)
{
if($RawLine -cmatch “^ERROR”)
{
Write-Error $RawLine
return
}
elseif($RawLine -match “^cfgUserAdminUserName=(.*)”)
{
$UserToRemove = $Matches[1]
}
}
if([String]::IsNullOrEmpty($UserToRemove))
{
Write-Error “No user found at index $Index”
return
}
if($PSCmdlet.ShouldProcess($UserToRemove, “Remove DRAC user”))
{
Write-Verbose “Attempting delete…”
[String[]]$RawDeleteResult = & racadm -r $TargetSystem -u $RACRootUser -p $RACRootUserPass config -g cfgUserAdmin -o cfgUserAdminUserName -i $Index `”`” 2>&1
foreach($RawDeleteLine in $RawDeleteResult)
{
if($RawDeleteLine -cmatch “^ERROR”)
{
Write-Error $RawDeleteLine
return # only necessary for future-proofing, in case other script is added beyond this
}
if($RawDeleteLine -match “successfully”)
{
Write-Verbose “Deletion of $UserToRemove was successful.”
}
}
}
}
END {}


Microsoft Windows Server 2012 R2 Core – Rename Network Interface Card

You may use the following the following command to rename a network interface card (NIC) using Windows PowerShell in Microsoft Windows Server 2012 R2 Core. In this particular example the NIC is being renamed from Ethernet0 to LAN01.

 

Rename-NetAdapter -Name Ethernet0 -NewName LAN01


Microsoft Windows Server 2012 R2 Core – Configure Hostname

After logging into a Microsoft Windows Server 2012 R2 Core installation you may use the following commands to configure the hostname of the server.

1. powershell
2. hostname
3. Rename-Computer HOSTNAME
4. shutdown -r -t 00


Microsoft Windows PowerShell – Scheduling a Microsoft Windows PowerShell Script

You may use the following settings to configure a Microsoft Windows PowerShell script to run as an automated task in Task Scheduler. This is a very helpful feature for running scripts during the evening.

  • General
    • When running the task, use the following user account: DOMAIN\– USERNAME
    • Run whether user is logged on or not
  • Actions
    • Start a program
    • powershell.exe -file “<<<INSERT PATH TO SCRIPT>>>”
  • Settings
    • Allow task to be run on demand
    • run task as soon as possible after a scheduled start is missed
    • Stop the task if it runs longer than: 3 days
    • If the running task does not end when requested, force it to stop

VMware vCenter Server 6.0 – Verify the Status of Critical Services

The script below will query the status of the VMware VirtualCenter Server (vpxd), VMware Inventory Service (invsvc) and VMware vSphere Web Client (vspherewebclientsvc) services on the specified server hosting an instance of VMware vCenter Server 6.0.

<#
.SYNOPSIS
This script will provide the status of the VMware VirtualCenter Server (vpxd), VMware Inventory Service (invsvc) and VMware vSphere Web Client (vspherewebclientsvc) services on VCENTER01.ROOT.SYSADMIN.NET.
.DESCRIPTION
This script will automate the process of providing the status of the VMware VirtualCenter Server (vpxd), VMware Inventory Service (invsvc) and VMware vSphere Web Client (vspherewebclientsvc) services on VCENTER01.ROOT.SYSADMIN.NET.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Friday, March 25, 2016.
#>

#Query the status of the VMware VirtualCenter Server (vpxd), VMware Inventory Service (invsvc) and VMware vSphere Web Client (vspherewebclientsvc) services on VCENTER01.ROOT.SYSADMIN.NET
Get-Service -ComputerName VCENTER01 | Where-Object {$_.name -eq “vpxd”}
Get-Service -ComputerName VCENTER01 | Where-Object {$_.name -eq “invsvc”}
Get-Service -ComputerName VCENTER01 | Where-Object {$_.name -eq “vspherewebclientsvc”}


VMware vCenter Server 5.0 – Verify the Status of Critical Services

The script below will query the status of the VMware Virtual Center Server (vpxd) and vCenter Inventory Service (vimQueryService) services on the specified server hosting an instance of VMware vCenter Server 5.0.

<#
.SYNOPSIS
This script will provide the status of the VMware Virtual Center Server (vpxd) and vCenter Inventory Service (vimQueryService) services on VCENTER01.ROOT.SYSADMIN.NET.
.DESCRIPTION
This script will automate the process of providing the status of the VMware Virtual Center Server (vpxd) and vCenter Inventory Service (vimQueryService) services on VCENTER01.ROOT.SYSADMIN.NET.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Friday, March 25, 2016.
#>

#Query the status of the VMware Virtual Center Server and vCenter Inventory Service services on VCENTER01.ROOT.SYSADMIN.NET
Get-Service -ComputerName VCENTER01 | Where-Object {$_.name -eq “vpxd”}
Get-Service -ComputerName VCENTER01 | Where-Object {$_.name -eq “vimQueryService”}


Microsoft Active Directory – Export Group Membership

You may use the following Microsoft Windows PowerShell script to export the users in a Microsoft Active Directory group to a notepad (.txt) file. Additionally, please edit the out-file -filepath portion of this script to a network location of your choice.

<#
.SYNOPSIS
This script will provide the members of a Microsoft Active Directory group and export them to a notepad (.txt) file.
.DESCRIPTION
This script automates the process of exporting the users in a Microsoft Active Directory group.
.EXAMPLE
N/A.
.AUTHOR
Written by Noel Enrique Alvarez on Thursday, March 17, 2016.
#>

#Import the Microsoft Active Directory module
Import-Module ActiveDirectory

#Request the name of the Microsoft Active Directory group
$GROUP = Read-Host “What is the name of the Microsoft Active Directory group?”

#Export the members of the Microsoft Active Directory group and export them.
Get-ADGroupMember -identity $GROUP | Select Name, SamAccountName | out-file -filepath “C:\Users\User01\Desktop\$GROUP.txt”