Category Archives: script

Validate Web Site is Up – Powershell

##Create WebClient
$webClient = New-Object System.Net.WebClient;
try {
     ##Download site source code
     $source = $webClient.DownloadString(“webURL”);
} catch {
     ##Couldn’t connect
     echo “Down!”;
}


PowerShell and Regular Expressions

Here is a simple example of finding matches in a line of text using Regular Expressions.

In the example below I am searching for the word “test” in the variable named $myVar.

The command $myRegEx = [regex]”test” defines what we are looking for and sets the variable to a regex type.

The third command searches through $myVar for the word test and the next command shows count of findings.

Image


PowerShell Script Run Time

Quick how to on getting the length of time your script ran.

##Beginning of the script.
$startTime = get-date;

##Your code here

##End of script
$endTime = get-date;
$runTime = $endTime – $startTime;
$runTime


Export vCenter Roles

Below is a powershell script to export vCenter roles to a ready to import set of commands.

Script below will not work without using PowerCLI or being connected to vCenter.

$viRolesList = Get-VIRole;
Foreach ( $role in $viRolesList ) {
     $roleName = $role.Name;
     $privilegeSet = $role | Get-VIPrivilege;

     Echo "New-VIRole -Name ""$roleName""" >> vRoles.txt;

     Foreach ( $privilege in $privilegeSet ) {
         $privilegeID = $privilege.ID;
         Echo "Set-VIRole -role ""$roleName"" -AddPrivilege `
         (Get-VIPrivilege -ID $privilegeID)" >> vRoles.txt;
     }
}

Compare VM Name to VM Filename

In case you were blind sided by the vsphere 5.x bug fix that removes the storage vmotion feature that renames the VM’s folder and files. Below is a script that will at least help you identify the VMs that don’t match their file names.

Open powerCLI
Connect to your vcenter

$colorObject = get-wmiobject -class "Win32_Process" -namespace "root\CIMV2";
$vmCluster = Read-Host "Cluster Name";
$vmList = Get-Cluster $vmCluster | Get-VM;
Foreach ( $vm in $vmList ) {
 $vmView = $vm | Get-View;
 $vmView.Config.Files.VmPathName -match "^.*\/(.*)\.vmx$";
 $vmFName = $matches[1];
if ( $vm.Name -match $vmFName ) {
 write-host "$vm matches $vmFName";
 } else {
 write-host "$vm does not match $vmFName" -foreground "red";
 }
}

List Files Last Modified 30 Days

How to list files last modified in the last 30 days via Powershell.  Run these commands in the directory containing the files to filter.

$lastModAge = (get-date).adddays(-30);
Get-ChildItem | Where { $_.LastWriteTime -gt $lastModAge }

NSLookup Sweep

I keep finding the need to find an open IP address for a new server, and I get tired of searching manually.  I am also not patient enough for a ping sweep, so I created a nslookup sweep with powershell. The nslookup sweep just looks through a subnet to find the first address without a dns entry. The next step just requires you to ping the address to insure it’s not being used.  Check out this post for a TCP Port Scanner in PowerShell.


$i = "1";
$subnet = "192.168.1";
while ( $i -lt 255 ) {
$ipaddr = "$subnet.$i"
$nsResult = nslookup $ipaddr 2>&1 | select-string "Name";
if ( $nsResult ) {
$i++;
}else{
echo "$ipaddr is available";
$i = "256";
}
}


PowerCLI and Storage

Below are some interesting sets of PowerCLI commands that will give you more information on your VM’s hard drives and your datastores.

The below will give you the total allocated hard drive space of all the VM’s in a cluster.

Get-Cluster "Cluster Name" | get-vm | Get-HardDisk | %{ $vmCapacity += $_.capacityKB }
$vmCapacity

The below will give you the Total Capacity, Free Space, and Usage of your datastores.

Get-Datastore | %{ $datastoreFreeSpace += $_.FreeSpaceMB }
Get-Datastore | %{ $datastoreTotalCapacity += $_.CapacityMB }
$datastoreUsageTotal = $datastoreTotalCapacity - $datastoreFreeSpace

write-host "Total Capacity: $datastoreTotalCapacity MBs"
write-host "Total Free Space: $datastoreFreeSpace MBs"
write-host "Total Usage: $datastoreUsageTotal MBs"


Create EventLogs using Powershell

Create container for logs:

New-EventLog -logname 'Event Log Name' -source 'logsourcename'

Note: Must be run in elevated powershell window

Command to create event log:

Write-EventLog -computername localhost -logName 'Event Log Name' -EventID 5000 -Message "Hello World!" -EntryType Information -source logsourcename

Finding newly created event log:
Open Server Manager

Expand Diagnostics

Expand Event Viewer

Expand Applications and Services Logs

Click on the container that you named using the -logName parameter
Your Logs should be in the right pane.

PowerShell and DNS

Here is a quick one liner to show how you can pull DNS information and filter it using powershell.  Replace zonename with the name of the zone you want to pull information from and replace computername with the name of the computer you are searching for.

dnscmd.exe /zoneprint zonename | where{$_ -like "*computername*"}