Solved

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

Posted on 2016-10-30
7
35 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 78

Expert Comment

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

Author Comment

by:K B
Comment Utility
I am trying to create the powershell file.
0
 
LVL 5

Author Comment

by:K B
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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 5

Author Comment

by:K B
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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 5

Author Closing Comment

by:K B
Comment Utility
genius!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now