Solved

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

Posted on 2016-10-30
7
40 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
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 5

Author Comment

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

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

 
LVL 68

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 5

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 68

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 5

Author Closing Comment

by:K B
ID: 41866927
genius!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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 …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

947 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

20 Experts available now in Live!

Get 1:1 Help Now