Solved

Combining PowerShell Scripts into Single PS1

Posted on 2014-02-03
4
596 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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…

710 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