-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.ps1
More file actions
59 lines (56 loc) · 2.42 KB
/
init.ps1
File metadata and controls
59 lines (56 loc) · 2.42 KB
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
#! /usr/bin/pwsh
if(!(Get-Module Microsoft.PowerShell.SecretManagement -ListAvailable)) {
Install-Module Microsoft.PowerShell.SecretManagement, Microsoft.PowerShell.SecretStore -Force -Scope CurrentUser
Register-SecretVault -Name SecretStore -ModuleName Microsoft.PowerShell.SecretStore -DefaultVault -AllowClobber
}
if(!(Get-Module proxmox-rest-module -ListAvailable)) {
Install-Module proxmox-rest-module
}
if(!(Get-Module netbox-rest-module -ListAvailable)) {
Install-Module netbox-rest-module
}
Import-Module Microsoft.PowerShell.SecretManagement, Microsoft.PowerShell.SecretStore, netbox-rest-module, proxmox-rest-module
# Read or create a proxmox config object
try {
$pxconfig=Import-Clixml $PSScriptRoot\pxconfig.xml
}
catch {
$pxconfig=@{
serverAddress = Read-Host -Prompt "IP address or hostname of Proxmox server ONLY - no port"
tokenID = Read-Host -Prompt "What is the ID (not the actual token) for the token you want to use?"
}
$pxconfig | Export-Clixml $PSScriptRoot\pxconfig.xml
}
# Get or create the API credential
try {
$Secret=Get-Secret -Name $pxConfig.serverAddress -AsPlainText -ErrorAction Stop
}
catch {
$Secret=Get-Credential -Message "Proxmox User@Realm & API KEY" -Title 'Proxmox Credentials'
Set-Secret -Name $pxconfig.serverAddress -Secret $Secret
}
$PXConnection = New-PXConnection -DeviceAddress $pxconfig.serverAddress -User $Secret.UserName -ApiKey $Secret.GetNetworkCredential().Password -TokenID $pxconfig.tokenID -Passthru -SkipCertificateCheck -Verbose
Write-Output "Connection initiated:"
Remove-Variable -Name Secret -ErrorAction SilentlyContinue | Out-Null
$PXConnection|Select-Object -ExcludeProperty ApiKey
try {
$nbconfig=Import-Clixml $PSScriptRoot\nbconfig.xml
}
catch {
$nbconfig=@{
serverAddress = Read-Host -Prompt "IP address or hostname of Netbox server"
}
$nbconfig | Export-Clixml $PSScriptRoot\nbconfig.xml
}
# Get or create the API credential
try {
$NBSecret=Get-Secret -Name $NBConfig.serverAddress -AsPlainText -ErrorAction Stop
}
catch {
$NBSecret=Read-Host -Prompt "API Key"
Set-Secret -Name $NBConfig.serverAddress -Secret $NBSecret
}
$NBConnection = New-NBConnection -DeviceAddress $nbconfig.serverAddress -ApiKey $NBSecret -Passthru -SkipCertificateCheck -Verbose
Write-Output "Connection initiated:"
Remove-Variable -Name Secret -ErrorAction SilentlyContinue | Out-Null
$NBConnection|Select-Object -ExcludeProperty ApiKey