Method to Duplicate String Multiple Times While Substituting Specific Variables For Each

Posted on 2014-02-03
Last Modified: 2014-02-20
Good Afternoon All -

I have a PowerShell string that I use with SCCM 2012 to advertise monthly updates.  The string (below) is a single line, but fairly long.

Start-CMSoftwareUpdateDeployment -SoftwareUpdateGroupName "Workstation Related - Feb 2014 Deployment" -CollectionName "Workstation Patch Management - Window #8 - 3rd Wednesday - Auto Restart" -DeploymentName "Workstation Patch Management - Window #8 - 3rd Wednesday - Auto Restart Feb 2014" -DeploymentType Required -VerbosityLevel OnlySuccessAndErrorMessages -TimeBasedOn UTC -DeploymentAvailableDay 2014/2/19 -DeploymentAvailableTime 6:00 -DeploymentExpireDay 2014/2/19 -DeploymentExpireTime 6:00 -UserNotification DisplaySoftwareCenterOnly -SoftwareInstallation $False -AllowRestart $False -RestartServer $False -RestartWorkstation $False -ProtectedType NoInstall -UnprotectedType NoInstall

What I'm wanting to do is find a way to duplicate this string, but be when doing so change some variables for each - possibly from a list.

The collection name defined in the string above is "Workstation Patch Management - Window #8 - 3rd Wednesday - Auto Restart."  I have a list of a couple hundred collections which I'd like to use this script for.  How may I easily duplicate the string above in a text editor (or other) and replace just that part of the string on each line with a line from the list of collections?  Doing so would result in making each command string similar, but still each have different collection names.

Per string, I would have to change the Collection Name,  Deployment Name, Time, and Date.

Is there any way to do this in Excel or other app without causing more frustration than it's worth?

Question by:BzowK
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
  • 6
  • 5
LVL 35

Expert Comment

by:Dan Craciun
ID: 39830658
Why not create a csv, like the one attached (you can use Excel to create it), with 4 fields (Collection Name, Deployment Name, Time and Date) and then reference it in your script.

something like
import-csv test.csv | % {Start-CMSoftwareUpdateDeployment -SoftwareUpdateGroupName $_.'Collection Name' -DeploymentName $_.'Deployment Name' etc etc}

Open in new window


Author Comment

ID: 39830774
Thanks Dan.  I tried that, but when I copied / pasted / ran the string I created, it returned immediately to a new prompt.  Below is the string I tried and attached is the CSV file.  What did I do incorrectly?  Thanks!

import-csv d:\test1.csv | % {Start-CMSoftwareUpdateDeployment -SoftwareUpdateGroupName $_.'Software Update Group Name' -CollectionName $_.'Collection Name' -DeploymentName $_.'Deployment Name' -DeploymentType $_.'Deployment Type' -DeploymentAvailableDay $_.'Available Day' -DeploymentAvailableTime $_.'Available Time' -DeploymentExpireDay $_.'Expire Day' -DeploymentExpireTime $_.'Expire Time' -RestartServer $_.'Restart Servers' -RestartWorkstation $_.'Restart Workstations'

I also tried it with "import-csv test1.csv |" and got the same result...
LVL 35

Expert Comment

by:Dan Craciun
ID: 39830824
What was the intended result? That command simply iterates through the test1.csv file, executes every line and then exits.
No output should mean no errors in this case.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 39830890
Well, I looked again and realized that I didn't end it with a "}"  I added it, then ran it again.  This time, I got the attached error.  When running the command successfully with the SUG I'm advertising, it usually does nothing for about 5 minutes then goes back to a command line.  I can then look in the SCCM console and see the advertisement has been created.  With this, nothing so far...
LVL 35

Expert Comment

by:Dan Craciun
ID: 39830918
Powershell does not interpolate boolean values in this case and sends a string as a parameter to -restartserver.

When needing booleans you have to do a type cast. In this case this would be:
-RestartServer [System.Convert]::ToBoolean($_.'Restart Servers') -RestartWorkstation [System.Convert]::ToBoolean($_.'Restart Workstations') 

Open in new window

Make sure you change your csv so the values are "True" or "False", not "$True" or "$False", because [System.Convert]::ToBoolean expects "True" or "False".

Author Comment

ID: 39830975
Thanks.  I actually found that out after posting after a bit of Googling.  I was playing with a string fix, but then saw you replied so just tried yours.  Unfortuately, it still failed with the same error.  Just so we are on the same page, here's what I did and the results:

I edited my command with your string so that it was:
import-csv d:\test.csv | % {Start-CMSoftwareUpdateDeployment -SoftwareUpdateGroupName $_.'Software Update Group Name' -CollectionName $_.'Collection Name' -DeploymentName $_.'Deployment Name' -DeploymentType $_.'Deployment Type' -DeploymentAvailableDay $_.'Available Day' -DeploymentAvailableTime $_.'Available Time' -DeploymentExpireDay $_.'Expire Day' -DeploymentExpireTime $_.'Expire Time' -RestartServer [System.Convert]::ToBoolean($_.'Restart Servers') -RestartWorkstation [System.Convert]::ToBoolean($_.'Restart Workstations')

When I ran it, PowerShell went to a ">>" prompt which I haven't seen before.  
I realized that there wasn't a "}" at the end to close it (assuming one was needed) so added it and ran it again.  This time I got a similar error
I timed the command which does not use a CSV and it takes ~90 seconds to complete.  I tried the first one again, but it's been sitting for 3 minutes now and still at that empty prompt.  Surely it's something small that is a typo or that I'm leaving out.

Any ideas?  Thanks
LVL 35

Expert Comment

by:Dan Craciun
ID: 39831006
Yeah, my mistake. You need to enclose conversions between parentheses, or they are output-ed as strings:

import-csv d:\test.csv | % {Start-CMSoftwareUpdateDeployment -SoftwareUpdateGroupName $_.'Software Update Group Name' -CollectionName $_.'Collection Name' -DeploymentName $_.'Deployment Name' -DeploymentType $_.'Deployment Type' -DeploymentAvailableDay $_.'Available Day' -DeploymentAvailableTime $_.'Available Time' -DeploymentExpireDay $_.'Expire Day' -DeploymentExpireTime $_.'Expire Time' -RestartServer ([System.Convert]::ToBoolean($_.'Restart Servers')) -RestartWorkstation ([System.Convert]::ToBoolean($_.'Restart Workstations'))}

Author Comment

ID: 39831014
No problem.  Thanks.  Just tried it, but still must be missing something.  The command took just fine and it immediately returned to a prompt.  The problem is, that it's usually 90 seconds before it returns to the prompt when running w/o using a CSV.  Waiting now to see if it perhaps moved it to a background process and will show up in a couple of minutes.
LVL 35

Expert Comment

by:Dan Craciun
ID: 39831021
BTW, when troubleshooting you can just put an "echo" before your command and it will output it to the shell instead of executing.
That's a quick way to see what parameters are being passed.

In this case it would be
import-csv d:\test.csv | % {echo Start-CMSoftwareUpdateDeployment etc etc}

Author Comment

ID: 39831052
Thanks - Even though I don't have results yet, I think it will work.  I just tried running the command w/o CSV and it's not working right now for whatever reason and immeditely returning to a prompt too.  Don't know why - hope it's nothing I did when testing.  Tried closing Powershell and re-opening, but same result.  Hopefully will be able to test later on once this is resolved as I can't restart it for a while since others are logged into the system.

Any ideas on that?  Thanks again for your help!
LVL 35

Accepted Solution

Dan Craciun earned 500 total points
ID: 39831069
One of the previous commands might be still running/hung.
From what I know, Powershell does not attach itself to the commands you run through it, so if you issue a command that is still running when you close Powershell the command will be unaffected.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

726 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