?
Solved

Powershell syntax - Quick question

Posted on 2013-11-05
1
Medium Priority
?
192 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
[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
1 Comment
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 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

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

A procedure for exporting installed hotfix details of remote computers using powershell
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

764 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