Skip to main content

Posts

Showing posts with the label 50025

What are the text color values for PowerShell output.

PowerShell offers many different options when it comes to displaying your data.  One of my favorites is changing the foreground and background colors of text.  This is probably because way back (circa 1985) I used the Inverse and Flash commands do make different parts of my Applesoft Basic programs stick out.  This is also an improvement on the MS DOS command prompt we so often work with.  It only has two colors, foreground and background.  Below is the syntax to write text in color on both the Shell, and the ISE. Write-Host “ Text to display” –ForegroundColor ColorValue – BackgroundColor ColorValue2 Here are you valid choices for each color value: Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White

What does 2>&1 mean in Powershell?

In class 50025, we noticed some odd code on page 9-2. This code 2>&1 did not come with any good description. It is a redirection operator. Below is some information on the different Powershell redirectors. > 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.

In a get-help –full, what does the Position parameter mean?

In PowerShell, you can get a wealth of information from using the Get-Help parameter. For example, if you type Get-Help Get-Command , you receive basic help information for the cmdlet Get-Command . For detailed information, type Get-Help Get-Command –full . The first section of this expanded help file is the basic information for the cmdlet. It includes the syntax, a description, and in some cases, related commands. The next section list the parameters and the third examples. We will focus on the parameters section. Below is the parameter Name from Get-Command . -Name Gets information only about the cmdlets or command elements with the specified name. represents all or part of the name of the cmdlet or command element. Wildcards are permitted. To list commands with the same name in execution order, type the command name without wildcard characters. For more information, see the Notes section. Required? false Po...

What are the drive types enumerated by Win32_LogicalDrive

IN PowerShell, we can leverage the power of the WMI interface to enumerate the properties of the hardware inside of our clients. A simple PowerShell script to do that is: $computer = "LocalHost" $namespace = "root\CIMV2" Get-WmiObject -class Win32_LogicalDisk -computername $computer -namespace $namespace The information below is an example of the returned data. Remember, this script will return this set of data of all drives connected to the client at the time it was executed. __GENUS : 2 __CLASS : Win32_LogicalDisk __SUPERCLASS : CIM_LogicalDisk __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_LogicalDisk.DeviceID="E:" __PROPERTY_COUNT : 40 __DERIVATION : {CIM_LogicalDisk, CIM_StorageExtent, CIM_Logical Device, CIM_LogicalElement...} __SERVER : FERRARI5X64 __NAMESPACE ...

Can you use AD Recycle bin with 2003 DCs.

The Active Directory Recycle Bin is the newest, and most reliable way of restoring objects into active directory. In the past, you could use an Authorative Restore of the object. The big problem here is that you would have to take a domain controller offline to do it. You also had the ability to re-animate tombstoned objects. When you delete an object from Active Directory, it is tombstoned. That means that it is no longer available for normal Active Directory operations and nearly all of its attributes are cleared. Recovering these objects meant that you had to manually re-apply the attributes like group membership. With AD Recycle Bin, you have up to 180 days to bring it all back. For many, the draw back is going to be the requirement of all Domain Controllers running Windows Server 2008 R2 and the forest functional level of Windows Server 2008 R2. http://technet.microsoft.com/en-us/library/dd391916(WS.10).aspx

How to add a PowerShell Snapin

Powershell is integrated into almost off of Microsoft's latest software. This is one of the reasons why PowerShell is expandable. One way that Powershell is expanded is through the use of Snapins. When you install software, say Exchange 2007, you also install the Exchange PowerShell Snapins for that product. For this demonstration, we will be using Exchange 2007 as our example software. Before we install the Snapins, lets to a little test. Execute the following commands. $a = Get-Command $a.Count This will list the number of cmdlets currently on your computer. On my test computer, I have 180 cmdlets. If you have not installed Exchange yet (or what ever Microsoft product you want to install), do so now. If this is a workstation, you may only need to installed the support tools for the product. Read the product documentation to determine what you need to do. Get-PSSnapin This commmand should list the currently installed snapins on your computer. ...

Query AD for Operating system with PowerShell.

The following script is a modification of the one written by The Scripting Guy: http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx . You will find a detailed explanation of the steps below at the link above. It will also return the OS version to you. I put my modifications in green $strCategory = "computer" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = ("(objectCategory=$strCategory)") $colProplist = "name", “operatingsystem” foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() foreach ($objResult in $colResults) {$objComputer = $objResult.Properties $objComputer.name $objComputer.operatingsystem Write-host “ “ }

