Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Tuesday, December 13, 2016

Setting ACLs on Website directories using PowerShell

One of the extremely common tasks I have to perform whenever I set up a website or virtual directory/application in IIS is that I have to also set up folder/directory permissions on the newly created directory as well.

This can be time consuming and error prone, especially if you have to set up numerous directories either on a single web server or multiple web servers.

That is why I heavily rely on PowerShell to help me accomplish this task!

For a primer on just how to accomplish this using PowerShell, you can check out this article: http://www.tomsitpro.com/articles/powershell-manage-file-system-acl,2-837.html

Of course, my needs are targeted towards IIS website directories in particular, so I have modified the original script to suit my needs.

You can check out my version of the PowerShell script:


Tuesday, November 8, 2016

Get PowerShell scripts from PowerShell Gallery

For those of you scripting with PowerShell, in the past it has typically been very difficult to find PowerShell scripts that you can use for your own purposes.  You would usually have to scavenge and scour the Internet for numerous blog posts and discussion forums before perhaps finding the PowerShell script that you can actually use!

Well, Microsoft has decided to reduce the pain of searching for PowerShell scripts in the future by introducing PowerShell Gallery: https://www.powershellgallery.com/

From within PowerShell, just like .NET developers use NuGet today, PowerShell allows you to get PowerShell scripts from the PowerShell gallery.

You can browse the PowerShell scripts in the PowerShell gallery here: https://www.powershellgallery.com/items

Once you have found the appropriate PowerShell script, cmdlet etc. that you want to use, you simply either run Install-Module or Install-Script to install your respective PowerShell Module or PowerShell script!  For more information on using the PowerShell Gallery, you can check out this article here: https://msdn.microsoft.com/powershell/gallery/psgallery/psgallery_gettingstarted 

Tuesday, September 27, 2016

Installing Telnet Client from PowerShell

If you need to install the Telnet Client, you can either install it from using the Server Manager GUI or you can install it directly from PowerShell!

I, personally prefer PowerShell since it is much quicker than going through the wizards in Server Manager.

If you want to install Telnet Client from ANY client OS or server OS, you can run the following command:


However, if you wish to install PowerShell on a server OS, you can run the following command:

Thursday, August 11, 2016

Setting up a Private Extension Gallery

If you use Visual Studio Extensions extensively, then you are probably very familiar with the Visual Studio Gallery, but what if you want to set up a Private Extension Gallery just for use with your internal development team?

Well, there is a solution for that as well!

If you read this article from MSDN, you will think that you need to set up your own Atom feed in order to accomplish this: https://msdn.microsoft.com/en-us/library/hh266746.aspx

https://msdn.microsoft.com/en-us/library/hh266717.aspx

Moreover, if you read this article, you will think that the only options you have available to you are an Atom Feed or SharePoint: https://msdn.microsoft.com/en-us/library/hh266735.aspx

But, thanks to CodePlex, you can download an MVC Web Application which provides this support for you!  http://inmetavsgallery.codeplex.com/

You will need to install WebDeploy so that you can deploy this Web Application to your IIS site.  But, you may find that it does not work even after installing the Web Application into IIS!  This is because of a registry key that needs to be imported before Visual Studio will recognize the WCF Service for hosting the Extensions as documented here: http://blog.ehn.nu/2013/12/inmeta-visual-studio-extension-gallery-version-2-0/

The key value for this is the "VSGallery" value for the Protocol Registry Subkey.

Unfortunately, the registry key that is provided on the CodePlex site is a bit outdated and does not work for later versions of Visual Studio such as Visual Studio 2015.

I have written a convenient PowerShell script to handle newer versions of Visual Studio:


If you prefer to add the Private Gallery directly through Visual Studio, you can do that as well as follows:



You will then end up with something like this in the Registry:



Now once you have done that, you simply have to run the following PowerShell script to update the Protocol Registry Subkey so that it is updated to "VSGallery" and provide the appropriate matching GUID:
Alternatively, if you are only planning to have a single Private Extension Gallery, you can use this script to dynamically retrieve it and update the Protocol for you:

Friday, July 15, 2016

Performing database deployments using AliaSQL

Most continuous integration build processes have a need to perform database deployments over the course of development, which is why a tool such as AliaSQL arose to fulfill that need!

You can find out about AliaSQL from here: https://github.com/ClearMeasure/AliaSQL

AliaSQL is available for download directly as an executable or can be installed via a NuGet package.

Once you download the tool, you will probably want to learn how to use it, so you should check out the "Getting Started" guide here: https://github.com/ClearMeasure/AliaSQL/wiki/Getting-started

If you want to look at a demo project, you can find that here: https://github.com/ericdc1/AliaSQL-Demo/tree/master/Source/Database.Demo/scripts

While I was working with AliaSQL, however, I found a problem in the samples that were provided in the Getting Started Guide:

AliaSQL.exe Update .\sqlexpress Demo ./scripts

First of all, the example assumes that you have SQL Server Express installed.  Fortunately, the name of the database server can just as easily be substituted as follows:

AliaSQL.exe Update SQL2014Svr Demo ./scripts

But what about remote SQL Server instances?  What do you do then?  Thankfully, a solution for that is also available:

AliaSQL.exe Update SQL2014Svr Demo ./scripts mysqluser mysqlpassword

Lastly, I could never get this command to work!!  So what was wrong?  Well, even though the AliaSQL.exe file is placed in the root of the scripts directory, the path that is used in the command assumes that AliaSQL resides OUTSIDE of the scripts directory!!  This was fixed by using the following command instead:

AliaSQL.exe Update SQL2014Svr Demo . mysqluser mysqlpassword

Notice that I no longer reference the relative path to the ./scripts directory and simply assume that the AliaSQL.exe exists at the root of the scripts directory (as follows the pattern in the demo and the KickStarter project).

That was all that was needed for working with AliaSQL to do my database deployments!

For your reference, I have also provided a sample PowerShell script on doing database deployments using AliaSQL:


Tuesday, July 12, 2016

Determining which .NET Framework versions are installed using PowerShell

If you want to determine which versions of the .NET Framework are installed on your machine, as of .NET v. 4.5, this is not so easy as in earlier releases because there are multiple iterations of this framework and they all reside in the C:\Windows\Microsoft.NET\Framework64\v4.0.30319 folder!

If you look for a solution on Microsoft's website, you will find this article: https://msdn.microsoft.com/en-us/library/hh925568

Unfortunately, it recommends writing a .NET application to determine the version of the .NET Framework installed on your machine.

But what if you want to use a PowerShell script instead?

Thankfully, one user posted a solution on StackOverflow: http://stackoverflow.com/questions/3487265/powershell-script-to-return-versions-of-net-framework-on-a-machine

You can see the resultant script here:

Monday, July 11, 2016

Copying files and folders to a remote folder using PowerShell

I have been switching many of scripts from using command-line operations over to PowerShell in recent weeks and one of the operations I had been doing earlier was using the "net use" command to map drives to remote shares on servers before copying them over using Robocopy.

However, I did not really need to use the "net use" command since it was just part of a background process and the drive letter could be deleted immediately after the copy operation was completed so I began searching for PowerShell alternatives.

I found just such an alternative using the "New-PSDrive" command:

https://technet.microsoft.com/en-us/library/hh849829.aspx

https://technet.microsoft.com/en-us/library/ee176915.aspx

Once I understood exactly how to use this PowerShell cmdlet, I was able to compose the following PowerShell script:

Saturday, July 2, 2016

What's new in PowerShell v. 5.0

I have been reading articles about PowerShell v. 5.0 for quite some time, but it has been in a "Preview" period for quite some time.  Of course, I lost track of monitoring the release since I continued to do most of my development on Windows Server 2012 R2 which shipped with PowerShell v. 4.0.

