Thursday 4 October 2018

Install RSAT Tools on Windows 10

I've written a short script to download and install the RSAT tools for Windows 10. This script works on the recent versions of Windows  10 only! Here goes:

# Install-RSATTools.PS1
# Thomas Lee - doctordns@gmail.com

# 1. Get Windows Client Version and Hardware platform
$Key = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$CliVer = (Get-ItemProperty -Path $Key).ReleaseId
$Platform = $ENV:PROCESSOR_ARCHITECTURE
"Windows Client Version : $CliVer"
"Hardware Platform : $Platform"

# 2. Create URL for download file
# NB: only works with 1709 and 1803.
$LP1 = 'https://download.microsoft.com/download/1/D/8/'+
'1D8B5022-5477-4B9A-8104-6A71FF9D98AB/'
$Lp180364 = 'WindowsTH-RSAT_WS_1803-x64.msu'
$Lp170964 = 'WindowsTH-RSAT_WS_1709-x64.msu'
$Lp180332 = 'WindowsTH-RSAT_WS_1803-x86.msu'
$Lp170932 = 'WindowsTH-RSAT_WS_1709-x86.msu'
If ($CliVer -eq 1803 -and $Platform -eq 'AMD64') {
$DLPath = $Lp1 + $lp180364}
ELSEIf ($CliVer -eq 1709 -and $Platform -eq 'AMD64') {
$DLPath = $Lp1 + $lp170964}
ElseIf ($CliVer -eq 1803 -and $Platform -eq 'X86') {
$DLPath = $Lp1 + $lp180332}
ElseIf ($CliVer -eq 1709 -and $platform -eq 'x86') {
$DLPath = $Lp1 + $lp170932}
Else {"Version $cliver - unknown"; return}

# 3. Display the download details
"RSAT MSU file to be downloaded:"
$DLPath

# 4. Use BITS to download the file
$DLFile = 'C:\foo\Rsat.msu'
Start-BitsTransfer -Source $DLPath -Destination $DLFile

# 5. Check Authenticode signature
$Authenticatefile = Get-AuthenticodeSignature $DLFile
If ($Authenticatefile.status -NE "Valid")
{'File downloaded fails Authenticode check'}
Else
{'Downloaded file passes Authenticode check'}

# 6. Install the RSAT tools
$WusaArguments = $DLFile + " /quiet"
'Installing RSAT for Windows 10 - Please Wait...'
$Path = 'C:\Windows\System32\wusa.exe'
Start-Process -FilePath $Path -ArgumentList $WusaArguments -Wait

# 7. Get RSAT Modules
Get-Module -Listavailable | Where-Object Name -like '*rsat*'

Friday 6 February 2015

Install-RMAzureVmCert

Function Install-WinRMAzureVmCert {
<#
.SYNOPSIS
Downloads and installs the certificate created or
initially uploaded during creation of a Windows based
Azure Virtual Machine.
.DESCRIPTION
Downloads and installs the certificate created (or uploaded)
during the creation of a Windows based Azure Virtual Machine.
Running this function obtains and installs the certificate into
your local machine certificate store. Writing to the localhost's
cert store requires PowerShell to run elevated. Once the
certificate is installed, you can connect to Azure VMs using SSL
to improve security.
.NOTES
File Name : Install-WinRmAzureVmCert.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0, Azure module 8.12
Tested : PowerShell Version 5
.PARAMETER SubscriptionName
The name of the Azure subscription whose VMs you want to get
certificates from. Use quotes around subscription names
containing spaces.
.PARAMETER ServiceName
The name of the Azure cloud service the virtual machine is
deployed in.
.PARAMETER VmName
The name of the Azure virtual machine to install the
certificate for.
.EXAMPLE
Install-WinRmAzureVMCert -SubscriptionName "my subscription" `
-ServiceName "mycloudservice" -Name "myvm1"
#>
[Cmdletbinding()]
param(
[string] $SubscriptionName,
[string] $CloudServiceName,
[string] $VMName)

Function IsAdmin
{
Write-Verbose 'Checking user is an Admin'
$IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
Write-Verbose "User is admin: [$IsAdmin]"
Return $IsAdmin
}

# First, ensure the user is admin and that the VM exists
if((IsAdmin) -eq $false)
{
Write-Error "Must run PowerShell elevated to install WinRM certificates."
return
}
If (-not (Get-AzureVM -ServiceName $CloudServiceName -Name $VMName))
{
Write-Error "VM $VMName does not exist."
return
}

# Pre-reqs OK so let's get started...
Write-Verbose "Getting WinRM Certificate for Service: [$CloudServiceName] and VMname: [$VMName]"
$AzureVM = (Get-AzureVM -ServiceName $CloudServiceName -Name $VMname).vm
$WinRmVmTp = $AzureVM.DefaultWinRMCertificateThumbprint
$AzureX509cert = Get-AzureCertificate -ServiceName $CloudServiceName -Thumbprint $WinRmVmTp -ThumbprintAlgorithm sha1
Write-Verbose "Found certificate with thumbprint: $WinRmVmTp"

# Now get cert into our cert store
# First create a temp file and dump the certificate data to it
$CertTempFile = [IO.Path]::GetTempFileName()
Write-Verbose "Using temp file: [$CertTempFile]"
$AzureX509cert.Data | Out-File $CertTempFile
Write-Verbose 'Temp file contains cert data'

# Create a certificate object from this file
$CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certTempFile

# Now get the local machine's trusted root store
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
Write-Verbose "[$($store.location)] [$($store.name)] cert store found sucessfully"

# Now Add the cert object to the store
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($CertToImport)
$store.Close()
Write-Verbose 'Certificate written to store'

# And nuke the temp file
Remove-Item $certTempFile
Write-Verbose 'Temp file removed'
}
Technorati Tags: ,

Sunday 1 February 2015

New-HttpVmEndpoint.ps1

Function New-HttpVmEndpoint {
<#
SYNOPSIS
This script defines a function to add an HTTP endpoint
to an Azure VM.
.DESCRIPTION
This script uses the Azure module to change the VM instance size.
.NOTES
File Name : New-HttpVmEndpoint.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 3.0, Azure module
Tested : PowerShell Version 5
.LINK
This script posted to:
http://www.pshscripts.blogspot.com

.EXAMPLE
New-HttpVmEndpoint -VmName 'psh1' -ServiceName 'psh1'

OperationDescription OperationId OperationStatus
-------------------- ----------- ---------------
Update-AzureVM 99709683-8c4b-607e-8856-5958a6967147 Succeeded

#>
[Cmdletbinding()]
Param (
$VmName,
$ServiceName
)
# So here, get the vm, add the endpoint, then update the VM
Get-AzureVM -ServiceName $ServiceName -Name $VmName |
Add-AzureEndpoint -Name "Http" -Protocol "tcp" -PublicPort 80 -LocalPort 80 |
Update-AzureVM

}

# And Test It
New-HttpVmEndpoint -vm 'psh1' -servicename 'psh1'

 

Technorati Tags: ,,,

Monday 19 January 2015

AzureVM.Format.Ps1xml

<!--

   Description
      This file contains display XML for the object returned from
      Get-AzureVM. It's purpose is to provide better (default) output
      from Get-AzureVm.
   Written by
     
Thomas Lee (tfl@psp.co.uk
   Copyright:
    
PS Partnership 2015
-->
<?xml version="1.0" encoding="utf-16"?>
<Configuration>
  <ViewDefinitions>
    <View>
      <Name>AzureVM</Name>
      <ViewSelectedBy>
        <TypeName>Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVMRoleListContext</TypeName>
      </ViewSelectedBy>
      <TableControl>
      <AutoSize />
        <TableHeaders>
          <TableColumnHeader>
             <Label>Name</Label>
          </TableColumnHeader>
          <TableColumnHeader>
             <Label>Service Name</Label>
          </TableColumnHeader>
          <TableColumnHeader>
             <Label>Status</Label>
          </TableColumnHeader>
          <TableColumnHeader>
              <Label>Hostname</Label>
          </TableColumnHeader>
          <TableColumnHeader>
              <Label>FQDN</Label>
          </TableColumnHeader>
          <TableColumnHeader>
              <Label>Instance Size</Label>
          </TableColumnHeader>         
          <TableColumnHeader>
              <Label>IP Address</Label>
          </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
          <TableRowEntry>
            <TableColumnItems>
              <TableColumnItem>
                <PropertyName>Name</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>Servicename</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>status</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>hostname</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>DNSName</PropertyName>
              </TableColumnItem>             
              <TableColumnItem>
                <PropertyName>instancesize</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>IPaddress</PropertyName>
              </TableColumnItem>
            </TableColumnItems>
          </TableRowEntry>
        </TableRowEntries>
      </TableControl>
    </View>
  </ViewDefinitions>
</Configuration>

Thursday 4 December 2014

Measure-TypeAccelerator.ps1

<#
.SYNOPSIS
This function 'measures' (counts) the number of
Type Accelerators on your system.
.DESCRIPTION
This function counts the number of type accelerators are
on your systems and returns that number.
.NOTES
File Name : Measure-TypeAccelerator.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : Version 3
.LINK
Script Repository
http://www.pshscripts.blogspot.com
.Example
Psh[C:\foo]> Measure-TypeAccelerator.ps1
84
.Example
Psh[C:\foo]> Count-TypeAccelerator.ps1
84

#>

Function Measure-TypeAccelerator {
# Define parameters and enable advanced functions
# NB no parameters!
[cmdletbinding()]
Param ()

# Start of function
Write-Verbose 'Getting acount of all Type Accelerators'
$Count = (([PSObject].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get).GetEnumerator() |
Measure).count
Write-Verbose "$Count Type Accelerators found on $(hostname)"
Return $count
}

# Set an alias
Set-Alias CTA Measure-TypeAccelerator
Set-Alias MTA Measure-TypeAccelerator

Wednesday 3 December 2014

Remove-TypeAccelerator

<#
.SYNOPSIS
This script removes a type accelerator from your system
.DESCRIPTION
This script removes a NEW TA from your system.
.NOTES
File Name : Remove-TypeAccelerator.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : Version 3
.LINK
Script Repository
http://www.pshscripts.blogspot.com
.Example
Psh[C:\foo]> Remove-TypeAccelerator tfl
Alias [tfl] removed
#>

###
# Start of script
###

Function Remove-TypeAccelerator {
[cmdletbinding()]
param (
[Parameter(Mandatory=$true)]
[string] $alias
)

# Start of function
Try
{
[void] ([PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::remove($alias))
}
Catch
{
Write-Error "Failed to remove alias [$alias]"
return
}

# Return
"Alias [$alias] removed"
}

Set-Alias rta Remove-TypeAccelerator

# Test this out
Remove-TypeAccelerator 'foo3'

Tuesday 2 December 2014

New-TypeAccelerator.ps1

<#
.SYNOPSIS
This script creates a new type accelerator on your system
.DESCRIPTION
This script adds a NEW TA to your system.
.NOTES
File Name : New-TypeAccelerator.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : Version 3
.LINK
Script Repository
http://www.pshscripts.blogspot.com
.Example
Psh[C:\foo]> New-TypeAccelerator tfl system.int32
Alias [tfl] added for type [system.int32]
#>

###
# Start of script
###

function New-TypeAccelerator {
[cmdletbinding()]
param (
[Parameter(Mandatory=$true)]
[string] $alias,

[Parameter(Mandatory=$true)]
[string] $type
)

# Start of function
Try
{
([PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::add($alias,$type))
}
Catch
{
Write-Error "Failed to add alias [$alias] to type [$type]"
return
}

# Return
"Alias [$alias] added for type [$Type]"
}

Set-Alias nta New-TypeAccelerator

# Test this out
New-TypeAccelerator

Monday 1 December 2014

Get-TypeAccelerator.ps1

<#
.SYNOPSIS
    This script defines a function to get a list of
    Type Accelerators in PowerShell and displays them nicely
.DESCRIPTION
    This script gets the details of type accelerators in the system. 
    Earlier versions of this script uses a different class, which
    has been taken private and is not available any more. This 
    script also creates an alias for the function. GTA takes a
    string parameter which is used as a regular expression to 
    find a subset of type accelerators.
.NOTES
    Additional Notes, eg
    File Name  : Get-TypeAccelerator.ps1
    Author     : Thomas Lee - tfl@psp.co.uk
    Requires   : Version 3
.LINK
    Original article:
      http://www.nivot.org/2008/12/25/ListOfTypeAcceleratorsForPowerShellCTP3.aspx
    Script Repository
      http://www.pshscripts.blogspot.com
.Example
    Psh[C:\foo]>Get-TypAccelerator int
     Name   Type                      
    ----   ----                      
    bigint System.Numerics.BigInteger
    int    System.Int32              
    int16  System.Int16              
    int32  System.Int32              
    int64  System.Int64              
    uint16 System.UInt16             
    uint32 System.UInt32             
    uint64 System.UInt64       

.Example
    Psh[C:\foo]>Get-TypAccelerator 's$'
    Name              Type                                                   
    ----              ----                                                   
    Alias             System.Management.Automation.AliasAttribute            
    cimclass          Microsoft.Management.Infrastructure.CimClass           
    ipaddress         System.Net.IPAddress                                   
    mailaddress       System.Net.Mail.MailAddress                            
    SupportsWildcards System.Management.Automation.SupportsWildcardsAttribute
    wmiclass          System.Management.ManagementClass    
#>

###
#   Start of script
###
Function Get-TypeAccelerator {
[Cmdletbinding()]
param (
  [string] $accelerator
)

([PSObject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get).getenumerator() |
  Select-object @{Name="Name"; expression={$_.key}},  
                @{name="Type"; expression={$_.value}} | 
  where name -match $accelerator | Sort name | Format-Table -Autosize
}
Set-Alias gta Get-TypAccelerator

# Test script
Get-TypAccelerator  int  # anything with int 
Get-TypeAccelerator 's$' # ends in s

Friday 28 November 2014

Get-Stack1.ps1


<#
.SYNOPSIS
MSDN sample showing push and other stack processing using PowerShell
.DESCRIPTION
This script creates a script then performs stack operations.
.NOTES
File Name : Get-Stack1.p1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
http://www.pshscripts.blogspot.com
.EXAMPLE
PSH [C:\foo]: .\get-stack1.ps1'
Stack at start:
fox
quick
The

(Pop) fox
Stack value after Pop:
brown
quick
The

(Pop) brown
Stack values after 2nd pop:
quick
The

(Peek) quick
Stack values after a peek:
quick
The
#>

##
# start of script
###

# Create and initialise a new stack object
$mystack = new-object system.collections.stack
$myStack.Push( "The" )
$myStack.Push( "quick" )
$myStack.Push( "brown" )
$myStack.Push( "fox" )

# Display the Stack
"Stack at start:"
$myStack
""# Pop an element from the Stack.
"(Pop)`t`t{0}" -f $myStack.Pop()
"Stack value after Pop:"
$myStack
""

# Pop another element from the Stack
"(Pop)`t`t{0}" -f $myStack.Pop()

# Display the Stack after 2nd pop
"Stack values after 2nd pop:"
$myStack
""

# Peek at the front
"(Peek)`t`t{0}" -f $myStack.peek()

# Display the Stack after the peek
"Stack values after a peek:"
$myStack

Thursday 27 November 2014

Zip-Pshscripts3.ps1

#Requires –Version 5.0
#
#.Synopsis
#    Creates a zip file of PowerShell scripts 
#.Description
#    The script creates a zip file containing all the files, 
#    recursing through the top level PowerShell Script Library folder.
#
#.Notes
#    This script require PowerShell V5 for the zip file cmdlets!
#    Author - Thomas Lee - tfl@psp.co.uk
#
#.Example
#    PS [c:\foo]>  .\Zip-PSHScripts3.ps1
#    Total files  : 347
#    ps1 files    : 342
#    txt files    : 1
#    other  files : 4


# Define what to zip and from where
$zipfile  = "C:\foo\ScriptLib.ZIP"
$zipfrom  = "C:\Users\tfl\Dropbox\PowerShell Script Library (master)"
$recurse  = "true"
$ziptoadd = "ps1"

# Check it out
if ( ! (Test-path -Path $zipfrom ))
{
  Write-Host 'scripts folder does not exist'
}

# Zip it up!
Try 
  {
    Compress-Archive -Path $zipfrom -DestinationPath $zipfile -CompressionLevel Optimal -Update
  }
Catch 
  {
  Write-Host ' Error Zipping up the script library'
  $Error[0]
  }

# Stats
$files = ls $zipfrom -file -recurse
$files_ps1 = $files | Where-Object Extension -eq '.ps1'
$files_txt = $files | Where-Object Extension -eq '.txt'
$files_other = $files | Where-Object { $_.extension -NE '.PS1' -and $_.Extension -ne '.txt'} 

"Total files  : {0}" -f $($files.count)
"ps1 files    : {0}" -f $($files_ps1.count)
"txt files    : {0}" -f $($files_txt.count)
"other  files : {0}" -f $($files_other.count)


# All done
ls $zipfile