How do you call functions from different code?

PowerShell Allows you to call up functions that are stored in other scripts. A few things that you need to consider before you do this. 1 – Your calling script must always have access to the script that it is including in its code. 2 – Troubleshooting. You now must consider multiple scripts with looking into bugs. 3- It will be harder to read your script because you will have to open multiple scripts. We are going to first look at the calling script. . d:\PowerShell\functionlib.ps1 $Name = Read-Host "What is your name: " WriteName($Name) StaggerName($Name) Line 1 is telling our script the file path to another script to include. In this case, . d:\PowerShell\FunctionLib.ps1 . Line 2 is asking for the user to input data. Lines 3 and 4 call 2 different functions from the same external script. Now let us look at the external script being called. Function WriteName($strName) { Write-Host $Name } Function ...

Can you mark a variable as global and have it available in multiple shells.

After testing this, I am going to say no. My test was run on Windows Vista with PowerShell V2 CTP 2. I first created a global variable in one shell. $Global:Var123 = “Hello World” I then verified it by typing Get-Variable . I opened a second PowerShell shell and typed $Var123 ....nothing. I then executed Get-Variable and confirmed that the variable was not present in the second shell.

How to Use Date/Time Information From Custom Logs in PowerShell. Part 2 of 2.

Last Tuesday in part I of this series, we looked at how to use the built in Date/Time methos to find how long ago an event was written in a Windows event log. But what about date/time information that we cannot receive in the correct format because it came from a third party product? No problem. We will work with what data is provided. The Get-Time cmdlet returns an object of System.DateTime fortunatly, using the New-Object cmdlet, we can create a new System.DateTime object with information from our logs. Your first task will be to parse the data so you can extract as much date time information as possible. Once you have done that, you need to create a DateTime object. $MyDate = New-Object System.DateTime . Now take a look at the contents of this object. $MyDate To view the information that we need to plug into this object, type $MyDate | FL . By changing just one property of this object, we will get it to reflect our date. Type $MyDate | GM -MemberType ScriptPro...

How to Use Date/Time Information From Custom Logs in PowerShell. Part 1 of 2.

PowerShell offers us some neat tools to help reduce our coding. In Part I, we are going to look at how to extract date/time information from the Windows event logs and do date/time math. In part II, we will look at how to use date/information from a third party log and utilize the same date/time methods that PowerShell offers us from the Windows logs. Let’s look at the format that time is given to us in PowerShell. Get-Date Now, let’s look at how date/time data is represented from using the Get-EventLog cmdlet. We will be gathering data from the Application log for the demonstration. Notice that we are provided the month in a thee character format. The day is present but not the year. The hour and minutes are in a 24 hour format. Let’s put the output of the event log into a variable. $A = Get-EventLog “Application” Note, this may take a few minutes. Once completed, we are going to determine the last event in the log. Since the objects of the event log are now st...

Is there an ESCAPE key in Powershell?

Yes there is. In other programming languages, you may have encountered the backslash ( \ ) and the start of an escape sequence. It is a little different in PowerShell. We use the backtick character ( ` ). It is usually found on the key to the left of the number ( 1 ) key and shares the key with the tilde ( ~ ). Here are a few examples Character Escape Code Null `0 Alert `a Backspace `b Form Feed `f New Line `n Carriage Return `r Tab `t Vertical quote `v Below is a script the will demonstrate a few of these. # ====================================== # Script Name: EscapeCodeDemo.PS1 # Author: Jason A.Yoder, MCT # Company: MCTExpert, Inc. # Website: www.MCTExpert.com # Blog: www.MCTExpert.blogspot.com # Version: 1.0 # Created: September 14, 2009 # Purpose: To demonstrate the different # escape s...

How to access remote computers with PowerShell?

Windows PowerShell V2 allows you to access remote computers and execute PowerShell commands on those remote clients. The following steps illustrate how to create a session with a single client. For every client that we will be remotely accessing, we need to run the command: Winrm quickconfig Press Y at all prompts. This will open the ports on the firewall that we need open for remote management. Now, on the copmuter that will be making the remote connection, type: Enter-PSSession –computerName ComputerName In my case, the ComputerName parameter is MCT-1. Once the session is established, your prompt will look like this: [MCT-1]: PS C:\Users\Administrator\Documentss> Go ahead and type Get-Service . You should notice that what is returned is the services from the remote client. Type Exit to return to your local client. Now what about multiple sessions? Once you have run WinRM QuickConfig on multiple clients, you can set up multiple sessions. ...