August 1

Powershell: Send keyboard input to the screen

Here is a very basic way to send keyboard inputs to the screen on a looped time interval:

while ($true) {
$minutes = 1

$myShell = New-Object -com “Wscript.Shell”

for ($i = 0; $i -lt $minutes; $i++) {
Start-Sleep -Seconds 30
$myShell.sendkeys(” “)
}
}

Category: Programming | Comments Off on Powershell: Send keyboard input to the screen
July 31

Powershell: Creating an endless loop in your script

Sometimes when scripting you need to create a never ending loop while your script either waits for some other task to complete or repeats a task over and over again. There are two ways people tend to go about creating loops in Powershell, but one of them will eventually leave your script in a heap.

What not to do – a self referencing function:

When people are first starting out in Powershell, they tend to make loops with self-referencing functions. It’s the logical thing to do – run a function and, if it doesn’t turn out the way you wanted it to, run the function again. Take the example below:

Function Count-Up
{
    if($i -lt 9999)
    {
        $i++
        Count-Up
    }
    else
    {
        Write-Host “Count complete - We have counted up to $i”
    }
}

The problem with this is that you aren’t really creating a loop – it’s actually a spiral. Each time the Count-Up function is executed by the If statement it is actually running within the previous Count-Up function. Eventually, and in this case quite quickly, you end up with a spiral so deep PowerShell decides to bail out. At that point you get a “call depth overflow” and Powershell merrily ploughs on with the rest of your script.

The Right Way – using the While statement:

The correct way to build a loop is to use the inbuilt statement While. With a While loop, While replaces the If statement and the loop will continue to run while the statement evaluates to true, removing the need to re-reference the function (and accidentally create a spiral). This is the previous example re-written for a While statement:

Function Count-Up
{
    while($i -lt 9999)
    {
        $i++
    }

    Write-Host “Count complete - We have counted up to $i”
}

Notice we have also got rid of the Else statement. A While loop will continue to run until the statement is no longer true, so it doesn’t require Else statements (or support them) – we can be sure that the line after the loop will only run once the loop has completed.

Creating a truly endless function:

Sometimes you will want a truly endless function, that will run and run until the PowerShell session is closed. This is acheived simply by providing the While statement a condition that is always true. In PowerShell this looks like this:

while($true)
{
    $i++
    Write-Host “We have counted up to $i”
}

By: A. Craik

Category: Programming | Comments Off on Powershell: Creating an endless loop in your script
May 28

PHP: Read the contents of files in a directory

Here is a basic php script to read the contents of “text” files in a directory.

<form>
<center>
<div>
IP’s of all clients</br></br>

<?php
foreach (glob(“workstations/*”) as $file) {
$file_handle = fopen($file, “r”);
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);
echo ‘<br>’;
}
?>

</div>
</center>
</form>

Category: Programming | Comments Off on PHP: Read the contents of files in a directory
April 23

Perl: Running IBM’s balance.pl from SVCTools on Centos/Red Hat

balance.pl is used when mdisk drive groups get off balanced in a storage pool.
At this time, IBM does not have a way to automatically balance drive groups.
This can really cause an issue as you add more drive chassis’ to your V7000.
Over time the earlier created mdisk groups house more data than the newer groups.
This cause the workload to be off balanced as the old drive do a far higher percentage of the work.
The end result is higher data transfer latencies.  We especially noticed this with writes rather than reads.

I ran into an issue where I could not get the SVC.pm module to be seen by my perl installation.

I ran the command:
perl balance.pl PoolName -k privatekeyname.ppk -c 192.168.1.10 -r

I received the following error:
Can’t locate IBM/SVC.pm in @INC (@INC contains: ……./usr/lib64/perl5) at /SVCTools/examples/balance.pl line 258.

I created an IBM directory in /usr/Lib64/perl5
example: /usr/Lib64/perl5/IBM
I placed the SVC.pm file in the IBM directory

balance.pl ran fine afterward

For testing use: balance.pl PoolName -k privatekeyname.ppk -c 192.168.1.10 -r

For actually running the extent migration use: balance.pl PoolName -k privatekeyname.ppk -c 192.168.1.10 -r -e

Additional Information:
EXAMPLES
To have the script report a set of migrations for a standard Managed
Disk Group configuration where all disks need to be balanced, only
a basic invocation is needed:

perl balance.pl myMDiskGrp -k C:key.ppk -c myCluster -r

This will provide a complete list of migration “phases”. All operations
in an individual phase can be run concurrently. If you are happy with
the results being produced by the script, you can run the commands
manually or have the script do this for you:

perl balance.pl myMDiskGrp -k C:key.ppk -c myCluster -r -e

If you do not want the script to run as many migrates as possible, you
can restrict the number using -n [number].

To run the script only against a particular set of MDisks or VDisks:

perl balance.pl myMDiskGroup -k … -r -m MDisk1,MDisk2,MDisk3,…

perl balance.pl myMDiskGroup -k … -r -vtVDisk1,VDisk2,VDisk3,…

For additional command line output, specify -v (–verbose).

Category: Programming | Comments Off on Perl: Running IBM’s balance.pl from SVCTools on Centos/Red Hat
April 23

Perl: cpan reload error

When trying to reload cpan I was also getting an error about “………01mailrc.txt.gz”

Started cspan
At the promt cpan{1}> type: reload index
There was a notification that there is a newer version of cpan

At the promt cpan{1}> type: install CPAN
The new version of CPAN was downloaded and installed

At the promt cpan{1}> type: reload index
All errors and we gone

Category: Programming | Comments Off on Perl: cpan reload error