• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

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

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
BzowK
Asked:
BzowK
  • 6
  • 5
1 Solution
 
Dan CraciunIT ConsultantCommented:
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
 
BzowKAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
BzowKAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
BzowKAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
BzowKAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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
 
BzowKAuthor Commented:
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
 
Dan CraciunIT ConsultantCommented:
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

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now