?
Solved

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

Posted on 2016-10-30
7
Medium Priority
?
78 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 82

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 8

Author Comment

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

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
Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

 
LVL 70

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 8

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 70

Accepted Solution

by:
Qlemo earned 2000 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 8

Author Closing Comment

by:K B
ID: 41866927
genius!
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

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