background preloader

Powershell

Facebook Twitter

Windows PowerShell Survival Guide - TechNet Articles - Home - TechNet Wiki. This article was formerly titled "Automating IT Tasks with Windows PowerShell Overview. "** Introduction and Getting Started PowerShell is a vast product, especially when you take into account how it's used in other products such as Lync Server, Exchange, and the Windows Operating System. It can be a little daunting to those new with PowerShell - there is a vast array of deeply technical discussions of all of PowerShell's many aspects, quirks and peculiarities and all. So start at the beginning, and learn the basics, either on you own, or possibly with the aid of a great training class.

Then work with it and return to find more. Purpose of this Document The purpose of this document is to help you to learn more about PowerShell and to be successful in applying it. Scope of this Document This page contains links to help you learn more about Microsoft Windows PowerShell. Learning PowerShell Fundamentals Guides New ! New ! New ! Windows PowerShell Best Practices Quick Reference Materials New ! Misc. Powershell - PowerShell Cmdlet and Help Designer. Windows PowerShell: Sharing Your Scripts - Made Easy | TechNet Magazine. One limitation of Windows PowerShell v1 is that it didn’t do much to make script sharing easier.

Sure, you could easily copy a .ps1 script file to another computer, or even zip it up and e-mail it to a colleague, but you could do that with VBScript more than a decade ago. If your script included reusable functions, however, whoever received it from you would have to know how to dot-source it, or would wind up actually having to modify it in order to execute those functions.

All in all, it was an acceptable situation—even if it wasn’t ideal. The situation became less acceptable for scripts that were accompanied by custom format views or type extensions, because those additional files had to be manually loaded into the shell in order to be used by the script. With Windows PowerShell v2, however, we’ve moved to a nearly ideal situation, thanks to the introduction of modules. Self-Contained Chunks of Shell Goodness A module is simply a collection of files that relate to one another. Windows PoweShell: Make a Command into a Reusable Tool | TechNet Magazine. You can repackage and reuse your efforts when it comes to Windows PowerShell commands and cmdlets. No matter how inexperienced you are with Windows PowerShell when you first start working with it, there’s always plenty of room for growth. You can start out running simple commands, work up to more-complicated commands, and eventually repackage those commands into something that looks and feels almost like a native cmdlet.

These are called advanced functions, and are informally known as “script cmdlets.” Consider a situation where you may want to retrieve some critical inventory information from a computer. You need the Windows version, BIOS serial number, service pack version and processor architecture. You can get that information with these three commands: The problem is that these commands generate three different result sets.

Accept one or more computer names as strings from the pipeline, as in: Get-Content names.txt | Get-OSInfo | ConvertTo-HTML | Out-File info.html Dealing with Errors. Windows PoweShell: Think Commands, Not Scripts | TechNet Magazine. Don’t be intimidated by the term “scripting,” because you can do a lot with Windows PowerShell using simple commands. Perception has been one of the biggest struggles Windows PowerShell has had in terms of administrator acceptance. There’s a lingering perception that the shell is a “scripting language,” akin to VBScript. While a lot of admins love what they can do with a scripting language, plenty more are turned off by the perception of complexity and a steep learning curve.

It’s a shame. Just a Script The following function will accept computer names from the command line, either as strings or in the “ComputerName” property of an input object; it will also retrieve the BIOS and OS information from each computer using Windows Management Instrumentation (WMI): Note that the parentheses force the shell to execute expressions—such as getting the BuildNumber property from the object in the $os variable—and return the result of that expression as the third parameter value of Add-Member. Windows PowerShell: Splatting | TechNet Magazine. Bundling parameters before sending them along to a command can save you time, but only if you’re using the latest version of Windows PowerShell. Where else but in the IT industry could you use a word like “splatting” in a serious, professional context?

Windshield repair, perhaps, but not many other places. In Windows PowerShell terms, splatting is a way of bundling parameters to send to a command. This is a new feature of Windows PowerShell 2.0. The Old Way Normally, you’d run commands by providing parameters to them right on the command line. Get-WmiObject –computername SERVER-R2 –class Win32_LogicalDisk –filter "DriveType=3" –credential "Administrator" You can still do that in version 2.0 of the shell, of course. Get-WmiObject –comp SERVER-R2 –cla Win32_LogicalDisk –filt "DriveType=3" –cred "Administrator" There are still positional parameters, of course. Get-WmiObject Win32_LogicalDisk –comp SERVER-R2 –filt "DriveType=3" –cred "Administrator" The New Way @{'key1'='value1';'key2'='value2'} Overview of Migration Cmdlets. Windows Server Migration Tools includes five Windows PowerShell cmdlets that let you migrate some server roles, features, operating system settings, shares, and other data from computers that are running Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, or Windows Server® 2008 R2, or to computers that are running Windows Server 2008 R2.

This topic identifies and describes the Windows PowerShell cmdlets that are part of Windows Server Migration Tools. For a complete list of operating systems that are supported by Windows Server Migration Tools, see Windows Server Migration Tools and Guides in this Help. Windows PowerShell Cmdlets (pronounced command-lets) are built-in commands included with Windows PowerShell 2.0, installed by default as part of Windows Server 2008 R2. For more information about Windows PowerShell and how cmdlets work, see the Getting Started guide for Windows PowerShell.

Running Windows PowerShell as an administrator Starting Windows Server Migration Tools. Automate Active Directory Migration Tool using Windows PowerShell « blog.powershell.no. Active Directory Migration Tool (ADMT) provides the ability to restructure Active Directory domain structures. It allows you to migrate users, groups and computers between domains, both intra-forest and inter-forest.

Features includes password migration, SID migration and security translation among several others. ADMT was recently released in version 3.2, adding support for Windows Server 2008 R2 and Managed Service Accounts. You can find the download here, and the ADMT migration guide here. ADMT provides three options on how to use it, where the first and maybe most used is the GUI: It`s wizard driven and pretty straightforward to use.

The second option is the admt.exe command line utility: In my opinion this is a pretty good example on how inconsistent various command line tools are compared to PowerShell. The third option is scripting. Based on this I`ve written a sample PowerShell script, Invoke-ADMTUserMigration, to migrate user accounts and passwords using Windows PowerShell. What's New in Active Directory Domain Services (AD DS) in Windows Server 2008 R2: Active Directory PowerShell. PowerShell Community Extensions. PowerShell Integration The goal is to provide the PSCX user with a snapin that looks and feels like PowerShell cmdlets.

We also want to avoid polluting the user's environment with unnecessary temp variables. Variables Try to limit the number of global scope variables that you create. In fact, the only global variable created by PowerShell should be $Pscx. Any exceptions should be posted for comment in the discussions group. $Pscx.Session.Cd#ForwardStack Note the naming convention for session objects. Periodically run PowerShell and examine the global variable space after running your functions and/or scripts. Variables that don't follow naming convention Any other "approved" global variables created from PSCX that don't follow the PSCX naming convention should be created using the Set-Variable cmdlet as shown in the following example in order to associate the variable with PSCX: Set-Variable Shell (new-object -com Shell.Application) -Scope global -Option ReadOnly -Description "PSCX variable"

Indented! » Blog Archive » Listing all domains in a forest. Indented! » Blog Archive » Microsoft DNS & static records. Windows 2008 has an improved user interface for DNS. The main console includes details of a records time stamp and whether or not the record is Static. Life isn’t quite so easy with Windows 2003.

However, as each static record has a time stamp set to 0 they can be found with a little work. When using an Active Directory Integrated zone records are stored within Active Directory as dnsNode objects. The Time Stamp value is encoded along with the rest of the record properties (TTL, etc) in the dnsRecord attribute on the dnsNode. The attribute is a Binary Large Object (BLOB), Microsoft do not currently publish references or maps for these attributes. WMI queries can be used as an alternative.

The DNS management console To see the current time stamp, and whether a record is dynamic or not first enable View / Advanced in the DNS console. The record below is dynamic, if the box is not ticked, and the time stamp field is blank the record is static. For example: Or VbScript PowerShell. Indented! » Blog Archive » Microsoft DNS & stale records. This post explains how to identify and report on stale records in a dynamically updated Microsoft DNS zone. The time stamp taken from a DNS record represents the numbers of hours since 01/01/1601 00:00. The value can be converted into a useful date within a script.

By default, all times are reported and tested in UTC. A stale record is a record where both the No-Refresh Interval and Refresh Interval have passed without the time stamp updating. Ordinarily stale records would be removed by a Scavenging process. These scripts may be useful if trying to asses the impact of enabling Scavenging or reducing Aging intervals. Listing stale records with VbScript This script uses a WMI query to return all A records for a domain, then it sorts through each record, echoing when the time stamp is older than our pre-defined maximum age. Listing stale records with PowerShell A timespan value is generated to represent the minimum value of TimeStamp for valid records. Reading Aging intervals with PowerShell. Indented! » Blog Archive » NTFS, PowerShell, Get-ACL & listing explicit rights.

A short script to list explicit rights assigned to a directory structure. It uses the recursive option of ls (an Alias for Get-ChildItem) to drop down through the directory structure. There are lots of little programs around that can do exactly the same thing, probably quite a few more efficiently than this. The match is not case sensitive.

If the value for $SecurityPrincipal is left blank the script will return all explicitly assigned rights. # Uses match, either a specific user / group or blank for all explicit rights $SecurityPrincipal = "chris" # The starting point $BasePath = "C:" # An array to hold the data returned $ExplicitRights = @() ForEach ($DirEntry in (Get-ChildItem $BasePath -Recurse)) { # Add an entry to the report where it matches the criteria set in the ? Indented! » Blog Archive » Modifying DNS records with WMI. The examples below show modification of Host or A records, but it is possible to extend the example to use any of the types above. Using VbScript to modify records This example demonstrates the use of WMI in VbScript to modify a record.

The first value for Modify is the TTL, by using objItem.TTL we just reinsert the existing value, only changing the IP address. ' Connect to the WMI Service Set objWMIService = GetObject("winmgmts:\\dns01\root\MicrosoftDNS") ' Run a query to get the record we want to change Set colItems = objWMIService.ExecQuery("SELECT * FROM MicrosoftDNS_AType" & _ " WHERE ContainerName='domain.example' AND OwnerName='test.domain.example', , 48) ' Loop through the results For Each objItem in colItems ' Modify the record objItem.Modify objItem.TTL, "1.2.3.4" Next If the TTL must be changed it is important to exclude the IP Address parameter, failure to do so will result in the record being deleted.

Using PowerShell to modify records. Indented! » Blog Archive » Administering Microsoft DNS in PowerShell. I have released a PowerShell 2.0 module using the WMI provider here. There are a few limitations of the interface. The properties associated with Aging are read-only and cannot be set. Several of the configuration options are not available including the option to enable GlobalNames with Windows Server 2008. Common variables The examples below use two common variables. $ServerName = "dns01" $ContainerName = "domain.example" Management Class vs Management Object Two different classes from the .NET Framework are used below. [WMIClass] creates the same object as the following example. $Scope = New-Object Management.ManagementScope("\\$ServerName\root\MicrosoftDNS") $Path = New-Object Management.ManagementPath("MicrosoftDNS_Zone") $Options = New-Object Management.ObjectGetOptions($Null, [System.TimeSpan]::MaxValue, $True) $ZoneClass = New-Object Management.ManagementClass($Scope, $Path, $Options) Creating an instance of a Management Object Exploring the Management Object Creating Zones Examples.

BITS file transfer PowerGUI PowerPack. What is this? The usual Windows way of copying files does not provide any resume option if the file transfer gets interrupted for some reason. Similarly, some of the web downloads such as SharePoint 2010 or any large file downloads from Microsoft download site, etc use the HTTP download process and hence do not have any resume option unless you use a download manager software. By default, BITS transfers files in the background, unlike other protocols that transfer files in the foreground. Background transfers use only idle network bandwidth in order to preserve the user’s interactive experience with other network applications, such as Internet Explorer.

Foreground or typical transfers are also supported. The BITS PowerPack for PowerGUI uses Background Intelligent Transfer Service 4.0 PowerShell cmdlets to transfer / download files from/to remote computers. Software Requirements1. 2. How to install this PowerPack? Power Pack Click Close to return to Admin Console How to start a web download? PowerShell Scriptomatic. Have you ever found yourself thinking, “I wonder when the Scripting Guys are going to write a PowerShell Scriptomatic”? Well, you can stop wondering: the Scripting Guys will probably never write a PowerShell Scriptomatic, a utility that would make it a snap to create WMI scripts using Windows PowerShell. Is that because the Scripting Guys don’t believe such a tool would be useful?

Heck no; we think a PowerShell Scriptomatic would be incredibly useful. So then why aren’t we going to write such a tool? One reason and one reason only: Ed Wilson has already written a PowerShell Scriptomatic for us. For those of you who don’t know the prolific Mr. Ah, good question: what exactly is a Windows PowerShell Scriptomatic? Don’t let the small size and the clean, crisp interface fool you; Ed has packed quite a bit of power into this little package. Select the namespace root\CIMV2.

Now pick a class; for example, choose Win32_BIOS. Nice, huh? See? What’s that? Atl-fs-001,atl-fs-002,atl-fs-003 Etc. Fix WMI - ScriptIT. Great PowerShell resource to watch « IgorShare Weblog.

Tools

Scripts. Shells. Learning. Modules. Providers. Debug Analyzer.NET | Welcome to Debug Analyzer.NET !