HewlettPackard / POSH-HPEOneView

PowerShell language bindings library for HPE OneView.
http://hewlettpackard.github.io/POSH-HPEOneView/
125 stars 52 forks source link

Get-OVNetworkSet from an Get-OVNetwork #580

Closed salcinad closed 2 years ago

salcinad commented 3 years ago

I have an rather simple task to remove few VLANs from Synergy OneView Composer2. But would like to do it without using OneView GUI.

We add VLAN to OneView with "VLAN_1234" so the VLAN Name is known, but what I don't know in which Network Set(s) is these VLAN member of.

From OneView GUI Network Info: Member of 3 network sets

I was trying something as: Get-OVNetwork -Name "VLAN_1234" | Get-OVNetworkSet But it throws an error: "Get-OVNetworkSet : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input."

What would be proper way of removing an VLAN without going to OneView GUI and searching for Network Sets where the VLANs is member off.

ChrisLynchHPE commented 3 years ago

That isn't a use case Get-OVNetworkSet supports (although that is an interesting one). Is the only operation you need to do is remove from network sets it is a member of? If so, you can do the following:

# Get the network
$Network = Get-OVNetwork -Name MyNetworkName

# Get associated network sets from the appliance Index
$Uri = '/rest/index/associations/resources?name=NETWORKSET_TO_NETWORK&childUri={0}' -f $Network.Uri
$IndexResults = Send-OVRequest -Uri $Uri

$NetworkSets = @()

# Get full network set resources from the API
ForEach ($parent in $IndexResults.members)
{

    Write-Host ("Network was found to be a member of '{0}' network set." -f $parent.parentResource.name)
    $NetworkSets += Send-OVRequest -Uri $parent.parentResource.uri

}

# Remove the network from network set
ForEach ($netset in $NetworkSets)
{

    Set-OVNetworkSet -InputObject $netset -RemoveNetwork $Network

}
salcinad commented 3 years ago

Thank you for having time to check this, it is rather not the Issue but an Question still thank you for your time.

Exactly to remove the Network from Network Sets it is a member of. And then delete Network itself. We also use as suggested by HPE Consultant NetworkSets for Uplink set to, separated NetworkSet with "_USL_MyNetworkSet". So we need just to Add/Remove Network from the correct NetworkSet and it will be used by Uplink and also for Profile/Templates.

I tried your Code but seems the $NetworkSets variable is empty. Here i am Talking about Network Type: Ethernet an it is Tagged see below.

I actually did my task with PowerShell, but it is not the code I would prefer to actually do this task as it lucks foreach, which I cannot get from "Member Of"

$NetworksToRemove = Get-OVNetwork -Name "MyNetworkName"
$NetworksToRemove | fl # with this I see the "Member Of" Value which then I use i below command
    Type: Ethernet

Name                    : MyNetworkName
Status                  : OK
Type                    : Tagged
VlanID                  : 12345
IPv4Subnet              : None
Purpose                 : General
Smartlink               : True
PrivateNetwork          : False
PreferredBandwidth      : 2500
MaxBandwidth            : 20000
Uplink Set              : {UplinkSet1, UplinkSet2}
Used By Server Profiles : None
Member Of               : {MyNetworkSet1 _USL_MyNetworkSet2, _USL_MyNetworkSet3}

# Network Set(s)
Get-OVNetworkSet -Name MyNetworkSet1 | Set-OVNetworkSet -RemoveNetwork $NetworksToRemove
Get-OVNetworkSet -Name _USL_MyNetworkSet2 | Set-OVNetworkSet -RemoveNetwork $NetworksToRemove
Get-OVNetworkSet -Name _USL_MyNetworkSet3 | Set-OVNetworkSet -RemoveNetwork $NetworksToRemove

Get-OVNetwork -Name "MyNetworkName" | Remove-OVNetwork

One question regarding "$NetworksToRemove" example of the "Set-OVNetworkSet" Command:

$NetworksToRemove = Get-OVNetwork -Name "Dev 45*"
Get-OVNetworkSet -Name NetSet-2 | Set-OVNetworkSet -RemoveNetwork $NetworksToRemove

    Remove the specified networks from the existing network set.

How would I add there more Network and not just an Filter "Dev 45*" , Let say I have three VLANs to remove. As it seems the "Get-OVNetwork" does not accept multiple inputs at least not with HPEOneView.610 Module:

$NetworksToRemove = Get-OVNetwork -Name "VLAN_1234","VLAN_3456"
Get-OVNetwork : Cannot process argument transformation on parameter 'Name'. Cannot convert value to type System.String.

I tried to have only one Quote and Separate VLANs with "," but seems it will also not be accepted. I hoped because this variable example have an "s" in Networks that it accepts multiple VLANs as Input.

ChrisLynchHPE commented 3 years ago

Member Of is a Format property, and not one of the original object. The reason why the $NetworkSets was empty was due to a typo I had on them$IndexResults = Send-OVRequest -Uri $Uri line (which I just fixed). That is the way you would want to ideally process the removal of a network from a network set.

As for bulk removal of networks from a network set, the Set-OVNetworkSet already supports it. Get-OVNetwork only supports a single string value for the -Name parameter. If you want multiple networks, use wildcard where you can. But, if you have different names that wildcard character cannot cover, then you need to use something like 'name1', 'name2' | % { Get-OVNetwork -Name $_ }.

salcinad commented 2 years ago

Seems like there is Problem with getting full network set resources from the API. If I try again your code above i'll get the following error.

Using HPEOneView.630 ( 6.30.2928.3678) with the HPE OneView Composer2 6.50.00-0452161 and PSVersion 5.1.17763.2803

Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "_USL_MyNetworkSet" to type "System.ConsoleColor". Error: "Unable to match the identifier name _USL_MyNetworkSet to a valid enumerator name. Specify 
one of the following enumerator names and try again:
Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"
At line:15 char:76
+ ...  to be a member of '{0}' network set." -f $parent.parentResource.name
+                                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Write-Host], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WriteHostCommand

The same error we get also from NetworkSets which does not contain "_USL_Name" at the begging.

If I just check the $IndexResults.members I see the all listed:

$IndexResults.members

parentResource                                                                                                                                                                                                          
--------------                                                                                                                                                                                                          
@{type=IndexResourceV300; name=_USL_MyNetworkSet; description=; attributes=; multiAttributes=; ownerId=cd; state=; status=OK; created=2021-01-19T08:36:36.920Z;.....

Edit # 1 By removing the "-f" from the "Write-Host" I get the needed output.

ChrisLynchHPE commented 2 years ago

Closing due to no further activity. This issue can always be re-opened if needed.