Quick Tip: Bulk remove selected vSphere snapshots

If you want to manually select the snapshots you want to delete in vSphere through PowerCLI the only viable option is piping your snapshots to the Out-GridView Cmdlet. Unfortunately it uses the default display properties of the snaps

$snaps = Get-VM | Get-Snapshot
$snaps | Out-GridView -PassThru 

VMwareSnapshotsOutGridViewDefaultProperties

As you can see the properties that are shown by default don’t help at all. So what do we do? Right, we select the properties we want to see before piping to Out-GridView:
VMwareSnapshotsOutGridViewSelectedProperties

So far so good, but what happens when we pipe the selected snapshots to Remove-Snapshot? It fails. Select-Object creates new objects and they have a different type than the snapshot objects:
VMwareSnapshotsObjectType

The best workaround is to use the Select-FromGridView function from the technet gallery which separates the displayed object’s properties from the objects passed down the pipeline:

$delete = $snaps | Select-FromGridView -By vm,created,name,description,sizegb
$delete | Remove-Snapshot -RunAsync -Confirm:$false -Verbose

Hope this helps 🙂

Quick tip: Pre-compile assemblies to improve PS startup time

Just a quick reminder for myself.

$FrameworkDir=[Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
$NGENPath = Join-Path $FrameworkDir 'ngen.exe'

[AppDomain]::CurrentDomain.GetAssemblies() |
  Select-Object -ExpandProperty Location |
  ForEach-Object { & $NGENPath """$_""" } 

Powershell loads and JIT compiles tons, and tons, and tons of stuff. This takes a lot of excessive CPU cycles and disk I/O.

ngen.exe (Native Image Generator) can be used to precompile the assemblies that Powershell loads during startup.

Source:
http://serverfault.com/questions/761301/calling-powershell-exe-is-extremely-slow

Best Practice – Git commit messages

Best practices:

  • The first line should contain 50 characters or less and explain the change very briefly.
  • The next paragraph should contain a bit more explanation about what you are trying to solve. Try to keep the length of the line under 72 characters; this way, it’s easy to scan people.
  • If you have more information that you want to tell, you can do so in the next paragraph. This can be as long and detailed as you want. More details are better!
  • If you want to create a list, use the – or * characters to do so.
  • When you’re working on an issue, add a reference to that issue in the last line.

An example of a nicely formatted Git commit message is as follows:

Show an error page if the user is not logged in

When a user is not logged in, we do not show an error message.
This was confusing to some users. We now show the correct error
message.

* Here is some extra information
* And here is some more bullet information

Fixes: #1123

Quick Tip: Use Extended Properties for your MSSQL DBs

It makes sense to use extended properties for your MS SQL databases to add additional information to them like:

  • Dev/Test/Prod
  • Point of contact/customer information
  • various timestamps

Here’s an example on how to use extended properties:


# load required assembly (only needs to be loaded once per session) and open connection to instance
# heads-up: this was done locally on the SQL server (2014)
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
$instance = "srvsql051\inst5"
$Server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $Instance
# retrieve databases
$databases = $server.Databases
# get test db
$db = $databases | where name -like "TestDB"
$MyExtProp = New-Object Microsoft.SqlServer.Management.Smo.ExtendedProperty
$MyExtProp.Parent = $db
$MyExtProp.Name = "MS_Description"
$MyExtProp.Value = "Test Database"
$MyExtProp.Create()
<# access like this:
PS C:\Windows\system32> $db.ExtendedProperties['MS_Description'].Value
Test Database
PS C:\Windows\system32> $db.ExtendedProperties
Parent : [TestDB]
Value : Test Database
Name : MS_Description
Urn : Server[@Name='SRVSQL051\INST5']/Database[@Name='TestDB']/ExtendedProperty[@Name='MS_Description']
Properties : {Name=Value/Type=System.Object/Writable=True/Value=Test Database}
UserData :
State : Existing
Parent : [TestDB]
Value : hey there
Name : MS_mycustomprop
Urn : Server[@Name='SRVSQL051\INST5']/Database[@Name='TestDB']/ExtendedProperty[@Name='MS_mycustomprop']
Properties : {Name=Value/Type=System.Object/Writable=True/Value=hey there}
UserData :
State : Existing
#>

Quick Tip: Set-ADAccountPassword -Whatif fixed with Server 2016

Hey guys,

just a quick heads-up to all of those who had to deal with this unfortunate bug:

Set-ADAccountPassword -Identity $User -NewPassword $Password -Whatif

Sets the password anyway. I checked by looking at the passwordlastset attribute of the user and found it had been reset.

It’s going to be fixed with Server 2016 according to Microsoft:

This is fixed in Windows Server 2016 and the accompanying RSAT.

 

Source: http://windowsserver.uservoice.com/forums/304621-active-directory/suggestions/10844361-set-adaccountpassword-whatif-does-not-work

PS Profile – Quick tip

I’m lazy, and so should you be, even if that means to save just a second when typing a command in the console.


$PSDefaultParameterValues.Add("format-table:autosize",$True)

view raw

ps_profile.ps1

hosted with ❤ by GitHub

 

Since I use | format-table -autosize or rather | ft -au quite a lot I can skip specifying the autosize switch entirely.