background preloader


Facebook Twitter

70-410 Installing and Configuring Windows Server 2012: PowerShell commands for the MCSA 70-410 Microsoft Exam. PowerTip: Import a PowerShell Module from a Shared Location - Hey, Scripting Guy! Blog. PowerShell 2.0 remoting guide: Part 12 – Using CredSSP for multi-hop authentication | Ravikanth Chaganti. I’ve published a free book on PowerShell 2.0 remoting. You can download it at: So, what is multi-hop authentication?

Well, let us look at an example to understand what is multi-hop authentication. Imagine a group of computers as shown here and you establish a remoting session from computer A (client) to computer B (server) and then from computer B, you try to create a file in a file share on computer C. CredSSP example Now, within the remoting session to computer B, we want to execute a command — as below — to create test.txt on computer C. Error without CredSSP This command results in an “Access Denied” error as shown above. How do we delegate credentials? The cmdlets to create a remoting session — Invoke-Command, Enter-PSSession and New-PSSession — have a parameter to specify the authentication method as CredSSP.

PowerShell 2.0 has Enable-WSManCredSSP, Disable-WSManCredSSP and Get-WSMANCredSSP cmdlets to manage CredSSP authentication Note: You need to run these cmdlets in an elevated prompt. PowerShell V4 – PipelineVariable Common Parameter | Keith Hill's Blog. The big new feature in Windows PowerShell 4.0 is Desired State Configuration however there are a number of other minor features one of which is a new common parameter called PipelineVariable. This parameter allows you to store the current pipeline object into the variable specified by the name you provide. This capability comes in handy when, in the process of executing commands in the pipeline, information is lost due to transformations on the objects flowing down the pipeline. For example, take the following case: Note in this output that the FileInfo objects output by Get-ChildItem got replaced with MatchInfo objects in the pipeline.

Most of time this is perfectly fine except when you’d like to access some information that was available on a pipeline object in a preceding stage of the pipeline. Get-ChildItem *.ps1 | Select-String function | Foreach {"$(Split-Path (Split-Path $_ -Parent) -Leaf)\$($_.Filename)"} The new PipelineVariable allows you to eliminate the extra Foreach command. About_Object_Creation. About_Functions_Advanced_Parameters. Emit-XML - Windows PowerShell Blog. I was writing a demo yesterday and needed a quick and dirty way to generate some XML so I wrote the function below. This highlights a couple of changes that are coming up in CTP3 that I thought I would preview. 1) The keyword "cmdlet" is going away and we'll just have "function". Notice that now you can specify the [Parameter()] attribute on parameters.

When you do that, we treat the function like a cmdlet.2) Here-strings can now NEST! Look at how simple the code becomes when you do this. I think you are going to find this to be one of the more important and powerful new features of PS V2. It provides incredible power in a very pithy (and natural) way. The first Here-String starts the XML document with a "<Object>". Function Emit-XML{Param ([Parameter(Mandatory=$true, ValueFromPipeline=$true)]$object) @"<Object>$( foreach ($p in $object |Get-Member -type *Property) { $Name = $p.Name $Value = $Object.

Here is an usage example: PowerTab - Home. Huddled Masses. Powershell collection. PowerShell and external commands done right // blog.edgylogic. Windows PowerShell is a massive step up from the VBScript horror used to manage Windows systems (I have no idea how people wrote websites with it without going mental). One of the things that annoyed me to no end though was how there seemed to be black magic involved when trying to make PowerShell execute external commands, i.e. not PowerShell cmdlets.

It is actually quite straight-forward once you wrap your head around it - it's just that we try to do things the way we did in VBScript or in OO languages, and PowerShell doesn't like that. Background I'm currently writing a script to automate creating and deleting volume shadow copies, creating a ShadowProtect image in between. This includes normal looking commands like, As well as funny looking ShadowProtect commands, The wrong way to do it If you ask Google, you'll probably get responses telling you to do this: This works, but it isn't the right way to going about mainly because it isn't the PowerShell way.

Enter echoargs.exe. Writing a PowerShell module in C#, Part 1: The basics. In this series we will cover the basics of building a Windows PowerShell binary module using C#. In the first part of the series we will build a module with just one cmdlet called Get-Salutation that will resemble the traditional “Hello World” example. We will use Visual Studio 2013 since it includes the reference assemblies for System.Management.Automation–the root namespace for Windows PowerShell–for Microsoft .Net Framework 4.0. Microsoft has not released a PowerShell 4.0 SDK with reference assemblies using Microsoft .NET Framework 4.5 at this moment. If you are planning to support Windows PowerShell 2.0 then you will need to download the Windows PowerShell 2.0 SDK from Microsoft Visual Studio 2013 Express for Windows Desktop version can also be used. Open Visual Studio and create a new project from the File menu: We give it a name and specify a location for our project.

In the Reference Module Manager click on Browse: About_Functions_Advanced_Parameters. Arrays - Powershell adding values together. Dynamic ValidateSet in a Dynamic Parameter - Windows PowerShell tips from the field. A colleague of mine needed a way to have a parameter accept only a specific set of paths. I told him this can be accomplished easily with the ValidateSet decoration on the parameter, but he then explained that what the actually needed, is to have the set dynamically defined by the sub folders in the current location, instead of a pre-defined set. This sent me out on a journey to explore the dynamic parameter world. The result of that journey, and the answer to his request is the example function below: and then you can use it: Test-DynamicValidateSet -Path [the magic happens here] \Martin.

WMI Reference. WMI (Windows Management Instrumentation) is available on all Windows systems. You can use WMI to locally or remotely query information about a computer and change settings. WMI Classes are organized into namespaces. To use WMI, you will need to know the exact name of the WMI Class you would like to query. Common WMI Classes are found in the namespace section “root/CIMV2″, but there are many other namespace locations with additional WMI classes. Browse the WMI Reference root/CIMV2root/default Additional Resources How do I use WMI? Using WMI With PowerShell To query WMI classes, you use Get-WMIObject or Get-CIMInstance. For example, to find out details about your operating system, try this: This will return only a selected subset of the properties available in this class. Beginning with PowerShell 3.0, you can use Get-CIMInstance as well: Get-CIMInstance in many instances returns better data types, for example real DateTime values instead of the WMI date and time format.

Guessing a WMI Class name. Converting CSV to Excel File. PowerShell can easily create CSV files using Export-Csv, and if Microsoft Excel is installed on your system, PowerShell can then have Excel convert the CSV file to a native XLSX Excel file. Here is some sample code. It uses Get-Process to get some data, then writes the data to a CSV file. Export-Csv uses -UseCulture to make sure the CSV file uses the delimiter your local Excel installation expects. $FileName = "$env:temp\Report" # create some CSV data Get-Process | Export-Csv -UseCulture -Path "$FileName.csv" -NoTypeInformation -Encoding UTF8 # load into Excel $excel = New-Object -ComObject Excel.Application $excel.Visible = $true$excel.Workbooks.Open("$FileName.csv").SaveAs("$FileName.xlsx",51) $excel.Quit() explorer.exe "/Select,$FileName.xlsx" Next, Excel opens the CSV file, then saves the data as XLSX file.

This works beautifully, except you may be running into an exception like this: This is a long known issue. ReTweet this Tip! IT Pro PowerShell experience | Share what I know or discovered when working with Windows PowerShell. The Lonely Administrator. Tome's Land of IT | IT Notes from the Powertoe – Tome Tanasovski. Powershell - Difference between PSObject, Hashtable and PSCustomObject. Use Copy-Property to Make it Easier to Write, Read,and Review Scripts - Windows PowerShell Blog.

Dennis Verwiej is doing some great PowerShell work over at his blog Just PowerShell It at . Recently he posted a blog entry Import Citrix Published Application in which he Imports a CSV file (previously exported) and then calls a few APIs to recreate the published application. If you at the code (in the middle of this blog entry), it the vast bulk of it is a series of lines setting properties before calling the API. I find that this is quite common - I end up writing tons of mind-numbing housekeeping code.

Because there is so much of it, it is often hard to tell whether it is right or not. For instance, did you get all the properties set or did you forget a couple? Why not leverage the special capabilities of PowerShell to make this problem go away? Below is a routine that I use for such situations. You have to deal with the situation of what to do if you try to set a property that doesn't exist. Leveraging this routine allows you to transform this code: Powershell: How to get get date-1 formatted mm-dd-yyyy? PowerShell One-Liners: Collections, Hashtables, Arrays and Strings.

The way to learn PowerShell is to browse and nibble, rather than to sit down to a formal five-course meal. In his continuing series on Powershell one-liners, Michael Sorens provides Fast Food for busy professionals who want results quickly and aren't too faddy. Part 3 has as its tasty confections Collections, Hashtables, arrays and strings. This series is in four parts: This is part 3 Notes on Using the Tables A command will typically use full names of cmdlets but the examples will often use aliases for brevity. Example: Get-Help has aliases man and help.

This has the side benefit of showing you both long and short names to invoke many commands. Most tables contain either 3 or 4 columns: a description of an action; the generic command syntax to perform that action; an example invocation of that command; and optionally an output column showing the result of that example where feasible. Many actions in PowerShell can be performed in more than one way.

Contents Collections (Arrays) String Search. Custom objects default display in PowerShell 3.0 | Shay Levy. April 13, 2012 In PowerShell 3.0 we can now create new custom objects using a hash table. PS> [PSCustomObject]@{ One = 1 Two = 2 Three = 3 Four = 4 Five = 5 } One : 1 Two : 2 Three : 3 Four : 4 Five : 5 Behind the scenes, PowerShell creates a hash table and wraps it a PSCustomObject. It is way faster than using the New-Object cmdlet and it also provides consistency, while maintaining backwards compatibility.

Another benefit of using PSCustomObject over New-Object is property order. PSCustomObject preserve the order of defined properties while New-Object doesn’t. PS > [PSCustomObject]@{One=1;Two=2;Three=3} One Two Three --- --- ----- 1 2 3 PS > New-Object PSObject -Property @{One=1;Two=2;Three=3} One Three Two --- ----- --- 1 3 2 Notice that when the new PSCustomObject is returned, all of its properties are displayed in the console. We can also define the properties of the type that will be displayed by default with the formatting cmdlets. Community: VMware vSphere™ PowerCLI. LucD notes - My PowerShell ramblings. Beyond Export-Csv: Export-Xls - LucD notes.

Warning: this post has no “virtual” content ! This time I post a function that allows you to export your data to a “real” spreadsheet (XLS format) instead of a CSV file. The reason for posting this function was a series of threads in the PowerCLI Community by Suresh. Over several threads he has been collecting scripts that create various reports on his vSphere environment. Ultimately he wanted to have a spreadsheet where each report would be stored on a worksheet. PowerShell has the very handy Export-Csv cmdlet to create CSV files but afaik nothing for creating XLS files That’s when I decided to “pimp” my XLS creating function I already had to something more suited for general use.

The function uses Excel through a COM object. Update June 16th 2010: the function has a new parameter -ChartType which allows the addition of a chart on the worksheet. Update November 2nd 2010: a complete overhaul of the function The script Annotations Line 112-117: The function tests if the XLS file exists or not. PowerShell Code Repository. Don't underestimate the power of ToString(IFormatProvider) Windows PowerShell Tip: Formatting Dates and Times. Here’s a quick tip on working with Windows PowerShell.

These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know. Formatting Dates and Times Having recently returned from a vacation in Europe, one of the Scripting Guys was reminded of the following fact: just because Americans do something, that doesn’t mean the rest of the world does that very same thing, or at least not in the very same fashion. This is true when it comes to putting ice in drinks; it’s also true when it comes to formatting dates and times. For example, in the US we use a 12-hour clock. What does that mean?

The same sort of thing is true when it comes to calendar days. So what does that mean to you, the Windows PowerShell scripter? Thursday, August 30, 2007 11:13:51 AM That’s fine, but what if all you really wanted was a value like this: Hey, no problem. Quick Formatting Etc., etc. Custom Formatting We can do that: PERF-02 Consider trade-offs between performance and readability · Penflip. Performance is not the only reason you write a script. If a script is expected to deal with ten pieces of data, a 30% performance improvement will not add up to a lot of actual time. It's okay to use a slower-performing technique that is easier to read, understand, and maintain - although "easier" is a very subjective term. Of the two commands above, any given person might select either of them as being "easier" to understand or read. This is an important area for people in the PowerShell community. For example: $content = Get-Content file.txt ForEach ($line in $content) { Do-Something -input $line} Most folks will agree that the basic aesthetics of that example are good.

Now consider this alternate approach: Get-Content file.txt |ForEach-Object -Process { Do-Something -input $\_} As described elsewhere in this guide, many folks in the community would dislike this approach for aesthetic reasons. This example reverts back to a native PowerShell approach, using commands and parameters. StreamReader.Peek Method (System.IO)

Get-MailboxReport.ps1/Get-MailboxReport.ps1 at master · cunninghamp/Get-MailboxReport.ps1. About_Parameters. About_Functions_Advanced_Parameters. Richard Siddaway's Blog. The April 2015 WMF 5.0 preview brings new functionality in the shape of cmdlets for working directly with the clipboard. You use Set-Clipboard to put data onto the clipboard £> get-command Set-Clipboard -Syntax Set-Clipboard [-Append] [-WhatIf] [-Confirm] [<CommonParameters>] Set-Clipboard [-Value] <string[]> [-Append] [-WhatIf] [-Confirm] [<CommonParameters>] Set-Clipboard -Path <string[]> [-Append] [-WhatIf] [-Confirm] [<CommonParameters>] Set-Clipboard -LiteralPath <string[]> [-Append] [-WhatIf] [-Confirm] [<CommonParameters>] For instance: Set-Clipboard -Value "test" You can add text or files to the clipboard.

The contents of the clipboard are retrieved using Get-Clipboard £> Get-Clipboard -Raw test As well as raw data you can pull data in a number of formats. £> Get-Command Get-Clipboard -Syntax Get-Clipboard [-Format <ClipboardFormat>] [-TextFormatType <TextDataFormat>] [-Raw] [<CommonParameters>] Format can be one of: Text, FileDropList, Image, Audio These all produce the same result: