Solved

Create Powershell code with Powershell (or Excel Macro) for a DSC configuration

Posted on 2016-10-30
7
48 Views
Last Modified: 2016-10-31
I have a CSV of Windows Features (pictured is a partial list) assume a column 'C' named "Ensure" populated with "Present" ...

csv.png

...that I would like to be read in - and then output into text that looks like this (below is the first two rows of the CSV pictured above, converted into the format I would need):

        
         WindowsFeature FileandStorageServices
        {
            Ensure = "Present"
            Name   = "FileAndStorage-Services"
        }
         WindowsFeature FileandiSCSIServices
        {
            Ensure = "Present"
            Name   = "File-Services"
        }

Open in new window


Notice how the DisplayName column would need to have any spaces, special characters or punctuation removed; Leaving only A-Z or 0-9 characters.

How could I automate the creation of this text (that will ultimately live as Powershell code).

Thank you!
0
Comment
Question by:K B
  • 4
  • 2
7 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 41866430
Why are you using excel or a csv for this?  DSC uses mof files that are compiled from the powershell script file
0
 
LVL 6

Author Comment

by:K B
ID: 41866676
I am trying to create the powershell file.
0
 
LVL 6

Author Comment

by:K B
ID: 41866702
Let me rephrase. I am attempting to create a powershell file from a customer provided CSV  the CSV is generated from executing get-windowsfeature from a machine with the desired state.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 69

Expert Comment

by:Qlemo
ID: 41866782
Import-CSV C:\Temp\EE\CustomDSC..csv |
  ? { $_.Ensure -eq 'Present' } |
  % {
     Write-Output $(@"
        WindowsFeature {0}
        {
            Ensure = "Present"
            Name   = "{1}"
        }
"@ -f ($_.DisplayName -replace '[^A-Za-z0-9]'), $_.Name)
  } | Out-File CustomDSC.txt
[/code]
0
 
LVL 6

Author Comment

by:K B
ID: 41866823
Qlemo,

Thank you very much for your reply!

I am running into an error when I run your code:

Import-CSV C:\scripts\features.csv |
  ? { $_.Ensure -eq 'Present' } |
  % {
     Write-Output $(@"
        WindowsFeature {0}
        {
            Ensure = "Present"
            Name   = "{1}"
        }
"@ -f ($_.DisplayName -replace '[^A-Za-z0-9]'), $_.Name)
  } | Out-File CustomDSC.txt

Open in new window


Here is the error:

2016-10-31_0844.png
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 41866921
Oh, curly braces inside a format string have a special meaning. Should have expected that ... Solution: We just double the literal curly braces.
Import-CSV C:\scripts\features.csv |
  ? { $_.Ensure -eq 'Present' } |
  % {
     Write-Output $(@"
        WindowsFeature {0}
        {{
            Ensure = "Present"
            Name   = "{1}"
        }}
"@ -f ($_.DisplayName -replace '[^A-Za-z0-9]'), $_.Name)
  } | Out-File CustomDSC.txt

Open in new window

0
 
LVL 6

Author Closing Comment

by:K B
ID: 41866927
genius!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question