Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Tuesday, October 28, 2014

Powershell training/basics

·         Always run Powershell as Administrator

·         Make sure Add-PSSnapin "Microsoft.SharePoint.Powershell" is added
If this reference is not added you will get not recognized as cmdlet,function…. Error






This can be resolved by adding Add-PSSnapin "Microsoft.SharePoint.Powershell"

    

·         Commenting,writing and in .ps1 files

Example : I have a Powershell.ps1

--Start of Script---

#Test.ps1 This is will add comment to Test powershell script

#Add
Add-PSSnapin "Microsoft.SharePoint.Powershell"

#This will write to output window
Write-Host This is Test powershell script

#Writing in different colours
Write-Host -f Red I am Red
Write-Host -f Yellow I am Red

#This will make sure the output does not disappear
powershell –noexit

--End of Script--


You can copy paste the script in txt file and save it as Powershell.ps1
On running this the output should look like this





Note : Many times the output window disappears and we fail to read the output, add the following command to retain the output window
powershell –noexit

·         Calling a function

The difference in .Net or other techniques of calling function is that in Powershell you first need to define the function and then you can make a call to function
Also it does not need circular bracket to call the function just the name of function(See example below)

--Start of Script---

#Test.ps1 This is will add comment to Test powershell script

#Add
Add-PSSnapin "Microsoft.SharePoint.Powershell"

function MySolution()
{
                Write-Host -f Yellow "I am in MySolution function"
                #Do something
}


#This is how you call a function
MySolution

#This will make sure the output does not disappear
powershell –noexit

--End of Script--

You can copy paste the script in txt file and save it as Powershell.ps1
On running this the output should look like this


  
·         Accepting inputs and doing something


--Start of Script---

#Test.ps1 This is will add comment to Test powershell script

param (
       
      [string]$action = "$(Read-Host ' Please specify the operation which you want to carry - [Add] [Retract] [Deploy] [Remove]')"
)

#Add
Add-PSSnapin "Microsoft.SharePoint.Powershell"

function AcceptInput()
{
if ($action -eq "Add")  {
               
Write-Host -f Yellow Do something if condition matches       

}
}


AcceptInput

#This will make sure the output does not disappear
powershell –noexit

--End of Script--

You can copy paste the script in txt file and save it as Powershell.ps1
On running this the output should look like this


   
Keep watching this space for more advanced scripts,would publish it soon


Wednesday, October 1, 2014

Powershell output disappears

Add powershell -noexit at the end of your code. 


Example :

your script
powershell-noexit  

Add this command at the end of the your script,this will display the output window as long as you don't close it manually :P


Reference :

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\M icrosoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

Resolution for this :

* Run: regedt32
* Goto HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell
* Right-Click > Permissions
* Select, and add your account, grant it "Full Control" privileges

It helped me out, being "alternative" admin, on my machine, which was prepped by an operations 
admin

Reference : 


Sunday, May 18, 2014

Taking back up of site collection in sharepoint using power shell


Run SharePoint 2010 Management Shell as Administrator
Enter the command in following format : 
Backup-SPSite http://server_name/sites/site_name -Path C:\Backup\site_name.bak
Example : 
Backup-SPSite http://inpunects01:7676/sites/ITServices -Path C:\Backup\ITServices.bak
You can name your bak file as per your wish.I have named it ITServices.bak
Dont be worried if you go to this path and find a small bak file,keep refreshing this folder and you can see that the bak file size keeps on increasing as long itdoes not take complete back up.
I would suggest,run this command and go grab yourself a coffee,when your back your .bak file is Ready ! 

References : http://technet.microsoft.com/en-us/library/ff607901(v=office.15).aspx