Add regex and wildcard filtering to Get-* cmdlets

This commit is contained in:
Alphaeus Mote
2025-05-09 14:58:31 -04:00
parent 060ab64d70
commit 9dba7a3bd6
9 changed files with 589 additions and 21 deletions
+108
View File
@@ -0,0 +1,108 @@
# Get-ProxmoxIPPool
Gets IP address pools.
## Syntax
```powershell
Get-ProxmoxIPPool
[-Name <String>]
[-UseRegex]
[<CommonParameters>]
```
## Description
The `Get-ProxmoxIPPool` cmdlet retrieves IP address pools used with Proxmox VE virtual machines. You can retrieve all pools or a specific pool by name.
## Parameters
### -Name
The name of the IP pool to retrieve. Supports wildcards and regex when used with -UseRegex.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: True
```
### -UseRegex
Use regular expressions for filtering.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## Inputs
### None
## Outputs
### PSProxmox.Models.ProxmoxIPPool
## Notes
- This cmdlet does not require a connection to a Proxmox VE server as IP pools are stored locally.
- If the `-Name` parameter is specified and the pool is not found, an error will be thrown.
- The `-Name` parameter supports wildcards (e.g., "Production*") by default.
- Use the `-UseRegex` parameter with `-Name` to filter using regular expressions (e.g., "^Prod[a-z]+$").
## Examples
### Example 1: Get all IP pools
```powershell
$pools = Get-ProxmoxIPPool
```
This example gets all IP pools.
### Example 2: Get a specific IP pool by name
```powershell
$pool = Get-ProxmoxIPPool -Name "Production"
```
This example gets the IP pool named "Production".
### Example 3: Get IP pools by name using wildcards
```powershell
$pools = Get-ProxmoxIPPool -Name "Prod*"
```
This example gets all IP pools with names starting with "Prod".
### Example 4: Get IP pools by name using regex
```powershell
$pools = Get-ProxmoxIPPool -Name "^Prod[a-z]+$" -UseRegex
```
This example gets all IP pools with names matching the pattern "Prod" followed by one or more lowercase letters.
## Related Links
- [New-ProxmoxIPPool](New-ProxmoxIPPool.md)
- [Clear-ProxmoxIPPool](Clear-ProxmoxIPPool.md)
+159
View File
@@ -0,0 +1,159 @@
# Get-ProxmoxNode
Gets nodes from a Proxmox VE cluster.
## Syntax
```powershell
Get-ProxmoxNode
[-Connection <ProxmoxConnection>]
[-Name <String>]
[-UseRegex]
[-RawJson]
[<CommonParameters>]
```
## Description
The `Get-ProxmoxNode` cmdlet retrieves nodes from a Proxmox VE cluster. You can retrieve all nodes or a specific node by name.
## Parameters
### -Connection
The connection to the Proxmox VE server.
```yaml
Type: ProxmoxConnection
Parameter Sets: (All)
Aliases:
Required: False
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Name
The name of the node to retrieve. Supports wildcards and regex when used with -UseRegex.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: True
```
### -UseRegex
Use regular expressions for filtering.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### -RawJson
Whether to return the raw JSON response instead of parsed objects.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## Inputs
### None
## Outputs
### PSProxmox.Models.ProxmoxNode
### System.String
## Notes
- This cmdlet requires a connection to a Proxmox VE server. Use `Connect-ProxmoxServer` to establish a connection.
- If no connection is specified, the cmdlet will use the default connection.
- If the `-RawJson` parameter is specified, the raw JSON response will be returned instead of parsed objects.
- If the `-Name` parameter is specified and the node is not found, an error will be thrown.
- The `-Name` parameter supports wildcards (e.g., "pve*") by default.
- Use the `-UseRegex` parameter with `-Name` to filter using regular expressions (e.g., "^pve[0-9]+$").
## Examples
### Example 1: Get all nodes
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$nodes = Get-ProxmoxNode
```
This example gets all nodes from the Proxmox VE cluster.
### Example 2: Get a specific node by name
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$node = Get-ProxmoxNode -Name "pve1"
```
This example gets the node named "pve1".
### Example 3: Get nodes by name using wildcards
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$nodes = Get-ProxmoxNode -Name "pve*"
```
This example gets all nodes with names starting with "pve".
### Example 4: Get nodes by name using regex
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$nodes = Get-ProxmoxNode -Name "^pve[0-9]+$" -UseRegex
```
This example gets all nodes with names matching the pattern "pve" followed by one or more digits.
### Example 5: Get the raw JSON response
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$json = Get-ProxmoxNode -RawJson
```
This example gets the raw JSON response for all nodes.
## Related Links
- [Connect-ProxmoxServer](Connect-ProxmoxServer.md)
- [Get-ProxmoxVM](Get-ProxmoxVM.md)
+66 -11
View File
@@ -6,9 +6,11 @@ Gets virtual machines from Proxmox VE.
```powershell
Get-ProxmoxVM
-Connection <ProxmoxConnection>
[-Connection <ProxmoxConnection>]
[-Node <String>]
[-VMID <Int32>]
[-Name <String>]
[-UseRegex]
[-RawJson]
[<CommonParameters>]
```
@@ -28,7 +30,7 @@ Type: ProxmoxConnection
Parameter Sets: (All)
Aliases:
Required: True
Required: False
Position: 0
Default value: None
Accept pipeline input: False
@@ -67,6 +69,38 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -Name
The name of the virtual machine to retrieve. Supports wildcards and regex when used with -UseRegex.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: True
```
### -UseRegex
Use regular expressions for filtering.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### -RawJson
Whether to return the raw JSON response instead of parsed objects.
@@ -100,16 +134,19 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
## Notes
- This cmdlet requires a connection to a Proxmox VE server. Use `Connect-ProxmoxServer` to establish a connection.
- If no connection is specified, the cmdlet will use the default connection.
- If the `-RawJson` parameter is specified, the raw JSON response will be returned instead of parsed objects.
- If the `-VMID` parameter is specified and the VM is not found, an error will be thrown.
- The `-Name` parameter supports wildcards (e.g., "web*") by default.
- Use the `-UseRegex` parameter with `-Name` to filter using regular expressions (e.g., "^web[0-9]+$").
## Examples
### Example 1: Get all VMs
```powershell
$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM -Connection $connection
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM
```
This example gets all VMs from all nodes.
@@ -117,8 +154,8 @@ This example gets all VMs from all nodes.
### Example 2: Get VMs on a specific node
```powershell
$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM -Connection $connection -Node "pve1"
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM -Node "pve1"
```
This example gets all VMs on the node "pve1".
@@ -126,17 +163,35 @@ This example gets all VMs on the node "pve1".
### Example 3: Get a specific VM by ID
```powershell
$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vm = Get-ProxmoxVM -Connection $connection -VMID 100
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vm = Get-ProxmoxVM -VMID 100
```
This example gets the VM with ID 100.
### Example 4: Get the raw JSON response
### Example 4: Get VMs by name using wildcards
```powershell
$connection = Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$json = Get-ProxmoxVM -Connection $connection -RawJson
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM -Name "web*"
```
This example gets all VMs with names starting with "web".
### Example 5: Get VMs by name using regex
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$vms = Get-ProxmoxVM -Name "^web[0-9]+$" -UseRegex
```
This example gets all VMs with names matching the pattern "web" followed by one or more digits.
### Example 6: Get the raw JSON response
```powershell
Connect-ProxmoxServer -Server "proxmox.example.com" -Credential (Get-Credential)
$json = Get-ProxmoxVM -RawJson
```
This example gets the raw JSON response for all VMs.
@@ -0,0 +1,109 @@
# Get-ProxmoxVMTemplate
Gets virtual machine templates.
## Syntax
```powershell
Get-ProxmoxVMTemplate
[-Name <String>]
[-UseRegex]
[<CommonParameters>]
```
## Description
The `Get-ProxmoxVMTemplate` cmdlet retrieves virtual machine templates. You can retrieve all templates or a specific template by name.
## Parameters
### -Name
The name of the template to retrieve. Supports wildcards and regex when used with -UseRegex.
```yaml
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 0
Default value: None
Accept pipeline input: False
Accept wildcard characters: True
```
### -UseRegex
Use regular expressions for filtering.
```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
## Inputs
### None
## Outputs
### PSProxmox.Models.ProxmoxVMTemplate
## Notes
- This cmdlet does not require a connection to a Proxmox VE server as templates are stored locally.
- If the `-Name` parameter is specified and the template is not found, an error will be thrown.
- The `-Name` parameter supports wildcards (e.g., "Ubuntu*") by default.
- Use the `-UseRegex` parameter with `-Name` to filter using regular expressions (e.g., "^Ubuntu-[0-9]+\\.[0-9]+$").
## Examples
### Example 1: Get all templates
```powershell
$templates = Get-ProxmoxVMTemplate
```
This example gets all templates.
### Example 2: Get a specific template by name
```powershell
$template = Get-ProxmoxVMTemplate -Name "Ubuntu-Template"
```
This example gets the template named "Ubuntu-Template".
### Example 3: Get templates by name using wildcards
```powershell
$templates = Get-ProxmoxVMTemplate -Name "Ubuntu*"
```
This example gets all templates with names starting with "Ubuntu".
### Example 4: Get templates by name using regex
```powershell
$templates = Get-ProxmoxVMTemplate -Name "^Ubuntu-[0-9]+\\.[0-9]+$" -UseRegex
```
This example gets all templates with names matching the pattern "Ubuntu-" followed by a version number (e.g., "Ubuntu-20.04").
## Related Links
- [New-ProxmoxVMTemplate](New-ProxmoxVMTemplate.md)
- [Remove-ProxmoxVMTemplate](Remove-ProxmoxVMTemplate.md)
- [New-ProxmoxVMFromTemplate](New-ProxmoxVMFromTemplate.md)
+95
View File
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text.RegularExpressions;
namespace PSProxmox.Cmdlets
{
/// <summary>
/// Base class for cmdlets that support filtering with wildcards and regex.
/// </summary>
public abstract class FilteringCmdlet : ProxmoxCmdlet
{
/// <summary>
/// <para type="description">Use regular expressions for filtering.</para>
/// </summary>
[Parameter(Mandatory = false)]
public SwitchParameter UseRegex { get; set; }
/// <summary>
/// Filters a collection of objects by a property value using wildcards or regex.
/// </summary>
/// <typeparam name="T">The type of objects to filter.</typeparam>
/// <param name="items">The collection of objects to filter.</param>
/// <param name="propertyName">The name of the property to filter by.</param>
/// <param name="pattern">The pattern to match. Can be a wildcard or regex pattern.</param>
/// <returns>A filtered collection of objects.</returns>
protected IEnumerable<T> FilterByProperty<T>(IEnumerable<T> items, string propertyName, string pattern)
{
if (string.IsNullOrEmpty(pattern))
{
return items;
}
var filteredItems = new List<T>();
var type = typeof(T);
var property = type.GetProperty(propertyName);
if (property == null)
{
throw new ArgumentException($"Property '{propertyName}' not found on type '{type.Name}'.");
}
foreach (var item in items)
{
var value = property.GetValue(item)?.ToString();
if (value == null)
{
continue;
}
bool isMatch;
if (UseRegex.IsPresent)
{
// Use regex matching
try
{
isMatch = Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase);
}
catch (ArgumentException ex)
{
throw new ArgumentException($"Invalid regular expression pattern: {ex.Message}", ex);
}
}
else
{
// Use wildcard matching
isMatch = WildcardMatch(value, pattern);
}
if (isMatch)
{
filteredItems.Add(item);
}
}
return filteredItems;
}
/// <summary>
/// Matches a string against a wildcard pattern.
/// </summary>
/// <param name="input">The input string to match.</param>
/// <param name="pattern">The wildcard pattern to match against.</param>
/// <returns>True if the input matches the pattern, false otherwise.</returns>
private bool WildcardMatch(string input, string pattern)
{
// Convert wildcard pattern to regex pattern
string regexPattern = "^" + Regex.Escape(pattern)
.Replace("\\*", ".*")
.Replace("\\?", ".") + "$";
return Regex.IsMatch(input, regexPattern, RegexOptions.IgnoreCase);
}
}
}
+11 -3
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using PSProxmox.IPAM;
using PSProxmox.Models;
@@ -20,12 +21,12 @@ namespace PSProxmox.Cmdlets
/// </summary>
[Cmdlet(VerbsCommon.Get, "ProxmoxIPPool")]
[OutputType(typeof(ProxmoxIPPool))]
public class GetProxmoxIPPoolCmdlet : PSCmdlet
public class GetProxmoxIPPoolCmdlet : FilteringCmdlet
{
private static readonly IPAMManager _ipamManager = new IPAMManager();
/// <summary>
/// <para type="description">The name of the IP pool to retrieve.</para>
/// <para type="description">The name of the IP pool to retrieve. Supports wildcards and regex when used with -UseRegex.</para>
/// </summary>
[Parameter(Mandatory = false, Position = 0)]
public string Name { get; set; }
@@ -37,7 +38,7 @@ namespace PSProxmox.Cmdlets
{
try
{
if (string.IsNullOrEmpty(Name))
if (string.IsNullOrEmpty(Name) || UseRegex.IsPresent || Name.Contains("*") || Name.Contains("?"))
{
// Get all pools
var pools = new List<ProxmoxIPPool>();
@@ -45,6 +46,13 @@ namespace PSProxmox.Cmdlets
{
pools.Add(ProxmoxIPPool.FromIPPool(pool));
}
// Apply name filtering if specified
if (!string.IsNullOrEmpty(Name))
{
pools = FilterByProperty(pools, "Name", Name).ToList();
}
WriteObject(pools, true);
}
else
+10 -3
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -24,7 +25,7 @@ namespace PSProxmox.Cmdlets
/// </summary>
[Cmdlet(VerbsCommon.Get, "ProxmoxNode")]
[OutputType(typeof(ProxmoxNode), typeof(string))]
public class GetProxmoxNodeCmdlet : PSCmdlet
public class GetProxmoxNodeCmdlet : FilteringCmdlet
{
/// <summary>
/// <para type="description">The connection to the Proxmox VE server.</para>
@@ -33,7 +34,7 @@ namespace PSProxmox.Cmdlets
public ProxmoxConnection Connection { get; set; }
/// <summary>
/// <para type="description">The name of the node to retrieve.</para>
/// <para type="description">The name of the node to retrieve. Supports wildcards and regex when used with -UseRegex.</para>
/// </summary>
[Parameter(Mandatory = false)]
public string Name { get; set; }
@@ -54,7 +55,7 @@ namespace PSProxmox.Cmdlets
var client = new ProxmoxApiClient(Connection, this);
string response;
if (string.IsNullOrEmpty(Name))
if (string.IsNullOrEmpty(Name) || UseRegex.IsPresent || Name.Contains("*") || Name.Contains("?"))
{
// Get all nodes
response = client.Get("nodes");
@@ -67,6 +68,12 @@ namespace PSProxmox.Cmdlets
nodes.Add(node);
}
// Apply name filtering if specified
if (!string.IsNullOrEmpty(Name))
{
nodes = FilterByProperty(nodes, "Name", Name).ToList();
}
if (RawJson.IsPresent)
{
WriteObject(response);
+20 -1
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -28,7 +29,7 @@ namespace PSProxmox.Cmdlets
/// </summary>
[Cmdlet(VerbsCommon.Get, "ProxmoxVM")]
[OutputType(typeof(ProxmoxVM), typeof(string))]
public class GetProxmoxVMCmdlet : ProxmoxCmdlet
public class GetProxmoxVMCmdlet : FilteringCmdlet
{
/// <summary>
@@ -43,6 +44,12 @@ namespace PSProxmox.Cmdlets
[Parameter(Mandatory = false)]
public string Node { get; set; }
/// <summary>
/// <para type="description">The name of the virtual machine to retrieve. Supports wildcards and regex when used with -UseRegex.</para>
/// </summary>
[Parameter(Mandatory = false, Position = 2)]
public string Name { get; set; }
/// <summary>
/// <para type="description">Whether to return the raw JSON response.</para>
/// </summary>
@@ -154,6 +161,12 @@ namespace PSProxmox.Cmdlets
}
}
// Apply name filtering if specified
if (!string.IsNullOrEmpty(Name))
{
allVMs = FilterByProperty(allVMs, "Name", Name).ToList();
}
if (RawJson.IsPresent)
{
WriteObject(JsonConvert.SerializeObject(allVMs));
@@ -177,6 +190,12 @@ namespace PSProxmox.Cmdlets
nodeVMs.Add(vm);
}
// Apply name filtering if specified
if (!string.IsNullOrEmpty(Name))
{
nodeVMs = FilterByProperty(nodeVMs, "Name", Name).ToList();
}
if (RawJson.IsPresent)
{
WriteObject(response);
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using PSProxmox.Models;
using PSProxmox.Templates;
@@ -20,10 +21,10 @@ namespace PSProxmox.Cmdlets
/// </summary>
[Cmdlet(VerbsCommon.Get, "ProxmoxVMTemplate")]
[OutputType(typeof(ProxmoxVMTemplate))]
public class GetProxmoxVMTemplateCmdlet : PSCmdlet
public class GetProxmoxVMTemplateCmdlet : FilteringCmdlet
{
/// <summary>
/// <para type="description">The name of the template to retrieve.</para>
/// <para type="description">The name of the template to retrieve. Supports wildcards and regex when used with -UseRegex.</para>
/// </summary>
[Parameter(Mandatory = false, Position = 0)]
public string Name { get; set; }
@@ -35,7 +36,7 @@ namespace PSProxmox.Cmdlets
{
try
{
if (string.IsNullOrEmpty(Name))
if (string.IsNullOrEmpty(Name) || UseRegex.IsPresent || Name.Contains("*") || Name.Contains("?"))
{
// Get all templates
var templates = new List<ProxmoxVMTemplate>();
@@ -43,6 +44,13 @@ namespace PSProxmox.Cmdlets
{
templates.Add(template);
}
// Apply name filtering if specified
if (!string.IsNullOrEmpty(Name))
{
templates = FilterByProperty(templates, "Name", Name).ToList();
}
WriteObject(templates, true);
}
else