Forum Discussion
PowerShell: Create Virtual Server
Add-PSSnapIn iControlSnapIn
$creds = get-credential
$username = ($creds.username).replace("\","")
$password = $creds.GetNetworkCredential().password
Initialize-F5.iControl -Hostname 1.1.1.1 -Username $username -Password $password
$IC = Get-F5.iControl
CODE TO CREATE VIP
$VirtualDefinition = $null
$WildMask = $null
$Resources = $null
$Profile = $null
$Profile_Array = $null
$VirtualDefinition = New-Object icontrol.CommonVirtualServerDefinition
$VirtualDefinition.address = "10.10.10.50"
$VirtualDefinition.name = "TestVirtual"
$VirtualDefinition.port = 80
$VirtualDefinition.protocol = "PROTOCOL_TCP"
$WildMask = "255.255.255.255"
$Resources = New-Object iControl.LocalLBVirtualServerVirtualServerResource
$Resources.default_pool_name = ""
$Resources.type = "RESOURCE_TYPE_POOL"
$Profile = New-Object iControl.LocalLBVirtualServerVirtualServerProfile
$Profile.profile_name = "Blank"
$Profile.profile_context = "PROFILE_CONTEXT_TYPE_ALL"
$Profile_Array = new-object "iControl.LocalLBVirtualServerVirtualServerProfile[][]" 1,1
$Profile_Array[0][0] = $Profile
$ic.LocalLBVirtualServer.create($VirtualDefinition, $WildMask, $Resources, $Profile)
... here is the error:
Exception calling "create" with "4" argument(s): "Exception caught in LocalLB::urn:iControl:LocalLB/VirtualServer::create()
Exception: Common::OperationFailed
primary_error_code : 16908342 (0x01020036)
secondary_error_code : 0
error_string : 01020036:3: The requested profile () was not found."
At U:\Powershell\(TESTING)F5-Scripting.ps1:33 char:32
+ $ic.LocalLBVirtualServer.create <<<< ($VirtualDefinition, $WildMask, $Resources, $Profile)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
... I'd like to be able to create one with no HTTP Profile at all, if possible. Thanks in advance for any assistance. I can't find a lot of good documentation on how to use PowerShell to manipulate iControl and even less on creating Virtual Servers.
12 Replies
- Hi,
In looking at how you are calling the method, you are passing scalar values in while the parameters expect 1-d and 2-d arrays.
There's a whole bunch of PowerShell articles and examples I've written in the iControl wiki in the PowerShell topic:
https://devcentral.f5.com/wiki/iControl.PowerShell.ashx
I looked through there and there isn't an example that I've written on that page that says it creates a virtual server, but by going to the VirtualServer's create() wiki topic
https://devcentral.f5.com/wiki/iControl.LocalLB__VirtualServer__create.ashx
You can see at the bottom under the "Sample Code" section, there's one in there that uses that method.
https://devcentral.f5.com/wiki/iControl.PsProvisionVEForLocalDev.ashx
In that code, look in the "Create-Virtual" function and you'll see how I coded it.
Now, that method does create a http virtual, but you should be able to substitute the http profile for a tcp profile or something else that ties to your existing virtual server.
Hope this helps.
-Joe - tharrison_91509
Nimbostratus
Joe - thanks for those links... very helpful indeed, but it sounds like you're saying there's no way to create a "blank" Virtual Server without any profiles selected at all as you can in the GUI? Forgive me if I sound a bit ignorant - I'm a scripting systems guy tasked with writing some stuff for the networking team to use. In the GUI, I am able to create a Virtual Server by specifying only the name, address and service port - everything else is left at "None". This is what I'd like to accomplish using PS and iControl, if possible. Thanks. - tharrison_91509
Nimbostratus
Bump - need some help with this. There has to be a way to create a Virtual Server using PowerShell with no HTTP profile. Just as an example, in the GUI, I can create a Virtual Server with the following settings:
Properties:
Name: test123
Destination: Host: 10.10.10.10
Service Port: 443, HTTPS
State: Enabled
Type: Standard
Protocol: TCP
OneConnect Profile: None
HTTP Profile: None
FTP Profile: None
SSL Profile (Client): None
SSL Profile (Server): None
Diameter Profile: None
SIP Profile: None
VLAN and Tunnel Traffic: All VLANs and Tunnels
SNAT Pool: Auto Map
Resources:
Default Pool: None
Default Persistence Profile: None
Fallback Persistence Profile: None
No iRules
No HTTP Class Profiles
Essentially a "blank" Virtual Server. I want to be able to do this so I can then populate those fields as necessary - and some of my virtual servers require some of those items to be set to "None". The one I'm having problems with is HTTP Profile - I can't get it to accept "None" and if I can do it in the GUI, I should be able to do it in PowerShell.
Thanks. - In looking at the code, you must pass in all parameters of the same 1st dimension size. The server code iterates thru that 1st dimension and then for the profiles, it iterates the 2nd dimension for each of the profiles for the given virtual server in the 1st dimension.
In looking that the code, I would think you could create a 2-D array with the second dimension empty.define definitions, wildmasks, and resources parameters declare empty 2-d array $Profile_Array = new-object "iControl.LocalLBVirtualServerVirtualServerProfile[][]", 1,0 Create the virtual VirtualServer.create($definitions, $wildmasks, $resources, $Profile_Array
I don't have a BIG-IP handy right now to test this on but it should work.
-Joe - tharrison_91509
Nimbostratus
Joe - that worked, but it threw an error:
Array assignment failed because index '0' was out of range.
At U:\Powershell\(TESTING)F5-Scripting.ps1:32 char:19
+ $Profile_Array[0][ <<<< 0] = $Profile
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : IndexOutOfRange
I can live with that, but i'd prefer if the error was gone. Thanks, though! - Sorry, meant to be more specific in that you shouldn't assign the value to the array as we've allocated it to size zero. Just comment out the $Profile_Array[0][0] = $Profile line. Array element [0][0] is no longer allocated so you can't assign to it.
Glad it worked!
-Joe - tharrison_91509
Nimbostratus
Let me ask you this - is it possible to simply "copy" a Virtual Server? Is there simple code to in effect do just that?My ultimate goal is to take a list of Virtual Servers that I currently have and in effect rename them. I know I can't rename Virtual Servers (or can I?) so I planned to loop through each Virtual Server, store all of its characteristics in variables and then create a new Virtual Server with a new name and apply each characteristic from the old one to it. It would be far easier if I could simply "copy" a Virtual Server, though....
- Unfortunately there isn't a "copy" option (in the API, GUI, or CLI). the only option for copying would be to edit the config directly and do a manual reload. That would be a nice enhancement request to put in if you really need it though.
-Joe - tharrison_91509
Nimbostratus
so now that I have a grasp on how to create pools and virtual servers, how would I go about retrieving ALL characteristics of a given virtual server or pool? Is there a list somewhere of all the information that PowerShell/iControl can pull from a VIP or pool?
By the way, thanks for all the help so far. - Brian_Rodriguez
Nimbostratus
I am extremely interested in this as well. My company has two datacenters in two different areas and we use them in a prod/dr configuration. If we create virtual servers in one location, we create an exact replica in our other location, which remains in standby and becomes active if the pool in the primary location goes down. A copy feature would be extremely helpful for these tasks. I would like to create the virtual server in the primary location, then copy it over to the DR location and only have to worry about the new ip addresses. Does anyone know how to script something like this or give me a step-by-step on performing this on the cli?
Thanks In Advance, Brian
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
