Skip to main content

Posts

Showing posts with the label Powershell 4

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData. Other Serializing methods In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in: Export-CliXml ConvertTo-JSON Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types: (Hashdata.Object.ps1) You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate t...

Operations Manager–Gateway servers and failover with Powershell dynamic parameters

Been thinking about doing a post about failover in SCOM with gateway servers. The idea is that you have several (2 or more) SCOM management servers in your management group. As you start to build your first gateway server, the question about failover/high availability for the gateway comes walking down the street. No problem, powershell comes to the rescue and provides you with a cmdlet you can use – Set-SCOMparentManagementServer . All nice and dandy, however you can only set the primary management server or the failover server, not both at the same time. In addition you have to provide the cmdlet with gateway and management server objects. You cannot just specify their names. These are by no means huge issues, however I wanted to play around with the dynamic parameter in Powershell and decided to get to work. Just a fair warning. Dynamic parameters are not pretty in Powershell, however if you put a little effort into the logic, it is quite reasonable to understand. What do...

Desired State Configuration - ConfigurationData

Update 12.10.2016 - More functions added to repro some while ago This module has been laying dormant in my BitBucket repro. Looks like people are realizing that DSC is about to become a thing. Need some feedback if this is a way of handling your Configuration data in DSC. I will publish it to the PowershellGallery if people find it useful. Should probably do a code review as well since this was written over 2 years ago :-0. Update 19.04.2014 - Module (beta) added to bitbucket here When you start to work with DSC, you quickly realize that separating the infrastructure from the configuration is a huge benefit and somewhat of a curse. You start to read blogs and webposts explaining how you can specify the configurationdata to you configuration, however for the people that does not speak powershell (yes they are out there) and/or still don’t like it, it looks kind of “untidy”. Example: Yes, Beelzebub has joined the party, pure evil. Just to explain ConfigurationData in Desired ...

Desired state configuration – xComputer domain join

Here we go again. This is just a short post on how to use the resource to join a computer to the domain. Steps we will perform: Download the resource from here . Follow the instructions to add it to the modules folder on you target computer. Create configuration data Build a very short configuration Here is the configuration data we will use: $ConfigData = @{ AllNodes = @( @{ NodeName = "*" PSDscAllowPlainTextPassword = $true } @{ NodeName = "localhost" } ); NonNodeData = @{           domainName = "try.local"       } } A few points to note here: We have a node called “*”. It is kind of a “wildcard”, however wildcards are not supported in DSC. All key/values you specify in this node, will get inherited by all nodes. That is why I do not need to specify the PSDscAllowPla...

Powershell – Get-EvntLog

Today we are doing 2 things, or actually 3 things: Building a custom Get-EvntLog function built upon the standard Get-WinEvent cmdlet Using DefaultDisplayProperties Using an embedded function in an function (why, because why not, we can?) Main reasons for doing this: Bug in .net and/or Get-WinEvent for any other culture than en-us (I use no-nb), The message property is returned as $null if I use Get-WinEvent Speed – Get-Eventlog is fine, however I want speed like we get with Get-WinEvent I can work around the bug in Get-WinEvent by temporarily switching to the culture en-us. When I have used Get-WinEvent and processed the results, I reset the culture to the original state. No harm done. A few words about the custom object function (Create-CustomObject). It takes 2 parameters; Properties ([string[]]) and DefaultDisplayProps([string[]]). At the end it spits out a new object (PSobject) with the DefaultDisplayPropertySet’ configured. To demonstrate: Okay, so ...

Powershell – The DSC book

Don Jones (@concentrateddon) is writing this awesome book about DSC. Please help him review the contents and make suggestions. You may leave feedback on the Powershell.org/FAQ web page (just create a new post there). I have been working as an consultant in IT since 1999, I have done a lot of strange “things” (work related that is). I have done my fair share of vb-scripting and small GUIs or console application development using vb.net etc. I have even created an automation solution/"framework" with powershell and .net (.net Forms). I have been using powershell since exchange 2007, however the last 2 years I have been a “heavy” user writing advanced scripts/functions, dot-sourcing, creating small modules etc. Full disclosure; I am not exaggerating when I say I am super excited about DSC. My comments (in random order, when I say you/your I target Mr Jones, I think :-)) DSC resources – As you mention Microsoft has released 2 waves of additional resources for DSC. Wh...

Powershell – Another windows registry “module” with a little remote trick

Well it is time for another pure powershell post. This time I have been working with the registry. Feels like I have been living in different hives no matter what area I have been working in in the last 15 years. It has become rather pleasant if I could say so compared to the old VBSCRIPT days and before that reg.exe and ini files. Fist a little history and a discussion. You have a couple of options in regards to reading and/or editing settings in the registry Use the built-in PS-drives (gives you access to HKLM and HKCU) Codeplex module (search on codeplex or in your favorite searchengine – most used and referenced: http://psrr.codeplex.com/ ) Use the .Net class (normally [microsoft.win32.registrykey]). This gives you access to all the hives in the registry Powershell Desired State Configuration (There is a built-in resource to access the registry) WMI/CIM Which one you pick depends upon your current needs and what you want to accomplish. If you are also in a hu...

More Powershell DSC resources released

  The PowerShell Blog has released information about the second wave of resources to use in your Desired State Configuration. Looks like the guys have been really busy  and released some neat resources for us. Here is the list: Resource Description Module Name Link xADDomain Create and manage an Active Directory Domain xActiveDirectory click here xADDomainController Create and manage an AD Domain Controller xActiveDirectory click here xADUser Create and manage an AD User xActiveDirectory click here xWaitForADDomain Pause configuration implementation until the AD Domain is available.  Used for cross machine synchronization. xActiveDirectory click here xSqlServerInstall Create and manage a SQL Server Installation. xSqlps click here xSqlHAService Create and manage a SQL High Availability Service. xSqlps click here xSqlHAEndpoint Create and manage the endpoint used to access a SQL High Availability Group. xSqlps ...

Powershell - Desired State Configuration (DSC) - Is it desired?

Finally found some time to play around with DSC on 2012 R2 and with the release of Windows Management Framework 4 , I can finally play with 2008 R2, Windows 7 and Windows 8 clients (if you upgrade to 8.1). As always with new releases of the Management Framework, some server configurations/applications do not play nicely with the new framework: System Center 2012 Configuration Manager ( not including SP1) System Center Virtual Machine Manager 2008 R2 (including SP1) Microsoft Exchange Server 2013 , Microsoft Exchange Server 2010 and Microsoft Exchange Server 2007 Microsoft SharePoint 2013 and M icrosoft SharePoint 2010 Windows Small Business Server 2011 Standard Please also note that .Net 4.5 is required before you install the Framework, otherwise the Framework installation will actually silently fail. As I previously wrote, if you were hoping for support for Windows Server 2008 support, you are not getting it. You will have to fall back to Windows Management Fram...