Powershell scripting 08-Dec-19

Windows-Features

  • Windows Features add inbuilt optional functionality to Windows Servers/Desktops Preview

Preview

  • From Powershell you can do the same thing by using a cmdlet

    • Windows10 – Enable-WindowsOptionalFeature
    • Windows Server – Install-WindowsFeature
  • Lets Execute the following command

Get-Command *-WindowsOptionalFeature
Get-Help Get-WindowsOptionalFeature -Online
Get-WindowsOptionalFeature -Online | More
  • Lets Install IIS Server on Windows 10 Clients
Enable-WindowsOptionalFeature -Online -FeatureName IIS-Webserver -All
  • Write a Powershell script which displays all the features available for installation and Asks for user input to install a windows feature. Script name is InstallFeature.PS1
InstallFeature.ps1
0 -> <featurename>
1 -> <featurename>
..
...

Enter the number of the feature which you want to install?

  • Solution is
$disabledFeatures = Get-WindowsOptionalFeature -Online | where State -NE 'Enabled'| Select FeatureName
for ($index = 0; $index -lt $disabledFeatures.Length; $index++) {
    $currentFeature = $disabledFeatures[$index]
    Write-Output "$index  => $currentFeature"
}
$choice = Read-Host -Prompt "Enter Your Choice"
$choiceinint = [convert]::ToInt32($choice)
Enable-WindowsOptionalFeature -Online -FeatureName $disabledFeatures[$choiceinint]


  • Write a script to uninstall the installed features
$installedfeatures = Get-WindowsOptionalFeature -Online | where State -EQ 'Enabled' | Select FeatureName

for ($index = 0; $index -lt $installedfeatures.Count; $index++) {
    $currentFeature = $installedfeatures[$index]
    Write-Output "$index  => $currentFeature"
}

$choice = Read-Host -Prompt "Enter the choice for uninstall. If you want to quit enter y"

if ($choice -ne 'y') {
    $choiceinint = [convert]::ToInt32($choice)
    Disable-WindowsOptionalFeature -FeatureName $installedfeatures[$choiceinint]
}

Powershell Modules

  • Modules are collection of cmd-lets and other powershell utilities
  • Experimenting:
    • Install a module for chocolatey installations and Write a script in powershell to search chocolatey for available softwares.
    • Install any module from powershell gallery
    Install-Module -Name chocolatey
    Get-Command -Module chocolatey
    
  • Exercise -1 : Use Chocolatey module to install tools required for devops
$mandatorytools = ('git', 'vscode')
$optionaltools = ('chefdk', 'packer', 'terraform')

foreach ($tool in $mandatorytools) {
    Write-Output 'Installing $tool'
    Install-ChocolateyPackage -Name $tool -AcceptLicense
    
}

foreach ($tool in $optionaltools){
    $choice = Read-Host -Prompt "Do you want to install $tool. Enter y to install, any other key to skip"
    
    if ($choice -eq 'y') {
        Write-Output "Installing $tool"
        Install-ChocolateyPackage -Name $tool -AcceptLicense
    }
}
  • Exercise-2: Write a Powershell script without using modules to install tools required for devops
$mandatorytools = ('git', 'vscode')
$optionaltools = ('chefdk', 'packer', 'terraform', 'winmerge')

foreach ($tool in $mandatorytools) {
    Write-Output "Installing $tool"
    Start-Process -NoNewWindow -FilePath 'choco.exe' -ArgumentList "install $tool -y"
}

foreach ($tool in $optionaltools){
    $choice = Read-Host -Prompt "Do you want to install $tool. Enter y to install, any other key to skip"
    
    if ($choice -eq 'y') {
        Write-Output "Installing $tool"
        Start-Process -NoNewWindow -FilePath 'choco.exe' -ArgumentList "install $tool -y"
    }
}

  • Write a powershell script called installchocopackages.ps1 where the user executes the installtion by using the following command
./installchocopackages.ps1 -packagenames "git,vscode"
or
use string[] in the parameter and try the script.

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube