Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Jun 14, 2023
1 parent a48e43c commit bafa026
Show file tree
Hide file tree
Showing 10 changed files with 30,207 additions and 30,199 deletions.
555 changes: 277 additions & 278 deletions Corsinvest.ProxmoxVE.Api/Corsinvest.ProxmoxVE.Api.psd1

Large diffs are not rendered by default.

67 changes: 35 additions & 32 deletions Corsinvest.ProxmoxVE.Api/Corsinvest.ProxmoxVE.Api.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# SPDX-License-Identifier: GPL-3.0-only

class PveValidVmId : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() { return Get-PveVM | Select-Object -ExpandProperty vmid }
[string[]] GetValidValues() { return Get-PveVm | Select-Object -ExpandProperty vmid }
}

class PveValidVmName : System.Management.Automation.IValidateSetValuesGenerator {
[string[]] GetValidValues() {
return Get-PveVM | Where-Object {$_.status -ne 'unknown' } | Select-Object -ExpandProperty name
return Get-PveVm | Where-Object {$_.status -ne 'unknown' } | Select-Object -ExpandProperty name
}
}

Expand Down Expand Up @@ -316,9 +316,11 @@ Output file
$data = [System.Collections.ArrayList]::new()
foreach ($item in $commands) {
$progress++
Write-Progress -Activity "Elaborate command $($item.Name)" `
-CurrentOperation "Completed $($progress) of $totProgress." `
-PercentComplete $(($progress / $totProgress) * 100)
$perc = [Math]::Round(($progress / $totProgress) * 100)
#-CurrentOperation "Completed $($progress) of $totProgress." `
Write-Progress -Activity "Elaborate command" `
-Status "$perc% $($item.Name)" `
-PercentComplete $perc

#help
$help = Get-Help $item.Name -Full
Expand Down Expand Up @@ -371,14 +373,14 @@ Date time
[Int32]. Return Unix Time.
#>
[CmdletBinding()]
[OutputType([Int32])]
[OutputType([long])]
param (
[Parameter(Mandatory,Position = 0,ValueFromPipeline )]
[DateTime]$Date
)

process {
[Int32] ($Date.ToUniversalTime() - (Get-Date '1/1/1970').ToUniversalTime()).TotalSeconds
[long] (New-Object -TypeName System.DateTimeOffset -ArgumentList ($Date)).ToUnixTimeSeconds()
}
}

Expand All @@ -398,7 +400,7 @@ DateTime. Return DateTime from Unix Time.
[long] $Time
)

return (New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0).ToLocalTime().AddSeconds($Time)
return [System.DateTimeOffset]::FromUnixTimeSeconds($Time).DateTime
}
#endregion

Expand Down Expand Up @@ -434,7 +436,7 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName | Select-Object -First 1
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName | Select-Object -First 1
if ($vm.type -eq 'qemu') {
$node = $vm.node
$vmid = $vm.vmid
Expand Down Expand Up @@ -605,7 +607,7 @@ PSCustomObject. Return Vm Data.
}
}

function Get-PveVM {
function Get-PveVm {
<#
.DESCRIPTION
Get VMs/CTs from id or name.
Expand Down Expand Up @@ -722,7 +724,7 @@ function VmCheckIdOrName
return $false
}

function Unlock-PveVM {
function Unlock-PveVm {
<#
.DESCRIPTION
Unlock VM.
Expand All @@ -746,14 +748,14 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | Set-PveNodesQemuConfig -PveTicket $PveTicket -Delete 'lock' -Skiplock }
ElseIf ($vm.type -eq 'lxc') { return $vm | Set-PveNodesLxcConfig -PveTicket $PveTicket -Delete 'lock' }
}
}

#region VM status
function Start-PveVM {
function Start-PveVm {
<#
.DESCRIPTION
Start VM.
Expand All @@ -777,13 +779,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuStatusStart -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { return $vm | New-PveNodesLxcStatusStart -PveTicket $PveTicket }
}
}

function Stop-PveVM {
function Stop-PveVm {
<#
.DESCRIPTION
Stop VM.
Expand All @@ -807,13 +809,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuStatusStop -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { return $vm | New-PveNodesLxcStatusStop -PveTicket $PveTicket }
}
}

