Solved

Powershell syntax - Quick question

Posted on 2013-11-05
1
186 Views
Last Modified: 2013-11-20
Just looking to shrink some code, the below two snippets I've done seem tacky and I'm sure that can be shrunk a lot. Just for a learning curve really, Suggestions?

Snippet 1
  
  $nvc = new-object System.Collections.Specialized.NameValueCollection
  
  # add all job properties of $d to the NVC
  $nvc.Add('id',$d.id)
  $nvc.Add('ref_id',$d.ref_id)
  $nvc.Add('ref_text',$d.ref_text)
  $nvc.Add('url',$d.url)
  $nvc.Add('user',$d.user)
  $nvc.Add('email',$d.email)
  $nvc.Add('email_title',$d.email_title)
  $nvc.Add('email_body',$d.email_body)
  $nvc.Add('next_run',$d.next_run)
  $nvc.Add('last_run',$d.last_run)
  $nvc.Add('last_result',$d.last_result)
  $nvc.Add('type',$d.type)
  $nvc.Add('time',$d.time)
  $nvc.Add('day',$d.day)
  $nvc.Add('status',$d.status)

Open in new window


Snippet 2
  Remove-Job -State Completed
  Remove-Job -State Failed
  Remove-Job -State Stopped
  Remove-Job -State Suspended

Open in new window


Thanks
0
Comment
Question by:mikeyd234
1 Comment
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39630017
For Snippet 1, try..
$nvc = new-object System.Collections.Specialized.NameValueCollection
# add all job properties of $d to the NVC
$d | 
   Get-Member -MemberType Property | 
       select -ExpandProperty Name | % {
	$nvc.Add($_,"d.$_")
	}

Open in new window

For Snippet 2, try..
Get-Job | ? {$_State -Notmatch "(NotStarted|Running|Blocked)"} | Remove-Job

or 

"Completed","Failed","Stopped","Suspended" | % { Remove-Job -State $_ }

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

13 Experts available now in Live!

Get 1:1 Help Now