Download vbscript_to_powershell PDF

Titlevbscript_to_powershell
File Size295.5 KB
Total Pages65
Document Text Contents
Page 32

As you might expect, if you can use Windows PowerShell to
determine the major version of the product you can also use it to
determine the minor version. In fact, all you have to do is call the
Get-Host Cmdlet and take a look at the value of the Version.Minor
property. This command returns the minor version and stores it in the
variable $a:

$a = (get-host).version.minor

When you run this command and then echo back the value of $a you
should get the following, depending on which version of PowerShell
you are running:

0

Second Definition: Returns a whole number between 0 and 59, inclusive,
representing the second of the minute.

Is it useful for you to be able to take a specified date and time and
throw out everything but the seconds? In other words, if today is
12:29:16 on 9/30/2006, is it useful for you to be able to determine
that the seconds are equal to 16?

To tell you the truth, we don’t know whether that’s useful or not. If it
is useful, however, you can do that in Windows PowerShell by using
the Get-Date Cmdlet and then examining the value of the Second
property:

$a = (get-date).second

When you run this command (assuming it really is 12:29:16 on
9/30/2006) and then echo back the value of $a you should get the
following:

16

SetLocale Definition: Sets the global locale and returns the previous locale.

To be honest, we didn’t put much effort into this one; after all,
SetLocale works only in Web pages and Windows PowerShell is not
designed for use in Web pages. So we let this one slide.

Sgn Definition: Returns an integer indicating the sign of a number.

Quick: is that a negative number or a positive number? An easy way
to verify that in Windows PowerShell is to use the System.Math class
and the Sign method. For example, this command uses the Sign
method to determine the sign of -453:

$a = [math]::sign(-453)

When you run this command and then echo back the value of $a you
should get the following:

-1

Page 33

If you check the sign of a positive number (e.g., 453) you should get
back something like this (actually, you should get back something
exactly like this):

1

Sin Definition: Returns the sine of an angle.

In mathematics the sine of an angle is defined as … well, it doesn’t
matter what the definition is. Instead, all that matters is that, in
Windows PowerShell, you can use the System.Math class and the
Sin method. For example, this command calculates the sine of a 45-
degree angle and then stores the result in the variable $a:

$a = [math]::sin(45)

When you run this command and then echo back the value of $a you
should get the following:

0.850903524534118

Space Definition: Returns a string consisting of the specified number of
spaces.

Sometimes it’s just enough that you can do something; it doesn’t
matter if you have to use some sort of crazy approach to do it. For
example, in VBScript you can use the Space function to create a
string consisting of x number of consecutive blank spaces. Can you
pull off this same feat in Windows PowerShell? You sure can: all you
have to do is, well, take a blank space and multiply it by 25:

$a = " " * 25

Sure it’s crazy. But, like we said, it works. Run the command, then
use this command to add the letter x to the end of the string:

$a = $a + "x"

Guess what you’ll get if you now echo back the value of $a? That’s
right, 25 blank spaces followed by the letter x:

x

Split Definition: Returns a zero-based, one-dimensional array containing
a specified number of substrings.

“Returns a zero-based, one-dimensional array containing a specified
number of substrings.” Granted, that doesn’t sound too terribly
interesting. Nevertheless, the VBScript Split function (or the
Windows PowerShell Split() method) can be incredibly useful. For
example, suppose you have a comma-delimited list of computer
names. As a single string value that’s of minimal use; as an array,
however, that opens up a whole world of possibilities, including the
opportunity to loop through each item in the array and perform a task
against each of those items (i.e., each of those computers).

Similer Documents