VMware Cloud Community
MikeEracleous
Contributor
Contributor

Unable to run Get-SecurityPolicy / Set-SecurityPolicy for a specific port group on a specific host

I am trying to write a script which gets/sets the security policy for a specific port group on a specific host via a powercli connection to a vCenter. This does not seem to be possible as both commands seem unable to accept pipeline input so if I do a "Get-SecurityPolicy" for a specific Port Group it returns the settings for each and every standard port group with the same name present on ANY host in vCenter. I want to get/set the security policy for a specific port group on a specific switch on a specific host.

Is this possible? It is part of a build / config script so I do not want to have to disconnect from vCenter and connect directly to the host just to configure this one option and then reconnect back to the vCenter again.

Thanks

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership

Since you didn't share any code, not sure how you are doing this, but the following works for me.

Get-VirtualPortGroup -Name 'MyPG' -VMHost MyEsx | 
Get-SecurityPolicy | 
Set-SecurityPolicy -ForgedTransmits $true


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MikeEracleous
Contributor
Contributor

I have been trying to do this via "$vmhost | Get-VirtualPortGroup" which does not seem to work.

So I normally fire up the Powershell ISE and lookup the command option from the side toolbar, the "VMHost" option was not listed there.

I will give this method a try, thanks.

 

Reply
0 Kudos
LucD
Leadership
Leadership

It depends what you have in $vmhost.
Is that an object returned by Get-VMHost or a String?
The latter will not work in this way.

Also,

Get-Module -Name VMware* -ListAvailable

check which PowerCLI version you are using.
You might benefit from an upgrade if you are using an older version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MikeEracleous
Contributor
Contributor

$vmhost was an object obtained from 

$vmhost = Get-VMHost <FQDN of host>

This did not work.

Reply
0 Kudos
LucD
Leadership
Leadership

Which PowerCLI version are you using?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MikeEracleous
Contributor
Contributor

Version 12 I believe

Reply
0 Kudos
LucD
Leadership
Leadership

What error did you get when you said "This did not work."
Can you show the code and the eventual error message?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MikeEracleous
Contributor
Contributor

$vmhost | Get-SecurityPolicy -VirtualPortGroup EPG-CORE-MGMT
Get-SecurityPolicy : 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.
At line:1 char:11
+ $vmhost | Get-SecurityPolicy -VirtualPortGroup EPG-CORE-MGMT
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (<Hostname>:PSObject) [Get-SecurityPolicy], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetSecurityPolicy

Reply
0 Kudos
LucD
Leadership
Leadership

Earlier you said you did

$vmhost | Get-VirtualPortGroup

now you say you do

$vmhost | Get-SecurityPolicy -VirtualPortGroup EPG-CORE-MGMT

which of course doesn't work since the Get-SecurityPolicy cmdlet does only take a VirtualPortgroup object over the pipeline, not a VMHost object.
Why don't you just use the code I gave at the beginning of this thread?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MikeEracleous
Contributor
Contributor

I am going to try it but I do believe you when you say it works, the rest after that is just me answering your questions. I will report back if there are any problems.

Reply
0 Kudos