Solved

Powershell syntax - Quick question

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

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

Suggested Solutions

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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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