Solved

Combining PowerShell Scripts into Single PS1

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

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 69

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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