PowerShell tips Use the New-module command to create objects dynamically _powershell

Source: Internet
Author: User

Remember how to create objects dynamically in PowerShell? Today to share the method dare not boast tall, but also enough to let New-object feel ashamed.

Background
There is a management Pack, called "Microsoft.SystemCenter.OperationsManager.SummaryDashboard", in System Center Operation Manager. In this MP, there is a discovery called "Collect agent Configurations". A script is used in the workflow that uses the New-module command.

New-module is the dynamic generation of a module component in memory. It's a bit overqualified to come up with a definition object.

Exercise

$PLA = new-module {
 $ name = ' PLA '
 $ Military District = @ (' Shenyang Military District ', ' Beijing Military area ', ' Jinan military area ', ' Nanjing Military Area ', ' Guangzhou Military area ', ' Chengdu Military Area ', ' Lanzhou Military District ')
 $ class = @ (' Navy ', The ' Air Force ', ' second artillery '
 
 function defends the party
 {return
  $true
 } function to defend the
 
 people {return
 $null
 }
 
 Function anti-flood rescue
 {return
 $true
 }
 
 function Earthquake relief
 {return
 $true
 }
 
 function Confirm class
 {
 param ($ A Class)
 if ($this. class. Contains ($ A Class)) {return
  $true
  } return
 $false
 }
 export-modulemember-variable *-function *
 
} -ascustomobject


Name of ps> $PLA Military Region----         --{Navy, Air Force, second artillery} {Shenyang Military Region, Beijing military area, Jinan Military Region, Nanjing Military Region ...} PLA ps> $PLA. Confirm Class (' army ') False ps> $PLA | Get-member TypeName:System.Management.Automation.PSCustomObject Name membertype Definition----- -------------------equals method bool Equals (System.Object obj) GetHashCode method int Gethashcod E () GetType method Type GetType () ToString Method String ToString () class Noteproper Ty system.object[] Arms =system.object[] Military District noteproperty system.object[] Military Region =system.object[] name Noteproperty System.St       
Ring name = People's Liberation Army defends the peoples Scriptmethod System.Object defend the People ();       
Defend party Scriptmethod System.Object Defend Party ();       
Flood rescue Scriptmethod System.Object Flood Rescue ();
 Earthquake relief Scriptmethod System.Object Earthquake Relief ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.