Solved

Combining PowerShell Scripts into Single PS1

Posted on 2014-02-03
4
603 Views
Last Modified: 2015-07-14
Hey Guys -

Got a newb, PowerShell 101 question for you...

I have a string that I use to advertise advertisements to collections in SCCM 2012.  One of the strings is below:

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

I have duplicated it many times and changed variables so that each line sets a new, different advertisement.  I tried putting all of these lines into a text file (using notePad++) with each string on a line then saved it as a PS1.  When I ran it in PowerShell, though, it failed.  Once it ran, but froze half way through.  If I copy / paste / run each line separately, it works great.

How do I combine these to work properly in a single script?  Thanks
0
Comment
Question by:BzowK
[X]
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
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39830681
Use a Start-Sleep -s 5 between your commands and see if it makes a difference.

This will force powershell to wait for 5 seconds before running the next command.

HTH,
Dan
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39830912
That shouldn't be an issue at all. It might "pause" for some time in-midst of execution, if the server has to do some work, but then continue. However, allowing for some rest by inserting sleeps is a good idea.
BTW, one way to implement multiple commands with the same parameters is by "splashing". That is you put the common parameters into a hash table. That allows for better readability:
$parms = @{
  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"
}
Start-CMSoftwareUpdateDeployment @parms -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"

Open in new window

You would have to provide only those parameters changing with each command that way.
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 40880918
I guess semi colon  missed in between the hash values
$parms = @{
  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"
}
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40881092
You guess wrong, Raheman. The semi-colon is a command separator, and required only if you put hash key/value pair or commands on a single line:
   Get-ChildItem; Get-User
is the same as
   Get-ChildItem
   Get-User
The same applies to hash tables.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
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 …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 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