archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Understanding and Creating Windows PowerShell 1.0 Variables - Techotopia
    placed to the right of the assignment operator PowerShell is a loosely typed language In contrast to strongly typed languages such as C C or Java this means that the type of the variable does not need to be specified at creation time Instead PowerShell infers the type from the data being assigned For example let s begin by assigning the word Red to a variable named mycolor mycolor Red We have now declared a variable with the name mycolor and assigned a string value to it of Red Since Red is clearly a string PowerShell safely assumes the variable is of type string We can similarly declare a variable to contain an integer value numberofcolors 6 The above assignment creates a variable named numberofcolors and assigns it a numeric value of 6 Once again the type of the variable can be inferred by PowerShell in this case an Int32 number In fact since all types are essentially objects the type of a variable may be identified at any time by calling the GetType method of the object PS C Users Administrator numberofcolors gettype IsPublic IsSerial Name BaseType True True Int32 System ValueType Having said that PowerShell is a loosely typed language it is important to note that the data type may be explicitly declared if desired For example numberofcolors int32 6 Once a variable has been created the value assigned to that variable can be changed at any time using the same assignment operator approach numberOfShapes 6 Set initial values myShape Circle numberOfShapes 7 Change the initial values to new values myShape Square Accessing PowerShell Variable Values Now that we have learned how to create a variable and assign an initial value to it we now need to look at how to access the value currently assigned to a variable In practice accessing a variable is as simple as referencing the name it was given when it was created For example if we want to display the value which we assigned to our numberofcolors variable we can simply reference it in a command The number of colors is numberofcolors This will cause the following output to appear in the browser The number of colors is 6 Similarly we can display the value of the mycolor variable mycolor is the current color Changing the Type of a PowerShell Variable As we mentioned at the beginning of this chapter PowerShell supports a number of different variable types We will look at these types in detail in the next chapter Windows PowerShell 1 0 Types First we are going to look at changing the type of a variable after it has been created As previosuly mentioned PowerShell is what is termed a loosely typed language This contrasts with programming languages such as Java which are strongly typed languages The rules of a strongly typed language dictate that once a variable has been declared as a particular type its type cannot later be changed In Java for example you cannot assign a floating

    Original URL path: http://www.techotopia.com/index.php/Understanding_and_Creating_Windows_PowerShell_1.0_Variables (2016-02-13)
    Open archived version from archive


  • Basic Windows PowerShell 1.0 Types - Techotopia
    of the object For example PS C Users Administrator myval 10 432 PS C Users Administrator myval gettype IsPublic IsSerial Name BaseType True True Double System ValueType As illustrated in the above PowerShell console output a variable assigned the value 10 432 is stored as type Double by Windows PowerShell To obtain only the full Net type name use gettype fullname as follows PS C Users Administrator myval gettype fullname System Int32 Alternatively the short name may similarly be obtained using gettype name as illustrated below PS C Users Administrator myval gettype name Int32 This technique may also be used to identify which type will be used for a value by enclosing the actual numeric value in question in parentheses and then using the gettype as previously outlined PS C Users Administrator 9898 989 gettype fullname System Double Rather than having the type implied the type may be also be explicitly set by prefixing the value with either the full or short type name For example PS C Users Administrator byte myval 10 PS C Users Administrator myval gettype IsPublic IsSerial Name BaseType True True Byte System ValueType In the case of explicitly making a value Decimal the value may be followed by the d character PS C Users Administrator myvalue 10d PS C Users Administrator myvalue gettype IsPublic IsSerial Name BaseType True True Decimal System ValueType Note that once the type has been explicitly set on a variable it is not possible to implicitly change the variable type simply by assigning a value of a different type to the variable Any attempt to do so will result in an error message from the shell As a case in point the following attempt to assign a string to a variable which has been explicitly declared as Int32 fails PS C Users Administrator int32 myvariable 520 PS C Users Administrator myvariable A String Cannot convert value A String to type System Int32 Error Input string was not in a correct f ormat At line 1 char 12 myvariable A String This problem may however be overcome quite simply by explicitly declaring the new type of the variable when assigning the value PS C Users Administrator string myvariable A String Specifying Hexadecimal Numbers Hexadecimal numbers are specified in PowerShell using the same approach used by most other programming languages such as C C C and Java The technique involves prefixing the number with 0x For example PS C Users Administrator 0x1233fadf 305396447 PowerShell Boolean Type Boolean values which can be either 1 or 0 are defined in PowerShell using the Net System Boolean type the short from of which is bool For example the following command assigns true to a variable of boolean type PS C Users Administrator bool myval 1 PS C Users Administrator myval gettype fullname System Boolean When working with boolean variables the pre defined true and false variables may also be used PS C Users Administrator bool myval false PS C Users Administrator myval False PowerShell String Type

    Original URL path: http://www.techotopia.com/index.php/Basic_Windows_PowerShell_1.0_Types (2016-02-13)
    Open archived version from archive

  • Working with Arrays in Windows PowerShell 1.0 - Techotopia
    with a single element Creating Windows PowerShell Multidimensional Arrays A multidimensional Windows PowerShell array is nothing more than an array in which each array element is itself an array A multidimensional array can therefore be thought of as a table where each element in the parent array represents a row of the table and the elements of each child array represent the columns of the row Multidimensional arrays are created in PowerShell by encapsulating each array element in parentheses and separating the elements with commas The following provides an example of a multidimensional array where a library array is assigned an array for each element containing information for each book PS C Users Administrator library 857643 Smith John The Widget Reference Guide 857644 William Paul The Life of a Widget 857645 Peters Karl A Widget of Honor Obtaining the Length of an Array The length of a Windows PowerShell array may be obtained by accessing the array s Length property using dot notation For example to get the length of the bookinfo array PS C Users Administrator bookinfo length 3 Accessing Elements in a Windows PowerShell Array google ADSDAQBOX FLOW google Once values have been stored in an array it is highly likely that these values will need to be accessed at some later point This is achieved using the array accessor notation combined with the index into the array of the desired value The array accessor is simply the array name followed by square brackets Within the square brackets is placed a number representing the index into the array of the desired element keeping in mind that the first array element in PowerShell is index 0 not 1 For example to access the second element of our bookinfo array the following notation would be used PS C Users Administrator bookinfo 1 Smith John A range of array elements may be accessed by specifying the start and end element within the square brackets separated by two dots PS C Users Administrator bookinfo 0 1 857643 Smith John In addition multiple elements may be accessed by specifying a comma separated list of index values For example the following extracts the first and third elements from the bookinfo array PS C Users Administrator bookinfo 0 2 857643 The Widget Reference Guide This approach may similarly be used to change the value of an array element Once again this involves the use of the index into the array but this time combined with the assignment operator to assign a new value The following command changes the name of the author in the bookinfo array bookinfo 1 Wilson David Accessing Elements in a Windows PowerShell Multidimensional Array To understand accessing elements of a multidimensional array it helps to continue to use the table analogy we started in an earlier section Assuming a multidimensional array we need to first specify the array row that we wish to access Secondly we need to specify the column in that row To access an element therefore we specify

    Original URL path: http://www.techotopia.com/index.php/Working_with_Arrays_in_Windows_PowerShell_1.0 (2016-02-13)
    Open archived version from archive

  • Windows PowerShell 1.0 Hashtables - Techotopia
    it is important to note that the key must be enclosed in double quotes PS C Users Administrator book author Smith John One advantage of this particular approach is that multiple elements may be extracted in a single command using a comma separated list of keys PS C Users Administrator book author title Smith John The Power of Widgets Modifying Windows PowerShell Hashtable Elements The value of a hashtable element may be changed using either object property dot notation or array accessor techniques the assignment operator and the new value The following commands change the values of the ISBN and title elements respectively PS C Users Administrator book isbn 432234234 PS C Users Administrator book author Fred Wilson PS C Users Administrator book Name Value author Fred Wilson isbn 432234234 title The Power of Widgets Adding Elements to a Windows PowerShell Hashtable New elements may be added to a hashtable using both object property and array accessor notation together with the new key and value pair To add publisher and price elements to our book hashtable therefore the following commands may be used PS C Users Administrator book publisher Pulp Media PS C Users Administrator book price 10 34 PS C Users Administrator book Name Value author Fred Wilson publisher Pulp Media price 10 34 isbn 432234234 title The Power of Widgets Removing Elements from a Windows PowerShell Hashtable Unwanted elements may be removed from a hashtable using the remove method of the object referencing the key associated with the element to be removed For example to remove the price element from our book hashtable PS C Users Administrator book remove price PS C Users Administrator book Name Value author Fred Wilson publisher Pulp Media isbn 432234234 title The Power of Widgets Clearing All Elements from a Windows PowerShell Hashtable

    Original URL path: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Hashtables (2016-02-13)
    Open archived version from archive

  • Basic Windows PowerShell 1.0 Operators - Techotopia
    expression 10 Multiplies two values 10 20 Divides two values 20 10 Add two values 10 20 Subtracts two values 2 10 Return the remainder of the division of two values 10 3 Pre and post increment of value val val Pre and post decrement of value val val Note that multiple operators may be used in a single expression For example x y 10 z 5 4 The Addition Operator If the left hand operand of an addition expression is a number PowerShell will attempt to convert the right hand operand to a number if it is not already one and perform an arithmetic addition If the right hand operand cannot be converted to a number an error will be reported PS C Users Administrator x 10 20 Both operands are numeric no conversion required PS C Users Administrator x 30 PS C Users Administrator x 10 20 Right hand operand is a string converted to numeric type PS C Users Administrator x 30 PS C Users Administrator x 10 twenty Right hand operand is a string conversion impossible Cannot convert value twenty to type System Int32 Error Input string was not in a correct for mat At line 1 char 10 x 10 twenty If both operands are strings the addition operand concatenates the strings together PS C Users Administrator string Hello there PS C Users Administrator string Hello there Similarly if the operands are arrays or hashtables the right hand operand will be appended to the end of the left hand operand Multiplication Operator If the left hand operand of a muliplication expression is a number PowerShell will attempt to convert the right hand operand to a number if it is not already one and perform an arithmetic addition If the right hand operand cannot be converted to a number an error will be reported PS C Users Administrator x 10 20 Both operands are numeric no conversion required PS C Users Administrator x 200 PS C Users Administrator x 10 20 Right hand operand is a string converted to numeric type PS C Users Administrator x 200 PS C Users Administrator x 10 twenty Right hand operand is a string conversion impossible Cannot convert value twenty to type System Int32 Error Input string was not in a correct for mat At line 1 char 10 x 10 twenty If the left hand operand is a string and the right hand operand a number the string will be duplicated the specified number of times PS C Users Administrator string Hello 3 PS C Users Administrator string Hello Hello Hello Similarly multiplication of an array causes duplication of array elements as illustrating in the following example PS C Users Administrator myarray red green blue PS C Users Administrator myarray red green blue PS C Users Administrator myarray myarray 3 PS C Users Administrator myarray red green blue red green blue red green blue Division Subtraction and Modulus Operators Unlike the above operators the Windows PowerShell divide subtract and

    Original URL path: http://www.techotopia.com/index.php/Basic_Windows_PowerShell_1.0_Operators (2016-02-13)
    Open archived version from archive

  • Windows PowerShell 1.0 Comparison and Containment Operators - Techotopia
    than case sensitive le Less than or equal to case insensitive ile Less than or equal to case insensitive cle Less than or equal to case sensitive Windows PowerShell Containment Operators In addition to the comparison operators a set of operators referred to as containment operators are provided for the purpose of identifying whether an array or collection contains a specific value The containment operators provided by PowerShell are listed in the following table As with the comparison operators both case sensitive and insensitive options are provided Operator Description contains Group of values in left hand operand contains value specified in right hand operand case insensitive icontains Group of values in left hand operand contains value specified in right hand operand case insensitive ccontains Group of values in left hand operand contains value specified in right hand operand case sensitive notcontains Group of values in left hand operand does not contain value specified in right hand operand case insensitive inotcontains Group of values in left hand operand does not contain value specified in right hand operand case insensitive cnotcontains Group of values in left hand operand does not contain value specified in right hand operand case sensitive Performing Windows PowerShell Comparisons When using the Windows PowerShell comparison operators it is important to keep in mind that PowerShell will look at the left hand operand of the expression to decide the value type to be used as the basis of the comparison Having made this decision if the right hand operand is of a different type PowerShell will attempt to convert it This concept is probably best described using some examples In the following example both operands are of the same type so no conversion is performed PS C Users Administrator 10 eq 20 False If however the operands are of

    Original URL path: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Comparison_and_Containment_Operators (2016-02-13)
    Open archived version from archive

  • Windows PowerShell 1.0 Pipes and Redirection - Techotopia
    PM VersionInfo Windows PowerShell Redirection Operators The operators implemented by Windows PowerShell to facilitate redirection are similar to those used in other shell environments The full complement of these operators is outlined in the following table google ADSDAQBOX FLOW google Operator Description Redirects output to specified file If the file already exists current contents are overwritten Redirects output to specified file If the file already exists the new output is appended to the current content 2 Redirects error output to specified file If the file already exists current contents are overwritten 2 Redirects error output to specified file If the file already exists the new output is appended to the current content 2 1 Redirects error output to the standard output pipe instead of to the error output pipe Windows PowerShell Redirection The operators outlined above are perhaps best demonstrated using some examples the first of which sends output to a file deleting any pre existing content PS C Users Administrator get date date txt The file date txt now contains the output from the get date command as demonstrated by displaying the contents of the file PS C Users Administrator type date txt Monday December 01 2008 1 11 36 PM Having created the file it is also possible to append more output to the end of the existing content using the operator as follows PS C Users Administrator get date date txt This time the original content remains in the file with the new output added to the end PS C Users Administrator type date txt Monday December 01 2008 1 11 36 PM Monday December 01 2008 1 13 45 PM As mentioned previously other shell environments allow input to be redirected This would ordinarily be achieved using the operator to read input from a file or even the keyboard As of version 1 0 of Windows PowerShell this feature has not been implemented although there is every reason to expect it will appear in subsequent versions Redirecting Error Output Windows PowerShell has the concept of different output streams for standard output and error messages The main purpose of this is to prevent error messages from being included within legitimate output In the following example only the valid output is redirected to the file Since we have not redirected the error output it is displayed in the console PS C Users Administrator dir mydata txt myfiles txt error txt Get ChildItem Cannot find path C Users Administrator myfiles txt because it does not exist At line 1 char 4 dir mydata txt myfiles txt output txt In the above console output PowerShell is telling us in the error message that the file named myfiles txt does not exist in the current directory Since there was no complaint about the mydata txt file it is safe to assume that part of the command worked and we should expect some valid output to have been written to the output txt file PS C Users Administrator type output txt Directory

    Original URL path: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Pipes_and_Redirection (2016-02-13)
    Open archived version from archive

  • Windows PowerShell 1.0 Flow Control with if, else and elseif - Techotopia
    developer Programmers who are familiar with C C C Java will immediately be comfortable using Windows PowerShell if statements The basic syntax of Windows PowerShell if statement is as follows if boolean expression PowerShell commands to be performed when expression evaluates to true here Essentially if the boolean expression evaluates to true then the script in the body of the statement is executed The body of the statement is enclosed in braces If on the other hand the expression evaluates to false the code in the body of the statement is skipped For example if a decision needs to be made depending on whether one value is greater than another x 20 y 10 if x gt y x is greater than y Clearly x is indeed greater than y causing the message to appear in the console window Using if else Statements google ADSDAQBOX FLOW google The next variation of the if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false The syntax for this construct is as follows if boolean expression commands to be executed if expression is true else commands to be executed if expression is false Using the above syntax we can now extend our previous example to display a different message if the comparison expression evaluates to be false x 10 y 20 if x gt y x is greater than y else x is less than y In this case the second statement will execute because the value of x is now less than the value of y Using if elseif Statements So far we have looked at if statements which make decisions based on the result of a single logical expression Sometimes it becomes necessary to make decisions based on a number

    Original URL path: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_Flow_Control_with_if,_else_and_elseif (2016-02-13)
    Open archived version from archive



  •