Posts

Showing posts from November, 2019

A modern way to keep configuration in file

In Windows environment, we are so used to the INI file or configuration in XML format. You may consider a modern way to keep configuration in a file which uses JSON format. In Powershell,handling JSON format is quite easy ConvertTo-Json This method converts in object into JSON format. ConvertFrom-Json This method converts the JSON text to an object. The next recipe is to call Set-Content and Get-Content to update file and load from file respectively. We develop a simple configuration Powershell which handles the maintenance to the config file. Let's call this file config-my.ps1. param (     # list, add, set, delete     [string]$axn,     # the name of the setting to be updated.     [string]$name,     # new value     [string]$value ) # validate user param if ($axn.Length -eq 0) {     # default param     $axn = "list" } elseif (($axn -eq "add") -or ($axn ...

Compress and decompress file

To compress and decompress file in Powershell is quite straight forward. #compress "test" folderCompress-Archive -Path "D:\temp3\test" -DestinationPath "D:\temp3\myFirstZipFile.zip" #decompress the file to "testFolder" folder. Expand-Archive -Path "D:\temp3\myFirstZipFile.zip" -DestinationPath "d:\temp3\testFolder" # to decompress the file back to the Expand-Archive -Path "D:\temp3\myFirstZipFile.zip" -DestinationPath "d:\temp3\"

Copying files with Powershell + Robocopy

Copying files using Copy-Item command can be slow. The fastest copying utility in Windows is Robocopy. Below is the script that copy all files in "D:\temp3\test" to two destinations listed in "dst-dir.csv". Here is the script for "copy-file-to-dir.ps1" ## sample values # #$src = 'D:\temp3\test' #$dst_csv = "$PSScriptRoot\dst-dir.csv" # param (     [string]$src = $(Read-Host("Source directory")),     [string]$dst_csv = $(Read-Host("Destination CSV file name (blank for default)")) ) if ($src.Length -eq 0) {     Write-Host "==> the source directory has not been specified. Exit."     return } if (!(Test-Path $src)) {     Write-Host "==> the source directory does not exist. Exit."     return } if ($dst_csv.Length -eq 0) {     #set default dir     $dst_csv = "$PSScriptRoot\dst-dir.csv" } if (!(Test-Path $dst_csv)) {    ...

Setting up IIS server

Someone invented this script and I'm just repeating the script here. Reference:         https://weblog.west-wind.com/posts/2017/may/25/automating-iis-feature-installation-with-powershell The script to activating IIS and the necessary components for ASP.NET # To list all Windows Features: dism /online /Get-Features # Get-WindowsOptionalFeature -Online # LIST All IIS FEATURES: # Get-WindowsOptionalFeature -Online | where FeatureName -like 'IIS-*' Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer Enable-WindowsOptionalFeature -Online -FeatureName IIS-CommonHttpFeatures Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpErrors Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpRedirect Enable-WindowsOptionalFeature -Online -FeatureName IIS-ApplicationDevelopment Enable-WindowsOptionalFeature -online -FeatureName NetFx4Extended-ASPNET45 Enable...

Installing MSSQL server with standard setup

Sometimes when you are installing the MSSQL Express, you will found out that you missed out changing the collation, change the instance name and other standard settings for the programs that you are distributing. To avoid patching the SQL server settings, it's better to use a script and all team members to follow. Here is the script that I have came out to setup the MSSQL. The most frequent updating the setting is the collation ... we forgot to change it to our standard before hitting Continue button. $sql_setup_file = "F:\my-setupFiles\sql2012 express\SQLEXPRWT_x64_ENU.exe " $db_dir = "F:\my_database" $bak_dir = "F:\my_database_backup" $sa_pwd = "1234567890" $param = "/QUIET /IACCEPTSQLSERVERLICENSETERMS /ACTION=""install"" /FEATURES=SQL,Tools /INSTANCENAME=sqlexpress /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AS /ERRORREPORTING=0 /SQLBACKUPDIR=""$bak_dir"" /SQLUSERDBDIR=""$db_dir...

Enabling DNS service in Windows server

To reduce the mouse clicking on the user interface and avoiding activating the wrong service, we will use Powershell command. With Powershell, it reduces the need of training the new support guy on all the details on where to click the feature. Here is the command to enable the DNS service in Windows server.    Install-WindowsFeature -Name DNS -IncludeAllSubFeature -IncludeManagementTools

Changing the execution policy

By default, running any PowerShell script file has been forbidden and the following error will appear on the screen:     File D:\temp5\test.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.     + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException     + FullyQualifiedErrorId : UnauthorizedAccess To view the current settings, run the following command     Get-ExecutionPolicy -List To be able to execute .ps1 file using Powershell console or ISE, run the following command:     Set-ExecutionPolicy RemoteSigned If you want to learn more about RemoteSigned, you have to do an Internet search.     https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpoli...