Created
February 23, 2016 20:18
-
-
Save adamdriscoll/479304e63979a5a6b986 to your computer and use it in GitHub Desktop.
VS Code BP Foreach Variable Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Add-RandomScsmBulkData { | |
[CmdletBinding()] | |
param( | |
[String[]]$Classes, | |
[String[]]$Relationships, | |
[int]$DataSetSize = 200) | |
$itemCount = 0 | |
$totalItems = ($classes.Length * $DataSetSize) + ($Relationships.Length * $DataSetSize) | |
foreach($Class in $Classes) | |
{ | |
for($i = 0; $i -lt $DataSetSize; $i++) | |
{ | |
Write-Progress -Activity "Creating objects for class [$Class]" -Status "Item [$i] of [$DataSetSize]" -PercentComplete (($itemCount / $totalItems) * 100) | |
New-RandomScsmObject -ClassName $Class | |
$itemCount++ | |
} | |
} | |
foreach($Relationship in $Relationships) | |
{ | |
for($i = 0; $i -lt $DataSetSize; $i++) | |
{ | |
Write-Progress -Status -Activity "Creating relationships for relationship [$Relationship]" -Status "Relationship [$i] of [$DataSetSize]" -PercentComplete (($itemCount / $totalItems) * 100) | |
New-RandomScsmRelationship -RelationshipName $Relationship | |
$itemCount++ | |
} | |
} | |
} | |
function New-RandomScsmObject { | |
param([Parameter(Mandatory)] | |
[string]$ClassName) | |
$Class = Get-SCSMClass -Name $ClassName | |
$PropertyHashTable = @{} | |
$Properties = Get-ScsmObjectProperty -Class $Class | |
Foreach($Property in $Properties) | |
{ | |
if (-not $Property.Required) | |
{ | |
if ((Get-Random -Minimum 0 -Maximum 100) -gt 90) | |
{ | |
Write-Verbose "Skipping optional property $($Property.Name)" | |
continue | |
} | |
} | |
if ($Property.Key) | |
{ | |
$PropertyHashTable[$Property.Name] = "{0}" | |
Write-Verbose "$($Property.Name) = {0}" | |
continue | |
} | |
$Value = New-RandomScsmObjectPropertyValue -Property $Property | |
$PropertyHashTable[$Property.Name] = $Value | |
Write-Verbose "$($Property.Name) = $Value" | |
} | |
New-SCSMObject -Class $Class -PropertyHashtable $PropertyHashTable | Out-Null | |
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
#THIS IS WHERE I'M SETTING MY BREAK POINT | |
} | |
function New-RandomScsmRelationship { | |
param([Parameter(Mandatory)] | |
[string]$RelationshipName, | |
[int]$MaxCardinality = 10) | |
$Relationship = Get-SCSMRelationshipClass -Name $RelationshipName | |
if ($Relationship -eq $null) | |
{ | |
throw "Relationship class $RelationshipName not found." | |
} | |
$SourceClass = Get-SCSMClass -Id $Relationship.Source.Class.Id | |
$TargetClass = Get-SCSMClass -Id $Relationship.Target.Class.Id | |
#$SourceMinCardinality = $Relationship.Source.MinCardinality | |
$SourceMaxCardinality = [Math]::Min($Relationship.Source.MaxCardinality, $MaxCardinality) | |
#$TargetMinCardinality = $Relationship.Target.MinCardinality | |
$TargetMaxCardinality = [Math]::Min($Relationship.Target.MaxCardinality, $MaxCardinality) | |
$SourceObjects = Get-SCSMObject -Class $SourceClass -MaxCount $SourceMaxCardinality | |
$TargetObjects = Get-SCSMObject -Class $TargetClass -MaxCount $TargetMaxCardinality | |
foreach($SourceObject in $SourceObjects) | |
{ | |
foreach($TargetObject in $TargetObjects) | |
{ | |
New-SCSMRelationshipObject -Relationship $Relationship -Source $SourceObject -Target $TargetObject | Out-Null | |
} | |
} | |
} | |
function New-RandomScsmObjectPropertyValue { | |
param($Property) | |
$TypeName = $Property.SystemType.Name | |
switch ($TypeName) | |
{ | |
"decimal" { New-RandomDecimal $Property } | |
"Int32" { [int](Get-Random -Min $Property.MinValue -Max $Property.MaxValue)} | |
"string" { [string](New-RandomString $Property) } | |
"enum" { New-RandomEnum $Property } | |
"bool" { (0..1 | Get-Random)} | |
default {throw "Unsupported data type! $TypeName"} | |
} | |
} | |
function New-RandomDecimal { | |
param($Property) | |
$Random = New-Object System.Random | |
$Double = $Random.NextDouble() | |
$Property.MinValue + ($Double * ($Property.MaxValue - $Property.MinValue)) | |
} | |
function New-RandomEnum { | |
param($Property) | |
$Enumeration = Get-SCSMEnumeration -Name "$($Property.Type)" | |
if ($Enumeration) | |
{ | |
throw "Enumeration $($Property.Type) not found!!" | |
} | |
$Enumeration | Where {$_.Name -ne $Property.Type } | Get-Random | Select -Expand Id | |
} | |
function New-RandomString { | |
param($Property) | |
$Length = Get-Random -Maximum $Property.MaxLength -Minimum $Property.MinLength | |
$set = "abcdefghijklmnopqrstuvwxyz0123456789".ToCharArray() | |
$SB = New-Object System.Text.StringBuilder | |
for ($x = 0; $x -lt $Length; $x++) { | |
$SB.Append(($set | Get-Random)) | |
} | |
$SB.ToString() | |
} | |
function Get-ScsmObjectProperty { | |
param($Class) | |
$Class.PropertyCollection | % { | |
$_ | |
} | |
Get-SCSMClass | Where { $_.Base.Id -eq $Class.Id -and $_.Extension } | ForEach-Object { | |
foreach($property in $_.PropertyCollection) | |
{ | |
$property | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment