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";
}
}