Tuesday, August 28, 2012

Changing PowerShell script execution settings

If you administer servers on a domain and use Powershell, you might come across this error:

File C:\<your file>.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

Step 1. View the current execution settings

PS C:\> Get-ExecutionPolicy <hit Enter>

It may return "Restricted" which will prevent you from executing any scripts at all...even those written by you.

Step 2. Set the execution setting

PS C:\> Set-ExecutionPolicy RemoteSigned

This will allow you to run only signed scripts and those written by you...provided you have the proper permissions to do so.


Tuesday, August 14, 2012

How To Restart Computers Remotely via PowerShell

It is a fact that Windows administrators periodically need to reboot servers and desktops. Because of this fact, I always was running into scripts that would remotely reboot a group of computers. But in PowerShell, this is now (dangerously) easy and no scripting is required. All we need is the Restart-Computer cmdlet.
You can use the cmdlet to restart your own computer without much hassle:

PS C:\> Restart-Computer

To restart a remote computer, you need to run the command with credentials that have the right privileges to remotely shut down a computer, typically an admin account. Assuming my current credentials were adequate, I could easily reboot the computer SERVER01:

PS C:\> Restart-Computer Server01 -whatif

What if: Performing operation "Restart-Computer" on Target " (Server01)".

although I didn't really; I took advantage of the -WhatIf parameter to verify my command. This is especially helpful if I'm rebooting a bunch of servers:

PS C:\> restart-computer "server01","server02","server03" -whatif

By the way, here's how you could shut down a list of computers:

PS C:\> restart-computer (get-content c:\work\computers.txt)

Because the cmdlet is using WMI objects and methods under the hood you can specify alternate credentials, either a saved credential object or a user name:

PS C:\> restart-computer (get-content c:\work\computers.txt) -credential "mycompany\administrator"

I'll get prompted for the password, but then this credential will be used for every computer in the list.

The Restart-Computer cmdlet will fail, if a logon session is detected. PowerShell will raise an exception. However, you can force a reboot using -- what else? -- the -force parameter. Be aware this will force applications to close with the potential loss of unsaved work.

Another option for rebooting or even logging off is to use the Win32_OperatingSystem WMI Class and the Win32ShutDown method. I recommend using Invoke-WMIMethod because it supports -WhatIf and -Confirm. I found it just as easy to pipe a WMI object from Get-WMIObject to Invoke-WMIMethod:

PS C:\> Get-WmiObject win32_operatingsystem -ComputerName Quark | Invoke-WMIMethod -name Win32Shutdown

The Win32Shutdown method can accept parameters. The default is 0 which means do a simple logoff. But if the user has open files or if the default method fails, you can always resort to a forceful logoff:

PS C:\> Get-WmiObject win32_operatingsystem -ComputerName Quark | Invoke-WMIMethod -name Win32Shutdown -ArgumentList @(4)

You have to specify the value for -ArgumentList as an explicit array. If you look at the MSDN documentation for this method you'll discover other method parameter values, which you can certainly use. But personally many of them are duplicated with cmdlets like Restart-Computer and Stop-Computer; given a choice I prefer to use a cmdlet.

I hope it goes without saying that this is pretty powerful ju-ju so be sure you've tested your scripts and tried out these commands in a non-production environment.

jQuery to override SharePoint OOTB Upload.aspx default for "Add as a new version to existing files" checkbox

A common query from SharePoint users with versioning enabled on document libraries is to default the "Add as a new version to existing files" checkbox on the OOTB upload.aspx page to unchecked. If you google how to uncheck it by default you'll get lots of entries showing you how to simply edit the Upload.aspx file in the \TEMPLATE\LAYOUTS\ folder in the 12/14 hive. This works fine of course if you don't care about tomorrow and having a supported/upgradeable SharePoint installation.

If you do there is a better way as described by Per Jakobsen here.
Using the AdditionalPageHead delegate control to inject javascript to the page through a feature not only gives you a supportable system but allows you to switch the default to checked or not checked by activating or deactivating the feature.

I tried a variation of this on a SharePoint 2007 installation and it worked fine, after an upgrade to 2010 it stopped working however. Having spent five minutes trying to debug the issue I decided it would be swifter to just change it to use jQuery's $(document).ready function rather than solve the specific problem. There seems to be others having weird things going on with _spBodyOnLoadFunctionNames.push in 2010, so this is probably a quicker fix (at least if you've already got jQuery available on the page). If you don't you could add a reference to it in the DefaultUploadOverwriteOff.ascx user control.

The change to Jakobsen's javascript is simple, just replace the function DefaultUploadOverwriteOff() with this:

?
1
2
3
4
5
6
$(document).ready(function() {
    if (document.title == "Upload Document") {
        $("input[id$='OverwriteSingle']").attr("checked",false);
        $("input[id$='OverwriteMultiple']").attr("checked",false);
    }
});

