Solved

Powershell syntax - Quick question

Posted on 2013-11-05
1
190 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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ā€¦
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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