Well, I was reminded of PowerShell v. 5.0 when I remoted into a recently built Windows Azure machine and discovered that PowerShell v. 5.0 was installed.

You can download Windows Management Framework v. 5.0 (PowerShell v. 5.0) from here which was re-released in February of this year: https://www.microsoft.com/en-us/download/details.aspx?id=50395

You can read more about the new features in PowerShell v. 5.0 here: https://blogs.technet.microsoft.com/keithmayer/2014/04/16/whats-new-in-powershell-getting-started-with-oneget-in-one-line-with-windows-powershell-5-0/

There is also a video which reviews the features of PowerShell v. 5.0: https://mva.microsoft.com/en-us/training-courses/whats-new-in-powershell-v5-16434?l=Oq4Os59VC_2506218965

The most exciting features of PowerShell v. 5.0 are DSC (Desired State Configuration) and OneGet.  I am excited to begin installing PowerShell v. 5.0 on all of my development machines and get started using all of the new features and capabilities available with PowerShell v. 5.0!

Check the currently installed version of PowerShell

When I administer servers, I often have a need to determine what version of PowerShell is installed on the server so that I can figure out which of my PowerShell scripts are supported on that particular server platform.

Microsoft has thankfully made this task very simple through the use of the $PSVersionTable.$PSVersion PowerShell command.

I created a small, simple little script which will output the currently installed version of PowerShell:

Creating Web Applications in IIS using PowerShell

I am a big fan of PowerShell and have been looking for ways to script out common IIS Administration tasks using PowerShell and there have been a number of methods provided in the past that made it reasonably difficult to create Web Applications in IIS using PowerShell.

However, I came across an article recently that makes this task a piece of cake!  https://technet.microsoft.com/en-us/library/hh867877(v=wps.630).aspx

This resulted in me creating the following PowerShell script:



This worked like a charm! Now I can easily automate nearly any IIS Administration task! Woo hoo!

Friday, July 1, 2016

Using Resource Manager to manage Azure Virtual Machines

As many who have worked with Windows Azure over the last few years, may know that Windows Azure switched from a Classic Service Management model to a Resource Manager model.

Unfortunately, there are still a large number of sample scripts available on the web that refer to the Classic Service Management model, so I needed to start finding out how to work with the new Azure Resource Manager model from my Windows PowerShell scripts.

Thankfully, I finally came across this Microsoft Azure article which describes some of the commands needed to work with Azure Resource Manager and virtual machines:

https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-ps-manage/

https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-ps-create/

Naturally, the piece that was missing from this article was how to put all the pieces together end-to-end so that I could log into Windows Azure and then manage my Azure Virtual Machines!

So, after going through some additional articles, I was finally able to piece together this PowerShell script:


Thursday, June 30, 2016

Getting the Publish Settings file for Windows Azure

If you are connecting to Windows Azure with PowerShell, then you can either set up an account in Azure Active Directory in order to be able to log into your Azure account as "Work or school account" or you can revert to using a Publish Settings file!

While this was relatively easy to find in the Classic Azure Portal, this is not so easy to find in the new Azure Portal.

Fortunately, you can still download the Publish Settings file using PowerShell!

The PowerShell script will ultimately send you to this Url to download your Publish Settings file once you log into the system: https://manage.windowsazure.com/publishsettings/index?client=powershell

So, if you are already logged into your Azure account in your web browser, you may be able to download the Publish Settings file directly.

If you still want to know how to do this through PowerShell, here is the PowerShell script for your review:


Wednesday, June 29, 2016

Excluding files using Robocopy and PowerShell

When I want to do web deployments as part of my continuous integration build process, I often need to exclude certain files such as the Web.config from being deployed to my staging or QA environments.

Therefore, I use Robocopy and PowerShell to accomplish this.

Using this simple little PowerShell script, you can use Robocopy to exclude files from your deployments as well:

