Solved

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

Posted on 2016-10-30
7
63 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 81

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 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 8

Author Closing Comment

by:K B
ID: 41866927
genius!
0

Featured Post

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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.

738 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