The ramblings of an old IT Pro travelling the digital byways.

Tuesday, June 28, 2016

Convert string to date and time in PowerShell

Sometimes, the output of a command will include date and time information as a system.string, but you need it in a true datetime format.  Simple solution.

In this case the queueTime property of my $objItem object is a system.string.

$objItem.queueTime

Output:
2016-06-17T13:27:22.3998361Z

Just use the [datetime] accelerator to convert the string to datetime object

[datetime]$objItem.queueTime

Output:
Friday, June 17, 2016 8:27:22 AM


Want to take it a step further?  Want to format that datetime?

"{0:MM/dd/yyyy HH:mm:ss}" -f [datetime]$objItem.queueTime

Output:
06/17/2016 08:27:22

Tuesday, June 21, 2016

Move Azure Resources

Sometimes it becomes necessary to move an Azure resource from one resource group to another within a subscription.  While this is technically not difficult, it can be a bit of a pain.  Let's let the computers do what they do best.

Following is a quick PowerShell script.  It assumes you've already authenticated to your subscription (Login-AzureRmAccount) and selected the appropriate context (Select-AzureRMSubscription).


<#
.NOTES
===================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on:   5/19/2016 3:11 PM
Created by:   TowerBE
Organization: XXXXXXX
Filename:             move-resource.ps1
===================================================================
.SYNOPSIS
Moves an Azure Resource.

.DESCRIPTION
A quick and dirty script to move an Azure resource from one Resource
Group to another

.PARAMETER resName
The name of the Azure Resource to be moved.

.PARAMETER oldRg
The name of the Azure Resource Group where the resource currently exists.

.PARAMETER newRg
The name of the Azure Resource Group where you're moving the resource.

.EXAMPLE
move-resource.ps1 -resName "myservicebus" -oldRg "busted" -newRg "hotness"
This will move the resource myservicebus from the busted resource group
to the hotness resource group.
#>

[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$resName,
[Parameter(Mandatory = $true)]
[string]$oldRg,
[Parameter(Mandatory = $true)]
[string]$newRg
)

$resource = Get-AzureRmResource -ResourceName "$resName" -ResourceGroupName "$oldRg"
Move-AzureRmResource -DestinationResourceGroupName "$newRg" -ResourceId $resource.ResourceId

Please note that not all resources can be moved, and not all resources support moving fully.  I've used this script for moving storage accounts, service bus namespaces and other resources types, but don't make any guarantees. If you want to read more about moving resources, go to https://azure.microsoft.com/en-us/documentation/articles/resource-group-move-resources/.

Sunday, June 12, 2016

Reload PowerShell Profile

When editing your PowerShell profile, you need to reload the profile to check or implement your changes.  There are three ways to accomplish this.


  1. Start a new PowerShell session
  2. Type the command & $Profile
  3. Type the command . $Profile


Way one is inconvenient.
Way two invokes the $Profile script.
Way three sources the $Profile script.

Are two and three really different?  Dunno, the end result is the same.

Saturday, June 11, 2016

Persisting Azure RM Credentials: Profile switch in PowerShell

I manage a number of Azure subscriptions for my employer. One of the things that used to drive me nuts is having to constantly go through the Login process, especially since we use Multi Factor Authentication, and then having to switch back and forth between subscriptions.

I recently stumbled across an blog about the Save-AzureRmProfile command. Did I mention I'm relatively new to Azure? While blog provided useful information and simplified things for me, I took it one step further (have to give my buddy Rick credit for sparking the idea).

 I saved a profile file for each one of my subscriptions,

    Save-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 

then edited my PowerShell profile to include a function for each profile (hint: if you don't have a PowerShell profile, New-Item –type file –force $profile).

    function goDev { 
        Select-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 
    }

Now, if I want to switch to my development subscription while in PowerShell, I simply type "goDev" and I'm there. I have a separate go function for each of the subscriptions I manage.

Saving my Azure profiles and modifying my PowerShell profile: 20 minutes.
Eliminating yet another pesky annoyance: Priceless!

About Me

My photo
A living, breathing contradiction.