and remove the last line: _spBodyOnLoadFunctionNames.push('DefaultUploadOverwriteOff');

This now works fine on both 2007 and 2010.

Adding and Deploying SharePoint 2010 .WSP Solutions

Every SharePoint specialist is doing this process when deploy SP solution package on production server, so I write this article to keep it as a reference for me and share it with others to help and get feedback/ides for best practices.
In my Scenario, I have 2 WSP files:
  • NewSolutionPack.wsp (to add it as first time)
  • UpdateSolutionPack.wsp (to update an exist WSP file)
  • Target web application (http://sps:1)
clip_image001[7]

The steps of deployment are easy:

1. Add solution to Farm Solutions
2. Deploy your solution to a specific web application or to all web applications
3. In case of exists solution, you will upgrade the solution using one of the following options:
a. Retract and delete the old solution, then add and deploy the new one
b. Or, upgrade the existing solution using upgrade method (recommended).

Steps of deployment using STSADM
1. Add solution to Farm Solutions
a. Open cmd (Star > Run > write cmd in box > click ok)
clip_image002[7]
b. Mont the folder that contain stsadm execution file (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN)
clip_image004[7]
clip_image006[7]
c. Enter the following script
stsadm -o addsolution -filename “C:\WSP Deployment\NewSolutionPack.wsp”
clip_image008[7]
2. Deploy your solution
a. To a specific web application
stsadm -o deploysolution -name “NewSolutionPack.wsp” -url “http://sps:1″ -allowgacdeployment -immediate –allowcaspolicies
clip_image010[7]

Check it from Central Administration > System Settings > Manage Farm Solutions

clip_image012[7]
b. To all web applications
stsadm -o deploysolution -name “NewSolutionPack.wsp” -allcontenturls -allowgacdeployment -immediate –allowcaspolicies

clip_image014[7]
3. Upgrade the solution by Retract and delete the old solution, then add and deploy the new one
a. stsadm -o retractsolution -name “NewSolutionPack.wsp” -url “http://sps:1″ –immediate
if you want to retract the solution from all web applications, you need to use this
stsadm -o retractsolution -name “NewSolutionPack.wsp” -allcontenturls –immediate
b. stsadm -o deletesolution -name “NewSolutionPack.wsp”
c. stsadm -o addsolution -filename “C:\WSP Deployment\NewSolutionPack.wsp”
d. stsadm -o deploysolution -name “NewSolutionPack.wsp” -allcontenturls -allowgacdeployment -immediate –allowcaspolicies
4. Upgrade the solution by using upgrade method
a. Enter upgrade script
stsadm -o upgradesolution -name “NewSolutionPack.wsp” -filename “C:\WSP Deployment\UpdateSolutionPack.wsp” -allowgacdeployment -immediate –allowcaspolicies
clip_image016[7]

Steps of deployment using PowerShell
1. Add solution to Farm Solutions
a. Open PowerShell (SharePoint 2010 Management Shell)
clip_image018[7]
b. Enter the following script:
Add-SPSolution “C:\WSP Deployment\NewSolutionPack.wsp”
clip_image020[7]
2. Deploy your solution
a. To a specific web application
Install-SPSolution -Identity “NewSolutionPack.wsp” -WebApplication “http://sps:1″ -GACDeployment -CASPolicies -Confirm:$false
b. To all web applications
Install-SPSolution -Identity “NewSolutionPack.wsp” -AllWebApplications -GACDeployment -CASPolicies -Confirm:$false
3. Upgrade the solution by Retract and delete the old solution, then add and deploy the new one
a. Uninstall-SPSolution -Identity “NewSolutionPack.wsp” -WebApplication “http://sps:1″ -Confirm:$false
if you want to retract the solution from all web applications, you need to use this
Uninstall-SPSolution -Identity “NewSolutionPack.wsp” -AllWebApplications -Confirm:$false
b. Remove-SPSolution -Identity “NewSolutionPack.wsp” -Confirm:$false
c. Add-SPSolution “C:\WSP Deployment\NewSolutionPack.wsp”
d. Install-SPSolution -Identity “NewSolutionPack.wsp” -WebApplication “http://sps:1″ -GACDeployment -CASPolicies -Confirm:$false
4. Upgrade the solution by using upgrade method:
Update-SPSolution -Identity “NewSolutionPack.wsp” -LiteralPath “C:\WSP Deployment\NewSolutionPack.wsp” -GacDeployment -CASPolicies -Confirm:$false
For more information about mapping from stsadm to PowerShell, read the full MSDN topic: Stsadm to Windows PowerShell mapping (SharePoint Foundation 2010)
I Hope this useful for you,
Thanks.