function Suspend-PveVM {
function Suspend-PveVm {
<#
.DESCRIPTION
Suspend VM.
Expand All @@ -837,13 +839,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuStatusSuspend -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { return $vm | New-PveNodesLxcStatusSuspend -PveTicket $PveTicket }
}
}

function Resume-PveVM {
function Resume-PveVm {
<#
.DESCRIPTION
Resume VM.
Expand All @@ -867,13 +869,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuStatusResume -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { return $vm | New-PveNodesLxcStatusResume -PveTicket $PveTicket }
}
}

function Reset-PveVM {
function Reset-PveVm {
<#
.DESCRIPTION
Reset VM.
Expand All @@ -897,15 +899,15 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuStatusReset -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { throw "Lxc not implement reset!" }
}
}
#endregion

#region Snapshot
function Get-PveVMSnapshots {
function Get-PveVmSnapshot {
<#
.DESCRIPTION
Get snapshots VM.
Expand All @@ -929,13 +931,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | Get-PveNodesQemuSnapshot -PveTicket $PveTicket }
ElseIf ($vm.type -eq 'lxc') { return $vm | Get-PveNodesLxcSnapshot -PveTicket $PveTicket }
}
}

function New-PveVMSnapshot {
function New-PveVmSnapshot {
<#
.DESCRIPTION
Create snapshot VM.
Expand Down Expand Up @@ -976,7 +978,7 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu')
{
if ($Vmstate) {
Expand All @@ -994,7 +996,7 @@ PveResponse. Return response.
}
}

function Remove-PveVMSnapshot {
function Remove-PveVmSnapshot {
<#
.DESCRIPTION
Delete a VM snapshot.
Expand Down Expand Up @@ -1024,13 +1026,13 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | Remove-PveNodesQemuSnapshot -PveTicket $PveTicket -Snapname $Snapname }
ElseIf ($vm.type -eq 'lxc') { return $vm | Remove-PveNodesLxcSnapshot -PveTicket $PveTicket -Snapname $Snapname }
}
}

function Undo-PveVMSnapshot {
function Undo-PveVmSnapshot {
<#
.DESCRIPTION
Rollback VM state to specified snapshot.
Expand Down Expand Up @@ -1060,7 +1062,7 @@ PveResponse. Return response.
)

process {
$vm = Get-PveVM -PveTicket $PveTicket -VmIdOrName $VmIdOrName
$vm = Get-PveVm -PveTicket $PveTicket -VmIdOrName $VmIdOrName
if ($vm.type -eq 'qemu') { return $vm | New-PveNodesQemuSnapshotRollback -PveTicket $PveTicket -Snapname $Snapname }
ElseIf ($vm.type -eq 'lxc') { return $vm | New-PveNodesLxcSnapshotRollback -PveTicket $PveTicket -Snapname $Snapname }
}
Expand All @@ -1081,7 +1083,7 @@ Set-Alias -Name Stop-PveQemu -Value New-PveNodesQeumStatusStop -PassThru
Set-Alias -Name Suspend-PveQemu -Value New-PveNodesQemuStatusSuspend -PassThru
Set-Alias -Name Resume-PveQemu -Value New-PveNodesQemuStatusResume -PassThru
Set-Alias -Name Reset-PveQemu -Value New-PveNodesQemuStatusReset -PassThru
#Set-Alias -Name Reboot-PveQemu -Value New-PveNodesQemuStatusReboot
Set-Alias -Name Restart-PveQemu -Value New-PveNodesQemuStatusReboot -PassThru
#Set-Alias -Name Shutdown-PveQemu -Value New-PveNodesQemuStatusShutdown
Set-Alias -Name Move-PveQemu -Value New-PveNodesQemuMigrate -PassThru
Set-Alias -Name New-PveQemu -Value New-PveNodesQemu -PassThru
Expand All @@ -1092,14 +1094,15 @@ Set-Alias -Name Start-PveLxc -Value New-PveNodesLxcStatusStart -PassThru
Set-Alias -Name Stop-PveLxc -Value New-PveNodesLxcStatusStop -PassThru
Set-Alias -Name Suspend-PveLxc -Value New-PveNodesLxcStatusSuspend -PassThru
Set-Alias -Name Resume-PveLxc -Value New-PveNodesLxcStatusResume -PassThru
#Set-Alias -Name Start-PveLxc -Value New-PveNodesLxcStatusReboot
Set-Alias -Name Restart-PveLxc -Value New-PveNodesLxcStatusReboot -PassThru
#Set-Alias -Name Start-PveLxc -Value New-PveNodesLxcStatusShutdown
Set-Alias -Name Move-PveLxc -Value New-PveNodesLxcMigrate -PassThru
Set-Alias -Name Copy-PveLxc -Value New-PveNodesLxcClone -PassThru

#NODE
Set-Alias -Name Update-PveNode -Value New-PveNodesAptUpdate -PassThru
Set-Alias -Name Backup-PveVzdump -Value New-PveNodesVzdump -PassThru
#Set-Alias -Name Stop-PveNode -Value New-PveNodesStatus -Command 'shutdown' -PassThru

#########
## API ##
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ this is a CmdLet for PowerShell to manage Proxmox VE.
* Wait-PveTaskIsFinish
* Get-PveTaskIsRunning
* Build-PveDocumentation
* Get-PveVM (from id or name)
* Unlock-PveVM (from id or name)
* Start-PveVM (from id or name)
* Stop-PveVM (from id or name)
* Suspend-PveVM (from id or name)
* Resume-PveVM (from id or name)
* Reset-PveVM (from id or name)
* Get-PveVm (from id or name)
* Unlock-PveVm (from id or name)
* Start-PveVm (from id or name)
* Stop-PveVm (from id or name)
* Suspend-PveVm (from id or name)
* Resume-PveVm (from id or name)
* Reset-PveVm (from id or name)
* And More
* Method direct access using Invoke-PveRestApi return PveResponse
* Connect-PveCluster accept multiple hosts for HA
Expand Down Expand Up @@ -230,7 +230,7 @@ vmstate name parent description
Other method

```ps
(Get-PveVM -VmIdOrName 100 | Get-PveNodesQemuSnapshot).ToTable()
(Get-PveVm -VmIdOrName 100 | Get-PveNodesQemuSnapshot).ToTable()
vmstate name parent description snaptime
------- ---- ------ ----------- --------
0 autowin10service200221183059 autowin10service200220183012 cv4pve-autosnap 1582306261
Expand Down
6 changes: 5 additions & 1 deletion Tutorial.dib
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#!meta

{"kernelInfo":{"defaultKernelName":"csharp","items":[{"aliases":[],"name":"csharp"}]}}

#!markdown

# Hello World
Expand Down Expand Up @@ -130,4 +134,4 @@ Get snapshot of VM/CT with number 100
#!pwsh

#Change number vm
(Get-PveVMSnapshots -VmIdOrName 100).ToTable()
(Get-PveVMSnapshot -VmIdOrName 100).ToTable()
15 changes: 5 additions & 10 deletions analize.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# This file is part of the cv4pve-api-powershell https://github.com/Corsinvest/cv4pve-api-powershell,
#
# This source file is available under two different licenses:
# - GNU General Public License version 3 (GPLv3)
# - Corsinvest Enterprise License (CEL)
# Full copyright and license information is available in
# LICENSE.md which is distributed with this source code.
#
# Copyright (C) 2020 Corsinvest Srl GPLv3 and CEL
# SPDX-FileCopyrightText: Copyright Corsinvest Srl
# SPDX-License-Identifier: GPL-3.0-only

Get-ChildItem -Path Corsinvest.ProxmoxVE.Api -Filter "*.psm1" -Recurse | Invoke-ScriptAnalyzer
Import-Module PSScriptAnalyzer

Get-ChildItem -Path Corsinvest.ProxmoxVE.Api -Filter "*.psm1" -Recurse | Invoke-ScriptAnalyzer -ExcludeRule PSUseSingularNouns
4 changes: 4 additions & 0 deletions build-doc.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: Copyright Corsinvest Srl
# SPDX-License-Identifier: GPL-3.0-only

Build-PveDocumentation -OutputFile .\doc\index.html -TemplateFile .\help-out-html.ps1
Loading

0 comments on commit bafa026

Please sign in to comment.