fbpx Windows | HostingUltraso.com
24/7/365 Support

Windows

Use a COM Object

Problem

You want to create a COM object to interact with its methods and properties.

Solution

Use the NewObject cmdlet (with the –ComObject parameter) to create a COM object from its ProgID. You can then interact with the methods and properties of the COM object as you would any other object in PowerShell.

$object = NewObject ComObject ProgId

For example:

PS >$sapi = NewObject Com Sapi.SpVoice PS >$sapi.Speak("Hello World")

Discussion

Reduce Typing for Long Class Names

Problem

You want to reduce the amount of redundant information in your script when you interact with classes that have long type names.

Solution

To reduce typing for static methods, store the type name in a variable:

$math = [System.Math] $math::Min(1,10) $math::Max(1,10)

To reduce typing for multiple objects in a namespace, use the f (format) operator:

$namespace = "System.Collections.{0}" $arrayList = NewObject ($namespace f "ArrayList") $queue = NewObject ($namespace f "Queue")

Program: Create Instances of Generic Objects .NET Framework

When you work with the .NET Framework, you’ll often run across classes that have the primary responsibility of managing other objects. For example, the System. Collections.ArrayList class lets you manage a dynamic list of objects. You can add objects to an ArrayList, remove objects from it, sort the objects inside, and more. These objects can be any type of object—String objects, integers, DateTime objects, and many more. However, working with classes that support arbitrary objects can sometimes be a little awkward.

Create an Instance of a .NET Object

Problem

You want to create an instance of a .NET object to interact with its methods and properties.

Solution

Use the NewObject cmdlet to create an instance of an object.

To create an instance of an object using its default constructor, use the NewObject cmdlet with the class name as its only parameter:

PS >$generator = NewObject System.Random PS >$generator.NextDouble() 0.853699042859347

Work with .NET Objects

Problem

You want to use and interact with one of the features that make PowerShell so powerful—its intrinsic support for .NET objects.

Solution

PowerShell offers ways to access methods (both static and instance) and properties.

To call a static method on a class, place the type name in square brackets, and then separate the class name from the method name with two colons:

[ClassName]::MethodName(parameter list)

Control Access and Scope of Variables and Other Items in Windows PowerShell

Problem

You want to control how you define (or interact with) the visibility of variables, aliases, functions, and drives.

Solution

PowerShell offers several ways to access variables. To create a variable with a specific scope, supply that scope before the variable name:

$SCOPE:variable = value

To access a variable at a specific scope, supply that scope before the variable name:

Access Environment Variables in Windows PowerShell

Problem

You want to use an environment variable (such as the system path, or current user’s name) in your script or interactive session.

Solution

PowerShell offers several ways to access environment variables. To list all environment variables, list the children of the env drive:

GetChildItem env: To get an environment variable using a more concise syntax, precede its name with

$env:

$env:variablename

i.e.: $env:username

Store Information in Variables in Windows PowerShell

Problem

You want to store the output of a pipeline or command for later use, or to work with it in more detail.

Solution

To store output for later use, store the output of the command in a variable. You can access this information later, or even pass it down the pipeline as though it was the output of the original command:

PS >$result = 2 + 2 PS >$result 4 PS >$processes = GetProcess PS >$processes.Count 85 PS >$processes | WhereObject { $_.ID eq 0 }

Handles
NPM(K)
PM(K)
WS(K) VM(M)
CPU(s)
Id ProcessName

Variables and Objects in Windows PowerShell

As touched on in Chapter 2, Pipelines , PowerShell makes life immensely easier by keeping information in its native form: objects. Users expend most of their effort in traditional shells just trying to resuscitate information that the shell converted from its native form to plain text. Tools have evolved that ease the burden of working with plain text, but that job is still significantly more difficult than it needs to be.

Automate Windows PowerShell Data-Intensive Tasks

Problem

You want to invoke a simple task on large amounts of data.

Solution

If only one piece of data changes (such as a server name or user name), store the data in a text file. Use the GetContent cmdlet to retrieve the items, and then use the ForeachObject cmdlet (which has the standard aliases foreach and %) to work with each item in that list.

Example 25. Using information from a text file to automate dataintensive tasks

Pages

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say