forked from OneIdentity/safeguard-ps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-bulk-assets.ps1
112 lines (101 loc) · 3.54 KB
/
load-bulk-assets.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)]
[string]$AssetPartitionName,
[Parameter(Mandatory=$true, Position=0)]
[int]$Quantity,
[Parameter(Mandatory=$false)]
[ValidateSet("OtherManaged","Linux")]
[string]$Platform = "OtherManaged",
[Parameter(Mandatory=$false)]
[string]$AssetPrefix,
[Parameter(Mandatory=$false)]
[string]$AccountName = "root",
[Parameter(Mandatory=$false)]
[switch]$NoAccounts
)
function Add-OneThousand
{
if ($script:Remaining -ge 1000)
{
$local:Chunk = 1000
}
else
{
$local:Chunk = $script:Remaining
}
Write-Host -ForegroundColor Green ("[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)) -NoNewline
Write-Host -ForegroundColor Yellow "-- Adding $local:Chunk assets -- $script:Prefix-$($script:FormatString -f $script:Index) through $script:Prefix-$($script:FormatString -f ($script:Index + $local:Chunk - 1))" | Out-Host
$local:Body = @()
for ($local:i = 1; $local:i -le $local:Chunk; $local:i++)
{
$local:Body += @{
Name = "$script:Prefix-$($script:FormatString -f $script:Index)";
Description = "Generated Asset";
NetworkAddress = "generated.fake.dns";
PlatformId = $script:PlatformId;
AssetPartitionId = $script:AssetPartitionId;
ConnectionProperties = $script:ConnectionProps
}
$script:Index++
}
Write-Host -ForegroundColor Green ("[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)) -NoNewline
Write-Host -ForegroundColor Yellow "-- Request prepared, sending" | Out-Host
$local:NewAssetIds = (Invoke-SafeguardMethod core POST Assets/BatchCreate -Body $local:Body -Timeout 3600).Response.Id
if (-not $NoAccounts)
{
Write-Host -ForegroundColor Green ("[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)) -NoNewline
Write-Host -ForegroundColor Yellow "-- Adding $local:Chunk accounts to those assets" | Out-Host
$local:Body = @()
foreach ($local:NewAssetId in $local:NewAssetIds)
{
$local:Body += @{
AssetId = $local:NewAssetId;
Name = $AccountName
}
}
Write-Host -ForegroundColor Green ("[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)) -NoNewline
Write-Host -ForegroundColor Yellow "-- Request prepared, sending" | Out-Host
Invoke-SafeguardMethod core POST AssetAccounts/BatchCreate -Body $local:Body -Timeout 3600 | Out-Null
}
$script:Remaining -= $local:Chunk
}
$ErrorActionPreference = "Stop"
if (-not $SafeguardSession)
{
throw "This cmdlet requires that you log in with the Connect-Safeguard cmdlet"
}
if ($AssetPrefix)
{
$script:Prefix = $AssetPrefix
}
else
{
$script:Prefix = ((65..90) | Get-Random -Count 5 | ForEach-Object { [char]$_ }) -join ""
}
$script:Remaining = $Quantity
$script:FormatString = "{0:d$(([string]$Quantity).Length)}"
$script:Index = 1
if ($Platform -ieq "Linux")
{
$script:ConnectionProps = @{ ServiceAccountCredentialType = "None" }
$script:PlatformId = (Get-SafeguardPlatform 'Other Linux').Id
}
else
{
$script:ConnectionProps = @{ ServiceAccountCredentialType = "Custom" }
$script:PlatformId = (Get-SafeguardPlatform 'Other Managed').Id
}
if ($AssetPartitionName)
{
$script:AssetPartitionId = (Get-SafeguardAssetPartition $AssetPartitionName).Id
}
else
{
$script:AssetPartitionId = -1
}
Write-Host -ForegroundColor Magenta "Generating $Quantity Assets of type 'Other Managed' with random prefix '$script:Prefix-'"
while ($script:Remaining -gt 0)
{
Add-OneThousand
}