Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Combining PowerShell Scripts into Single PS1

Posted on 2014-02-03
4
Medium Priority
?
612 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 2000 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 71

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 71

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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