Friday, May 10, 2019

Start-Azure

This is a script to take control of the browser in logging into Microsoft Azure Portal.

I had the recurring irritant, that when I wanted to access my Azure Dashboard,   I had to go through 2 logins, and that is because I was using Chrome browser,  as it worked best in my environment...
and a proxy server login... it took a few steps and because of proxy server required yet another login.
Something not required on IE/Edge"

I needed a way to send keystrokes to the open window and control it that way.
So, I got out some Whip It up a tude,  and started playing in the shell, and searching with google.
(Google knows everything, ya just got to know how to ask.)
A Blog article by Massimo Santin, I found this on his blog.
a-simple-powershell-script-to-send-keys-to-an-application-windows/

Send-Keys.ps1
<#
.SYNOPSIS
Send a sequence of keys to an application window

.DESCRIPTION
This Send-Keys script send a sequence of keys to an application window.
To have more information about the key representation look at http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
(C)2013 Massimo A. Santin - Use it at your own risk.

.Source 
https://invista.wordpress.com/2013/08/16/a-simple-powershell-script-to-send-keys-to-an-application-windows/

.PARAMETER ApplicationTitle
The title of the application window

.PARAMETER Keys
The sequence of keys to send

.PARAMETER WaitTime
An optional number of seconds to wait after the sending of the keys

.EXAMPLE
Send-Keys "foobar - Notepad" "Hello world"

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad".

.EXAMPLE
Send-Keys "foobar - Notepad" "Hello world" -WaitTime 5

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad" 
and wait 5 seconds.

.EXAMPLE 
    New-Item foobar.txt -ItemType File; notepad foobar.txt ; Send-Keys "foobar - Notepad" "Hello world{ENTER}Ciao mondo{ENTER}" -WaitTime 1; Send-Keys "foobar - Notepad" "^s"

This command sequence creates a new text file called foobar.txt, opens the file using a notepad,
writes some text and saves the file using notepad.

.LINK
http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
#>
Function Send-Keys {
param (
    [Parameter(Mandatory=$True,Position=1)]
    [string]
    $ApplicationTitle,

    [Parameter(Mandatory=$True,Position=2)]
    [string]
    $Keys,

    [Parameter(Mandatory=$false)]
    [int] $WaitTime
    )

# load assembly cotaining class System.Windows.Forms.SendKeys
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Add-Type -AssemblyName System.Windows.Forms

# add a C# class to access the WIN32 API SetForegroundWindow
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class StartActivateProgramClass {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

# get the applications with the specified title
$p = Get-Process | Where-Object { $_.MainWindowTitle -eq $ApplicationTitle }
if ($p) 
{
    # get the window handle of the first application
    $h = $p[0].MainWindowHandle
    # set the application to foreground
    [void] [StartActivateProgramClass]::SetForegroundWindow($h)

    # send the keys sequence
    # more info on MSDN at http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
    [System.Windows.Forms.SendKeys]::SendWait($Keys)
    if ($WaitTime) 
    {
        Start-Sleep -Seconds $WaitTime
    }
}
}


So I went about utilizing this function to do what I needed, I compiled the information about the windows that needed to be open and what needed to be entered into the windows. and created this script.

start-azure.ps1
<#  
    .NOTES
    ===========================================================================
     Created on:    4/10/2019 6:53 PM
     Created by:    Richard Stoddart
     Organization:  Fabricam.com
     Filename:   start-azure.ps1    
    ===========================================================================
    .DESCRIPTION
        Open Azure session on workstation. 
#>

#Setup
#load function Send-Keys
. "$PSScriptRoot\Send-Keys.ps1"

$EmailFile = "$PSScriptRoot\Email.txt"
$CipherFile = "$PSScriptRoot\Cipher.txt"


#deal with email setting
# delete $EmailFile to restart this query. 

IF (!(Test-Path $EmailFile))
{
    read-host -Prompt "EMAIL File not found! Please Enter EMAIL:" |
    Out-File $EmailFile
}

$Email = Get-Content $EmailFile

#Deal with password cipher

# delete $cipherfile to restart this query. 
IF (!(Test-Path $CipherFile))
{
    read-host -Prompt "Password File not found! Please Enter new Password:" -AsSecureString |
    ConvertFrom-SecureString |
    Out-File $CipherFile
}

# https:portal.azure.com
& "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  "https://portal.azure.com"
#Waiting for startup. 
$Loop = 100
While ($Loop -gt 1)
{
    If (((Get-Process | ? { $_.ProcessName -eq "chrome" } | 
? { $_.mainwindowtitle -like "Sign in*" }).mainwindowtitle)) { $loop = 0 }
    else { $loop-- }
    if ($loop -eq 1) { Return "timeout opening azure site" }
    Write-Host "." -NoNewline
    Start-Sleep -Seconds 1
}
"`nAzure login open, Proceed"
#enter login info
Send-Keys -ApplicationTitle "Sign in to Microsoft Azure - Google Chrome" -Keys "$email`r"
Start-Sleep -Seconds 2
Send-Keys -ApplicationTitle "Sign in to Microsoft Azure - Google Chrome" -Keys "`r"


#Proxy Server login section

#Waiting for proxy. 
$Loop = 100
While ($Loop -gt 1)
{
    If (
      ((Get-Process | ? { $_.ProcessName -eq "chrome" } |
      ? { $_.mainwindowtitle -like "https://gfs.private.fabricam.com*" }).mainwindowtitle)
     ) 
     { $loop = 0 }
    else { $loop-- }
    if ($loop -eq 1) { Return "timeout logging into proxy" }
    Write-Host "." -NoNewline
    Start-Sleep -Seconds 1
}

$GFSTitle = (Get-Process | ? { $_.ProcessName -eq "chrome" } |
    ? { $_.mainwindowtitle -like "https://gfs.private.fabricam.com*" }).mainwindowtitle

$Cipher = get-content $CipherFile | ConvertTo-SecureString
$Pass = (New-Object PSCredential "user", $Cipher).GetNetworkCredential().Password

Send-Keys -ApplicationTitle $GFSTitle -Keys "$ENV:USERDOMAIN\$EnV:USERNAME`t$Pass`r"

Clear-Variable Pass, Email


* Set the proxy server name to your environment Proxy server web name.

This is used in the include so it must be in the same DIR.

Once I worked the bugs out it, it works like a charm. and I created a Shortcut on the desktop to link to the script to go and open Azure for me.

It may take a little tweaking in your environment. Your proxy server may have a different name, and I did edit this sample to a fictitious company.

I did some password encryption to securely store the Proxy server password. To keep the security wonks off my case.


Wednesday, May 1, 2019

Powershell + Devops Global Summit 2019

I am here at the summit and its great!
YouTube: PowerShell Devops Summit report 1
The first day was opening keynote speeches

First up wall Will Anderson and he covered the welcome and introduction to the conference.
he did great with covering what we needed to know about the con.

then we got blown away by the powerhouses, Don Jones and Jeffry Snover.

Here is a link to my first video report.  It's going to be impromptu, no editing but I hope you enjoy it.

here is the first video3
LINK:  PowerShell Devops Summit report 1

LINK: PowerShell Devops Summit 2019 - report 2

LINK:   PowerShell Devops Summit 2019 report 3

LINK:  PowerShell Devops Summit 2019 report 4