Thursday, June 23, 2016

Publishing a Web Application using psake

I have long used MSBuild scripts for setting up my continuous integration builds, but I have started migrating some of my MSBuild scripts over to psake to make the code easier to test and maintain.

One of the problems I struggled with was setting up publishing for a Web Application.  Apparently, this is a bane for many developers or DevOps team members, so I was able to find a StackOverflow article which described a partial solution for my needs: http://stackoverflow.com/questions/4768190/how-to-publish-web-site-using-psake

Of course, this particular snippet did not exactly work for my needs, so I tweaked it to more closely suit my needs as follows:

Of course, you will need to set up a Publishing Profile for your web application, in order for this particular script to work, but once that has been set up, you simply pass the publishUrl property in order to alter the publishing output path!

That is all there is to it!

Friday, April 8, 2016

Using the Copy-Item Cmdlet to exclude files

I recently began switching over some of my command line tasks over to their PowerShell equivalents.

Of course, the Copy-Item cmdlet is the PowerShell replacement to xcopy or RoboCopy.  https://technet.microsoft.com/en-us/library/hh849793.aspx

Of course, the TechNet article provides the available switches and numerous examples, but does not provide a specific example for EXCLUDING FILES!!

When I had previously used robocopy, I had used the /XF switch to exclude files, however, I could not achieve the same result when using this PowerShell command:


$ExclusionFiles = @("packages.config", "connectionStrings.config")

    #Copy over the items
    Copy-Item -Path $SourcePath -Destination $DestinationPath -Exclude $ExclusionFiles -Recurse -Force

Well, as it turns out, my $SourcePath variable needed to append a wildcard character to the end of the path in order for the Exclude switch to properly work as follows:


$ExclusionFiles = @("packages.config", "connectionStrings.config")

    #Copy over the items
    Copy-Item -Path "$SourcePath\*" -Destination $DestinationPath -Exclude $ExclusionFiles -Recurse -Force

That was all that was needed to get my PowerShell script to work in the same way as RoboCopy!!

Tuesday, April 5, 2016

WRN: Assembly binding logging is turned OFF.

I recently was working on troubleshooting an ASP.NET MVC Web Application when I suddenly encountered this error message:


WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].


After doing some digging, I wanted a convenient method of enabling Assembly Logging that would not require me to manually edit the registry.

Of course, my standard go-to for automating tasks is PowerShell!

Therefore, I came up with this PowerShell script to enable Assembly Logging as follows:


$FusionLogDirectory = "C:\FusionLog"

Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name ForceLog         -Value 1               -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogFailures      -Value 1               -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogResourceBinds -Value 1               -Type DWord
Set-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogPath          -Value $FusionLogDirectory -Type String

If at some point in time, you need to disable Assembly Logging, you can do so with this script:


Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name ForceLog
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogFailures
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogResourceBinds
Remove-ItemProperty -Path HKLM:\Software\Microsoft\Fusion -Name LogPath

Friday, April 1, 2016

psake and MSBuild - Better together

I was recently introduced to an alternative method of building projects using a tool called psake.

This is basically a project which leverages PowerShell to do the heavy lifting of tasks that would normally require custom .NET tasks or extensions to MSBuild to accomplish normally.

https://github.com/psake/psake

The documentation for psake can be found here:
http://psake.readthedocs.org/en/latest/

Psake is also conveniently available as a NuGet package: https://www.nuget.org/packages/psake

For a quick tutorial on getting started with psake, you can check out this blog post: https://www.pluralsight.com/blog/software-development/psake-better-build-system-net

Since psake is built on PowerShell which itself is built on the .NET Framework, it opens up a lot of possibilities to accomplish things which are not readily built into MSBuild.

Of course, now that MSBuild is open source, the number of built-in MSBuild tasks will probably grow over time, but psake will be able to easily fill those gaps in the interim!!

