Solved

Combining PowerShell Scripts into Single PS1

Posted on 2014-02-03
4
567 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
  • 2
4 Comments
 
LVL 34

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 68

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 18

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 68

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now