Set-Creds
Connect-Vcenter
Here are 2 seemingly useless functions that I’ve loved for some time now. Both tasks that are achieved are effectively not that difficult individually, or frankly by them self, but when repeated multiple times become very tiresome!
Set-Creds
The first function, Set-Creds
, is a basic wrapper for the pre-existing Get-Credential
function already supplied by Microsoft. This command can easily be run, the data stored in an encrypted Variable (to obfuscate the data from prying eyes) and then passed on to various other commands where the current user shell context (typically the currently logged in user/password) is not suitable.
Here is the function it self.
function Set-Creds {
param(
[parameter(Mandatory=$false)]$VariableName='creds',
[parameter(Mandatory=$False)][switch]$ClearVariable=$false
)
IF ($ClearVariable -eq $false)
{
Set-Variable -Name $VariableName -Value (Get-Credential) -Scope global
}
ElseIf ($ClearVariable -eq $true)
{
Remove-Variable -Name $VariableName -Scope global
}
}
Here is the code in action:
Connect-VCenter
Here is another seemingly easy task to achieve. Loading the VMWare PowerCLI commands and then connecting to what ever VCenter you have in mind. This is actually again, a very simple task, but IT CAN BE MADE SO MUCH EASIER. How you might ask? Yes, that’s right, another Function!!!!
WARNING – This is a little lazy this code. I’ve not tested this on machines running anything less than PowerCLI version 5 – Prior to that, I would need to write some try/catch kinda stuff – So, its not going to break anything, but you might get a few errors.
function Connect-Vcenter { param( [parameter(Mandatory=$true)]$vcenter, [parameter(mandatory=$false)]$Credentials=$null, [parameter(Mandatory=$false)][switch]$Multiple=$false ) #Load snap-in & "C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" if ($Credentials -eq $null) { $credentials = Get-Credential } if ($Multiple -eq $true) { # Connect up - Allows for multiple vcenter connections :O Connect-VIServer $vCenter -Credential $Credentials } else { #Connect to vCenter - But not if you are already connected if ($global:DefaultVIServers.Count -lt 1) { Connect-VIServer $vCenter -Credential $Credentials } else { Write-Host "Already connected to a vcenter server!!" } } }
Here we show the code in use, much the same as before…
All together now!
Right, now here is where things start to make a little more sense. Lets set the scene. You’ve arrived onsite at customers site. You need to connect to their VCenter server and do some work and you are in a hurry, your username and password aren’t on the same domain as the customers. Easy, now we just use the Set-Creds
and the Connect-VCenter
commands from within the first Powershell you can get open:
So, what have we achieved? We’ve managed to load the VMWare PowerCLI into a standard Powershell Shell, create a variable with a SECURELY stored password and connect to the vcenter server all in a few simple commands.
Plenty of uses for this moving forward, I’m sure you’ll see me reference them in the future as I build up my online arsenal of functions. Which leads me to another point. How will having these functions make life easier if I am constantly having to load them into a shell? I’ve plans to write up how I make my own custom Powershell module and also customizing your $Profile to ensure they are always in there! But that is for a later date…