This app can't be activated by the Built-In Administrator

I recently started playing around with Windows Server 2016 to begin familiarizing myself with the platform when I suddenly encountered this error message:


I had never seen this error message before, but figured it might be caused by User Account Control so I proceed to the UAC settings to turn off UAC:





However, even after changing these UAC settings, I encountered yet another related error message:




Well, this seemed to indicate a much more involved solution, so a quick search on the Internet returned this article:



UAC Group Policy Settings and Registry Key Settings


Of course, since I absolutely LOVE PowerShell, I decided to write a script that would solve this issue for me!


Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name FilterAdministratorToken -Value 1

That was all that was necessary to get my system working as expected!!  Pretty simple, huh??

Wednesday, March 9, 2016

The source files could not be downloaded REVISITED

I was recently attempting to install .NET Framework v. 3.5 on my Windows Server 2012 R2 server, when I got this dreaded error message:


Well, I had encountered this error message numerous times before and therefore followed my instructions from a previous article: http://samirvaidya.blogspot.com/2015/03/the-source-files-could-not-be-downloaded.html

Unfortunately, this time it DID NOT WORK!!

Well, after a tremendous amount of Internet-based research, I discovered numerous complaints about a series of Windows Hotfixes/Updates which may have caused/introduced this problem.

I also found this MSDN article which seems to indicate that this issue is caused by security update 3005628:  https://msdn.microsoft.com/en-us/library/hh506443.aspx?f=255&MSPPError=-2147217396

https://support.microsoft.com/en-us/kb/3005628

So, naturally, I decided to patch my OS to the latest set of Windows Updates and run the command once again:


This time the same command DID WORK!

Therefore, if you have recently patched your OS and you are also encountering this problem, verify that your Windows Server 2012 R2 OS installation is fully patched!  This could be the root cause of all of your woes!!

Friday, February 12, 2016

Updating IIS SSL Certificate Bindings using PowerShell

I recently had a requirement to use PowerShell to automatically update the SSL Certificate bindings in IIS so I started hunting around for scripts that would help me accomplish this.

Initially, I encountered this script which provided some insight on how to accomplish this:  http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-in

Unfortunately, this article was quite old and outdated and relied on a PowerShell Snap-In which I could not easily determine how to load.

Thankfully, I then came across this much, much NEWER article which is much, much more helpful in determining exactly how to accomplish this task: https://blogs.technet.microsoft.com/heyscriptingguy/2015/03/01/weekend-scripter-use-powershell-to-update-ssl-bindings/

I ended up using this slightly modified version of that PowerShell script to achieve my goals:


[CmdletBinding()]
Param (
 [Parameter(Mandatory = $True, HelpMessage = "Please enter the name of the Web Site")]
 [string]$WebsiteName = "Default Web Site",
 [Parameter(Mandatory = $True, HelpMessage = "Please specify the SSL Port Number")]
 [string]$SSLPort = "443",
 [Parameter(Mandatory = $True, HelpMessage = "Please enter the SSL Certificate Common Name such as *.microsoft.com")]
 [string]$SSLCertSubject
)

Import-Module WebAdministration

Import-Module PKI


function Get-CertificateThumbprint
{
 Param ([string]$CertificateSubject)
 
 $CertThumbprint = (Get-ChildItem -Path cert:\LocalMachine\My -Recurse | Where-Object { $_.Subject -like "CN=$CertificateSubject*" } | Select-Object Thumbprint).Thumbprint
 
 return $CertThumbprint
}


$thumbPrint = Get-CertificateThumbprint -CertificateSubject $SSLCertSubject
$IPAddress = "0.0.0.0"

Clear-Host

New-WebBinding -Name $WebsiteName -IPAddress "*" -Port $SSLPort -Protocol "https"

Get-Item -Path "cert:\LocalMachine\My\$thumbPrint" | New-Item -Path "IIS:\SSLBindings\$IPAddress!$SSLPort"