Tuesday, March 31, 2015

Write Less Code With PowerShell Parameter Validation

You’ll often write a script or function that needs to accept some kind of input. This could be a computer name, a file path or anything like that. You can tell Windows PowerShell to expect these parameters, collect them from the command line, and put their values into variables within your script or function. That makes dealing with input easy and efficient.
You just have to know how to declare your parameters. The simplest means of doing so is the param block:
Param(
  [string]$computerName,
  [string]$filePath
)
You don’t have to break that down into separate lines like I’ve done. It’s legal to run it all together on a single line. I prefer to break it down for easier reading, though. 

That way, my parameters are consistent with what’s already in the shell.
If I put this into a script named Get-Something.ps1, I’d use the parameters like this:
./Get-Something –computerName SERVER1 –filePath C:\Whatever
I could also truncate the parameter names. This lets me type fewer characters and they still work:
./Get-Something –comp SERVER1 –file C:\Whatever
I could even omit the names entirely. Windows PowerShell will automatically and positionally accept values. Here I need to be careful to provide values in the same order in which the parameters are listed in my file:
./Get-Something SERVER1 C:\Whatever
Of course, by using parameter names, the command becomes a bit easier for a person to figure out. I then get the luxury of putting the parameters in any order I want:
./Get-Something –filePath C:\Whatever –computerName SERVER1
Windows PowerShell also provides a more complex way of declaring parameters. This more full-fledged syntax lets you define parameters as mandatory, specify a position (if you don’t do so, then the parameter can only be used by name) and more. This expanded syntax is also legal in both scripts and functions:
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$computerName,
 
   [Parameter(Mandatory=$True)]
   [string]$filePath
)
Again, you can run all that together on a single line, but breaking it down makes it a bit easier to read. I’ve given both of my parameters a [Parameter()] decorator, and defined them both as mandatory.
If someone tries to run my script and forgets one or both of these parameters, the shell will prompt for them automatically. There’s no extra work on my part needed to make that happen. I’ve also defined –computerName as being in the first position, but –filePath needs to be provided by name.
There are some other advantages to using the [CmdletBinding()] directive. For one, it ensures my script or function will have all the Windows PowerShell common parameters, including –Verbose and –Debug. Now, I can use Write-Verbose and Write-Debug within my script or function, and their output will be suppressed automatically.
Run the script or function with –Verbose or –Debug, and Write-Verbose or Write-Debug (respectively) are magically activated. That’s a great way to produce step-by-step progress information (Write-Verbose) or add debugging breakpoints (Write-Debug) in your scripts.
As they’re currently written, both parameters will accept only a single value. Declaring them as [string[]] would let them accept an entire collection of values. You’d then enumerate this using a Foreach loop, so you could work with one value at a time.
Another neat parameter type is [switch]:
Param([switch]$DoSomething)
Now, I can run my script or function with no –DoSomething parameter and internally the $DoSomething variable will be $False. If I run the script with the –DoSomething parameter, $DoSomething gets set to $True. There’s no need to pass a value to the parameter. Windows PowerShell sets it to $True if you simply include it. This is how switch parameters operate, such as the –recurse parameter of Get-ChildItem.
Keep in mind that each parameter is its own entity, and it’s separated from the next parameter by a comma. You’ll notice that in a previous example:
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$computerName,
 
   [Parameter(Mandatory=$True)]
   [string]$filePath
)
There the entire –computerName parameter, including its [Parameter()] decorator, appears before the comma. The comma indicates I’m done explaining the first parameter and I’m ready to move on to the next. Everything associated with –filePath follows the comma. If I needed a third parameter, I’d put another comma:
[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$computerName,
 
   [Parameter(Mandatory=$True)]
   [string]$filePath,

   [switch]$DoSomething
)
All of that is contained within the Param() block. Note that you don’t have to use the [Parameter()] decorator on every parameter. Only use it on the ones where you need to declare something, such as the parameter being mandatory, accepting pipeline input, being in a certain position and so on. Run help about_functions_advanced_parameters in Windows PowerShell for more information on other attributes you can declare that way.
Writing functions and scripts that accept input only via parameters is a best practice. It makes them more self-contained, easier to document and more consistent with the way the rest of the shell works.

Monday, March 23, 2015

How to fix SharePoint 2013 Web Application error “The context has expired and can no longer be used”


Today, I came across this odd error quite weired, after some research I found the folowwing solution.

Here is a quick guide that might help you get rid of it.

