Solved

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

Posted on 2014-02-03
11
230 Views
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.

Example
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?

Thanks!
0
Comment
Question by:BzowK
  • 6
  • 5
11 Comments
 
LVL 34

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


HTH,
Dan
test.csv
0
 

Author Comment

by:BzowK
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...
test1.csv
psprompt.png
0
 
LVL 34

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

Author Comment

by:BzowK
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...
psresult2.png
0
 
LVL 34

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".
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:BzowK
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.  
Prompt
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
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
0
 
LVL 34

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'))}
0
 

Author Comment

by:BzowK
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.
0
 
LVL 34

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}
0
 

Author Comment

by:BzowK
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!
0
 
LVL 34

Accepted Solution

by:
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

18 Experts available now in Live!

Get 1:1 Help Now