?
Solved

Powershell syntax - Quick question

Posted on 2013-11-05
1
Medium Priority
?
193 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 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