Category Archives: script

TicTacToe in Powershell

So for fun I thought I would create a two player game of tictactoe using Powershell.  It was a good excuse to mess with a multidimensional array, as you will see below.  The variable for the multidimensional array is called $ticTacMatrix, it is how I set up the grid for the game.

$ticTacMatrix=@();
$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$ticTacMatrix+=,@(' ',' ',' ');
$gridCount = 9;

function playGame(){
    ##This function populates the matrix
    param($gridPlace1,$gridPlace2,$placeValue);
    $gridPlace1 = Read-Host "Position 1";
    $gridPlace2 = Read-Host "Position 2";
    if($placeValue -notmatch "[X,O]"){
        Write-Output "invalid";
    }elseif([int]$gridPlace1 -gt 2 -or [int]$gridPlace2 -gt 2){
        Write-Output "outofrange";
    }elseif($ticTacMatrix[$gridPlace1][$gridPlace2] -match "[X,O]"){
        Write-Output "inuse";
    } else {
        $ticTacMatrix[$gridPlace1][$gridPlace2] = $placeValue;
    }
}

function checkwin(){
    ##This function checks to see if there is a winner
    param($gameBoard);
    $winner = '';
    $firstColumn=@();
    $firstColumn = $gameBoard[0][0],$gameBoard[1][0],$gameBoard[2][0];
    $secondColumn=@();
    $secondColumn = $gameBoard[0][1],$gameBoard[1][1],$gameBoard[2][1];
    $thirdColumn=@();
    $thirdColumn = $gameBoard[0][2],$gameBoard[1][2],$gameBoard[2][2];

    $firstDiagonal=@();
    $firstDiagonal = $gameBoard[0][0],$gameBoard[1][1],$gameBoard[2][2];
    $secondDiagonal=@();
    $secondDiagonal = $gameBoard[0][2],$gameBoard[1][1],$gameBoard[2][0];

    if(($gameBoard[0] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[1] -match 'X').count -eq 3){$winner='X';}
    if(($gameBoard[2] -match 'X').count -eq 3){$winner='X';}
    if(($firstColumn -match 'X').count -eq 3){$winner='X';}
    if(($secondColumn -match 'X').count -eq 3){$winner='X';}
    if(($thirdColumn -match 'X').count -eq 3){$winner='X';}
    if(($firstDiagonal -match 'X').count -eq 3){$winner='X';}
    if(($secondDiagonal -match 'X').count -eq 3){$winner='X';}

    if(($gameBoard[0] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[1] -match 'O').count -eq 3){$winner='O';}
    if(($gameBoard[2] -match 'O').count -eq 3){$winner='O';}
    if(($firstColumn -match 'O').count -eq 3){$winner='O';}
    if(($secondColumn -match 'O').count -eq 3){$winner='O';}
    if(($thirdColumn -match 'O').count -eq 3){$winner='O';}
    if(($firstDiagonal -match 'O').count -eq 3){$winner='O';}
    if(($secondDiagonal -match 'O').count -eq 3){$winner='O';}

    if($winner -eq 'X' -or $winner -eq 'O'){
        Write-Output $winner;
    }
}

Clear-Host;
Write-Host "Welcome to Powershell based TicTacToe" -ForegroundColor 'Green';
Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
Write-Host '';

Foreach($row in $ticTacMatrix){$row -join '|';}

$inputVal = 1;
$everyOther = 'no';
$player = 'X';
while($inputVal -le $gridCount){
    Write-Host '';
    Write-Host "Player: $player" -ForegroundColor 'Cyan';
    $gamePlayValue = playGame -placeValue $player;
    if($gamePlayValue -eq 'inuse'){
        Write-Host "Already in use" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }elseif($gamePlayValue -eq 'outofrange'){
        Write-Host "Position out of range" -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }elseif($gamePlayValue -eq 'invalid'){
        Write-Host 'Invalid player' -ForegroundColor 'Red';
        Write-Host '';
        Foreach($row in $ticTacMatrix){$row -join '|';}
        Continue;
    }
    Clear-Host;
    Write-Host "Choose your position based on cordinates, 0,0 is the top left corner." -ForegroundColor 'Green';
    Write-Host '';
    Foreach($row in $ticTacMatrix){$row -join '|';}
    $gameOver = checkWin -gameBoard $ticTacMatrix;
    if($gameOver -eq 'X'){
        Write-Host '';
        Write-Host 'X Wins' -ForegroundColor 'Green';
        Break;
    }
    if($gameOver -eq 'O'){
        Write-Host '';
        Write-Host 'O Wins' -ForegroundColor 'Green';
        Break;
    }
    if($everyOther -eq 'no'){
        $player = 'O';
        $everyOther = 'yes';
    }else{
        $player = 'X';
        $everyOther = 'no';
    }
    $inputVal++;
}

if($inputVal -eq 10){
    Write-Host "No Winners" -ForegroundColor 'Red';
}

 

Advertisements

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 }