If you see this error after opening your SharePoint 2013 site, there is a lack of synchronization between Date and Time settings in your SharePoint 2013 Server and your SharePoint web application.
Sorry, something went wrong. The context has expired and can no longer be used. (Exception from HRESULT: 0x80090317)
Here is how you fix it!
  • Open Central Administration -> Application Management.
  • Locate the relevant Web Application and click on 
  • Web Application General Setting window will open up, notice that theDefault Time Zone is missing.

  • Open Date and Time options on your server and check which time zone is configured. Configure the same time zone in Web Application General Setting.
  • Perform IIS Reset and open your SharePoint 2013 site again.
Hooray! SharePoint site works again!
I have to thank Alex Entrekin who helped me.
Cheers,

Thursday, March 19, 2015

Export & Import Hyper-V Virtual machines with snapshots

Here is a straithforward article about import/export Hyper-V virtual machines with snapshots.
Before I get going I want to get this out of the way:  Export / import in Hyper-V is by no means intuitive or easy to use.  It is definitely something that I hope we can improve in a future release - but for this release it is functional.
So with that behind us, let's start pulling this apart.
If you want to move or copy a virtual machine with Hyper-V, then you will need to use the export / import functionality provided by Hyper-V. 
The first thing you need to do is to pick the virtual machine that you want to copy and / or move, and then select Export...from the action menu / pane.  You will be presented with the following dialog:
export
Today I will be looking at the case where you specify an export path (in my case: "C:\Export") but do not check the option toExport only the virtual machine configuration.
Before going further I need to cover virtual machine names and IDs.  Each Hyper-V virtual machine has one of each of these.

The virtual machine name is what you called the virtual machine.  For today's post I am using a virtual machine with a name of "Test Export VM".  While you are likely to give each of your virtual machines different names - the virtual machine name is not required to be unique.

The virtual machine ID is a GUID that Hyper-V generates automatically for each virtual machine.  This ID is used to uniquely identify one virtual machine from another.  For the most part the virtual machine ID is never displayed in the Hyper-V user interface (with the exception of error messages).  The virtual machine I am using for today's post has a virtual machine ID of "6D59FE56-6D20-4129-9BF3-2457DDB58A9A".

Beyond this, each snapshot that a virtual machine has has its own name and ID.
Hitting Export will result in Hyper-V copying everything that makes up the selected virtual machine into a new folder under the export path you specify.  This new folder will be named after the virtual machine name (in my case: "C:\export\Test Export VM").  Under this new directory will be the following items:
  • The Virtual Machines folder

    • This folder will contain a single .exp file, which will use the virtual machine ID for its name (in my case: "6D59FE56-6D20-4129-9BF3-2457DDB58A9A.exp").  The .exp file is the exported configuration of the virtual machine. 

      There will also be another folder in this folder, which is also named use the virtual machine ID.  If the virtual machine was in a saved state when it was exported this sub-folder will contain two saved state files (a .vsv and a .bin file), otherwise it will be empty.
  • The Virtual Hard Disks folder

    • This folder contains copies of each of the virtual hard disks associated with the virtual machine.  Note that if you have two virtual hard disks with the same name (but different locations) associated with a virtual machine, exporting the virtual machine will fail.
  • The Snapshots folder

    • This folder will contain:
      • A .exp file for each snapshot the virtual machine had (name after the snapshot ID)
      • A folder named after the snapshot ID that contains the saved state files for the snapshot in question.
      • A folder named after the virtual machine ID that will contain the differencing disks used by all of the snapshots associated with the virtual machine (.avhd files).
  • config.xml

    • I will look at this file in more detail another day.  It is not necessary for standard export / import usage.
You can freely move / copy / backup this entire directory structure now.  When you are ready to import the virtual machine you will need to go the the Hyper-V Manager and select Import Virtual Machine... from the action menu / pane.  You will see:
import
Before clicking Import there are three important things to know:
  • You need to specify the folder that was created during export, not the folder that was used for export.  So in my case I need to specify "C:\Export\Test Export VM" instead of "C:\Export".
  • When you import a virtual machine it will be left in its current directory (in my case "C:\Export\Test Export VM") and it will be impossible to move the virtual machine after import.  So make sure that you move the exported virtual machine to your desired location before you import it.
  • Importing a virtual machine deletes the .exp files, which stops you from importing it again.  If you want to use an exported virtual machine as a backup / template that you will import multiple times - you need to make a copy of it before importing it.
After you click Import the file structure of the exported virtual machine will remain roughly the same, with the following exceptions:
  • The .exp files will be deleted and will be replaced with .xml configuration files.
  • The config.xml file will be deleted.
And now the virtual machine will appear under the Hyper-V manager and you will be able to interact with it directly.
Cheers,

Source : http://blogs.msdn.com/b/virtual_pc_guy/archive/2008/08/26/hyper-v-export-import-part-1.aspx