Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-02-03
11
Medium Priority
?
243 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 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


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 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.
0
A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

 

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

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

Accepted Solution

by:
Dan Craciun earned 2000 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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Screencast - Getting to Know the Pipeline

810 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