Useful PowerShell profile code

2016/07/22

To check the location for your Powershell profile file, type $profile .
To reload your PowerShell profile, use & $profile .

Edit your PowerShell profile to set custom colors and console size as follow:

$host.UI.RawUI.BackgroundColor = ‘Gray’
$host.UI.RawUI.ForegroundColor = ‘Black’

$pshost = get-host
$pswindow = $pshost.ui.rawui

$newsize = $pswindow.buffersize
$newsize.height = 9000
$newsize.width = 140
$pswindow.buffersize = $newsize

$newsize = $pswindow.windowsize
$newsize.height = 65
$newsize.width = 140
$pswindow.windowsize = $newsize

# You’ll need a HOME variable defined as environment variable
cd $env:HOME

Clear-Host

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


PowerShell function for registry value test

2016/07/20

Below is a useful function for checking the existence and the value of a registry key.
Might need some rework, it’s not in a final version

function Test-RegistryValue
{
  param(
    [Parameter(Mandatory=$true)] [string]$Path,
    [Parameter(Mandatory=$true)] [string]$Name,
    [Parameter(Mandatory=$false)] $KeyValue
  )

  if( -not (Test-Path -Path $Path -PathType Container) ) {
    return $false
  }

  $properties = Get-ItemProperty -Path $Path
  if( -not $properties ) {
    return $false
  }
  else {
    $value = ($properties | Select-Object -ExpandProperty $Name)
    if ($value.ToString().ToLower() -eq $KeyValue.ToString().ToLower()) {
      return $true
    }
    else {
      return $false
    }
  }

  $member = Get-Member -InputObject $properties -Name $Name
  if( $member ) {
    return $true
  }
  else {
    return $false
  }
}

Note: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.


Upload a ZIP to Azure storage account in PowerShell

2016/06/23

This was tested under PowerShell 5, with Azure PowerShell module installed.

After you download your Azure publish settings file from https://manage.windowsazure.com/publishsettings/index?client=powershell&schemaVersion= you can use Get-AzureSubscription to choose a subscription for the upload (if you have several on your account)

if(-not (Get-Module Azure)) {
  Import-Module Azure
}

Import-AzurePublishSettingsFile “path/to/your/azure/publish/settings/file”

(Get-AzureSubscription).SubscriptionName # Choose subscription if several present

Select-AzureSubscription -SubscriptionName “YourSubscritionNameHere”
$storAcc = ‘companyartifacts’
$storCntr = ‘builds’
$blob = ‘Buildxyz.zip’
$key = (Get-AzureStorageKey -StorageAccountName $storAcc).Primary

$ctxt = New-AzureStorageContext -StorageAccountName $storAcc -StorageAccountKey $key

Write-Output “Uploading ZIP to Azure…”

Set-AzureStorageBlobContent -File “path\to\Buildxyz.zip” -Container $storCntr -Blob $blob -BlobType Block -Context $ctxt -Force

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.


Deleting folder SymLinks in PowerShell 4 without wiping folder content

2016/06/15

How ?
Use the old CMD for this:

cmd.exe /c rmdir “yourSymLinkFolder”

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.
Note 3: While I may have tested myself most of the bits of code posted here, please be aware that you’re trying the code/scripts at your own risk, and I take no responsibility for any damage that may occur on your system(s).


Manually install Windows 7 updates

2016/06/09

Download the updates from Microsoft Update Catalog for the desired architecture, x86 or x64, then run from the command line the following:

cd “your/folder/containing/downloaded/updates”
for %h in (*.msu) do start /wait wusa “%cd%\%h” /quiet /norestart

Note 1: Credits go to StackOverflow, IT blogs and alike, sites that helped me with the code when searching for solutions.
Note 2: Be aware that when copy-pasting, the quotes and possibly other characters get messed up by WordPress, you’ll have to replace them.
Note 3: While I may have tested myself most of the bits of code posted here, please be aware that you’re trying the code/scripts at your own risk, and I take no responsibility for any damage that may occur on your system(s).


Useful Chef development prerequisites

2016/03/02

The PowerShell commands below are some main steps needed to configure Chef on a development machine; still, they are not the only needed steps, there are others, like adding the SSH key to GitLab, downloading PEM key from Chef server, maybe Azure configuration, etc.

# Installing packages with Chocolatey
choco install atom -y
choco install git -y
choco install poshgit -y
choco install chefdk -y
choco install virtualbox -y
choco install virtualbox.extensionpack -y
choco install vagrant -y
choco install vagrant-winrm-config -y

# Restart required after packages installation
Restart-Computer

# Install gems for Pester and chef-vault, if used for tests
chef gem install kitchen-pester
chef gem install chef-vault-testfixtures

# Environment variables configuration
$home_folder = “d:\home”
$chef_user = “”
$chef_repo = “d:\repos”
$chef_module = “your-chef-module”
if (!(Test-Path $home_folder)) {
  mkdir $home_folder
}
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;C:\Program Files\Git\usr\bin”, “Machine”)
[Environment]::SetEnvironmentVariable(“HOME”, “$home_folder”, “User”)
[Environment]::SetEnvironmentVariable(“CHEF_ORG”, “your-organization”, “User”)
[Environment]::SetEnvironmentVariable(“CHEF_USER”, $chef_user, “User”)
[Environment]::SetEnvironmentVariable(“Path”, $env:Path + “;C:\HashiCorp\Vagrant\bin”, “Machine”)
[Environment]::SetEnvironmentVariable(“VAGRANT_HOME”, “$env:HOME\vagrant.d”, “User”)

# Git configuration
git config –global user.name “”
git config –global user.email “”
git config –global push.default simple

# SSH key generation
ssh-keygen -t rsa -b 4096 -C “”

# Handle Git repository locally
cd $chef_repo
git clone git@gitlab.com:cookbooks-parent-folder/$chef_module.git
cd $chef_module
git submodule update –init –remote

# Handle knife settings
Copy-Item “$chef_repo\$chef_module\chef\.chef\knife.rb” “$env:HOME\.chef\knife.rb”

# Install vagrant plugin for WinRM
vagrant plugin install vagrant-winrm


Check for dangerous certificates on Windows

2016/01/27

Download sigcheck application from Windows Sysinternals and then execute the command

sigcheck -tv

under a command prompt or PowerShell window

More info on How-To Gheek .


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: