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 ();