Category: Scripting

Microsoft Windows PowerShell – List Microsoft Active Directory Group Members

This script will list the name and account name of each user within an Active Directory group and provide the output of the results in a file and directory of your choice.

Example: Get-ADGroupMember -identity “<Group Name>” | Select Name, SamAccountName | out-file -filepath “<File Path>”


Microsoft Windows PowerShell – Configuring iSCSI Initiator Connections with Windows Server 2012 R2

You may modify the script below to automate the process of configuring an iSCSI Initiator with the Windows Server 2012 R2 operating system.

01


Microsoft Windows PowerShell – Enable IMCPv4 Ping Requests

You may use the following (2) commands to enable IMCPv4 ping requests for Windows Server 2012 R2 with Microsoft Windows PowerShell.

01


Microsoft Windows Powershell – Hyper-V Cmdlets

In order to view all of the Microsoft Hyper-V Cmdlets use the command get-command -module hyper-v. You may explore these commands and create scripts to automate your Hyper-V administration. Automation can save your Information Technology (IT) department both time and money.

01


Microsoft Windows PowerShell – Configure IP Address(es) and DNS Server(s)

You may modify the following Microsoft PowerShell (.ps1) script to configure the Internet Protocol (IP) and Domain Name System (DNS) servers for a Network Interface Card (NIC) in Windows Server 2012 R2.

01


Microsoft Hyper-V – Create a Virtual Switch using a PowerShell Script

Microsoft Hyper-V allows you to create (3) types of virtual switches (External, Internal and Private). These virtual switches may be created manually using the Hyper-V Manager or you may use a PowerShell (.ps1) script to automate the process. Below you will find a script to create an External virtual switch.

1. Issue the Get-NetAdapter command to provide a list of physical network interface (NIC) card(s) on the host. Take note of the name of the NIC that will be used for the external virtual switch.
01

2. You may modify the script below to create an external virtual switch.

02

3. Run Microsoft Powershell (as an Administrator) to create the external virtual switch.

03

4. Lastly, verify the creation of the virtual switch using the Hyper-V Manager > Virtual Switch Manager.

04


Microsoft Hyper-V – Install Hyper-V and the Management Tools using PowerShell

Microsoft allows you to install the Hyper-V role using PowerShell (run as an administrator) as well as the graphical user interface (GUI). One of the advantages of using PowerShell is that you can create a script to deploy a large number of Hyper-V hosts. During my initial testing I was able to install the Hyper-V role but the Management Tools were not being installed. The PowerShell installation requires the -IncludeManagementTools parameter to install the Management Tools.

The command to install both the Hyper-V role and the Management Tools is as follows:

Install-WindowsFeature –Name Hyper-V -ComputerName <computer_name> -IncludeManagementTools -Restart

01

If you will be installing the Hyper-V role on the local host then remove the -ComputerName <computer_name> parameter.

Microsoft provides great documentation for their solutions and the Install the Hyper-V Role and Configure a Virtual Machine document is no exception.

Enjoy!


Microsoft Office 2010 – Office Customization Tool

If you ever find yourself performing a large scale deployment of Microsoft Office Professional Plus 2010 you may use the Office Customization Tool to customize the deployment.For instance, you may specify the product key to use as well as perform a silent installation.

I found this Utility Spotlight article very helpful.

Enjoy!


Microsoft Windows PowerShell – Server Uptime Script

You may use the following Microsoft Windows PowerShell (.ps1) script to create an HTML file daily that provides an uptime report for the servers in your Information Technology (IT) infrastructure.

 

=====START=====

<######################################################################

# Author : Bhavik Solanki 

# Date : 28th March 2012 

# Version : 1.0

# Desctiption : This script will help to monitor Server availability.

#
######################################################################>

Function GetStatusCode
{
Param([int] $StatusCode)
switch($StatusCode)
{
0 {“Success”}
11001 {“Buffer Too Small”}
11002 {“Destination Net Unreachable”}
11003 {“Destination Host Unreachable”}
11004 {“Destination Protocol Unreachable”}
11005 {“Destination Port Unreachable”}
11006 {“No Resources”}
11007 {“Bad Option”}
11008 {“Hardware Error”}
11009 {“Packet Too Big”}
11010 {“Request Timed Out”}
11011 {“Bad Request”}
11012 {“Bad Route”}
11013 {“TimeToLive Expired Transit”}
11014 {“TimeToLive Expired Reassembly”}
11015 {“Parameter Problem”}
11016 {“Source Quench”}
11017 {“Option Too Big”}
11018 {“Bad Destination”}
11032 {“Negotiating IPSEC”}
11050 {“General Failure”}
default {“Failed”}
}
}

Function GetUpTime
{
param([string] $LastBootTime)
$Uptime = (Get-Date) – [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBootTime)
“Days: $($Uptime.Days); Hours: $($Uptime.Hours); Minutes: $($Uptime.Minutes); Seconds: $($Uptime.Seconds)”
}

#Change value of the following parameter as needed
$OutputFile = “\\server.domain.com\Information Technology Reports\Server Uptime\Server Uptime – ” + $(Get-Date -Format ‘MM_dd_yyyy HH_mm tt’) + “.html”
$ServerList = Get-Content “C:\Scripts\Server_Uptime.txt”

$Result = @()
Foreach($ServerName in $ServerList)
{
$pingStatus = Get-WmiObject -Query “Select * from win32_PingStatus where Address=’$ServerName'”

$Uptime = $null
if($pingStatus.StatusCode -eq 0)
{
$OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerName -ErrorAction SilentlyContinue
$Uptime = GetUptime( $OperatingSystem.LastBootUpTime )
}

$Result += New-Object PSObject -Property @{
ServerName = $ServerName
IPV4Address = $pingStatus.IPV4Address
Status = GetStatusCode( $pingStatus.StatusCode )
Uptime = $Uptime
}
}

if($Result -ne $null)
{
$HTML = ‘<style type=”text/css”>
#Header{font-family:”Trebuchet MS”, Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>’

$HTML += “<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
<TR>
<TH><B>Server Name</B></TH>
<TH><B>IP Address</B></TD>
<TH><B>Status</B></TH>
<TH><B>Uptime</B></TH>
</TR>”
Foreach($Entry in $Result)
{
if($Entry.Status -ne “Success”)
{
$HTML += “<TR bgColor=Red>”
}
else
{
$HTML += “<TR>”
}
$HTML += ”
<TD>$($Entry.ServerName)</TD>
<TD>$($Entry.IPV4Address)</TD>
<TD>$($Entry.Status)</TD>
<TD>$($Entry.Uptime)</TD>
</TR>”
}
$HTML += “</Table></BODY></HTML>”

$HTML | Out-File $OutputFile
}

=====END=====

This script assumes you have a Microsoft Notepad (.txt) file titled Server_Uptime.txt in the C:\Scripts directory that contains the Fully Qualified Domain Name (FQDN) of each server for the report.

Below you may view a sample report. For security reasons, I have removed the FQDN of each server as well as it’s Internet Protocol (IP) address.

Server_Uptime

 


Microsoft Windows PowerShell – Delete Files Older Than X Days

You may use the following Microsoft Windows PowerShell script to delete files in a directory that are X days old (I found the script here: http://elderec.org/2012/02/scripting-delete-files-and-folders-older-than-x-days/).

===START===

# set folder path
$dump_path = “C:\shares\dump”

# set min age of files
$max_days = “-7”

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item

===END===

In order to customize the script substitute the variables (i.e. $dump_path = “C:\shares\dump”) for your environment. As always, be sure to test scripts in a lab environment that mirrors your production environment as closely as possible.

Enjoy!