About Me

My photo
a Dynamic and Energetic guy.....
Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Friday, May 9, 2014

Execution of PowerShell Script is disabled on the system

When we RUN PowerShell Scripts, we are getting "PowerShell Script is disabled on the system"

This is mainly because of EXECUTION POLICY.


We can run the command and get the CURRENT security status. It is "Restricted"

So we have to increase security for Scripts.

No, we can't increase Security if we are not running the Command Prompt with elevated privileges.
Yes, Now the Security level is upgraded.

Now we can run POWERSHELL Scripts.





Thursday, May 8, 2014

Delete files using PowerShell based on Created Time

We have to clean our SharePoint backup location after 7 days.
We have decided to create a POWERSHELL script and run it as a WINDOWS TIMER.
It is simple.

The PowerShell Script,

# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = [int]1 # 30 days
foreach ($path in Get-Content "pathList.txt") {
# Write information of what it is about to do
Write-Host "Trying to delete files older than $oldTime days, in the folder $path" -ForegroundColor Green
# deleting the old files
Get-ChildItem $path -Recurse -Include "*.PNG", "*.bak" | WHERE {($_.LastWriteTime  -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force
}



there we have set 3 parameters.
Filepath   = the folder that we needs to clean
DAYS    = deleting files older than this DAYS
Types     = the file types that we need to delete

the "pathList.txt" has all the File paths that we have to delete.

 


We can delete SELECTED file types.


After we run the script, we can see all the FILES TYPES (.png) been deleted if OLDER than specify DAYS.

We can set a windows timer service and execute the .ps1 file



Sunday, August 25, 2013

Update SharePoint 2010 Farm Credentials Using PowerShell


#Input the Managed Account
#If there is only one managed account, the following line could be written as:
#$inputManagedAcct = Get-SPManagedAccount

$inputManagedAcct = Read-Host "Enter managed account as Domain\User" 
#Input the desired new password 
$inputPasswd = Read-Host "Enter new password for managed account" –AsSecureString 
#Change the password for the managed account to the new value 
Set-SPManagedAccount -Identity $inputManagedAcct -NewPassword $inputPasswd



------------------------------------------------------------------------------------------------------------------------------------------------------------       Update after AD Reset    ----------------------------------------------------------------

#Input the Managed Account
#If there is only one managed account, the following line could be written as:
#$inputManagedAcct = Get-SPManagedAccount

$inputManagedAcct = Read-Host "Enter managed account as Domain\User:" 
#Input the Managed Account 
$inputPasswd = Read-Host "Enter password from Active Directory for managed account:" –AsSecureString 
#Change the password in SharePoint for the managed account to the new value 
Set-SPManagedAccount -Identity $inputManagedAcct -ExistingPassword $inputPasswd –UseExistingPassword $true 

Tuesday, January 29, 2013

PowerShell Command Builder

I got a nice tool that can create "Windows Power Shell Commands" dynamically.
Go here


Simple Power shell command generation screen,
"Create -SPSite"


"Add -SPSolution"

Simple...


Sunday, January 27, 2013

Apply MasterPage to all Site Collections Using PowerShell Commands

------------------------ Set Master Page ----------------------------- 
foreach ($site in get-spsite -url http://spsvr*) {
     $web = Get-SPWeb $site
     $web.CustomMasterUrl = "/_catalogs/masterpage/my_v4.master" 
     $web.Update()
}
 
------------------------ Using Other Method --------------------------
 
foreach ($site in Get-SPSite)
{
 foreach ($web in $Site.AllWebs)
 {
  $web.MasterUrl  = "/_catalogs/masterpage/my_v4.master";
  $web.CustomMasterUrl = "/_catalogs/masterpage/my_v4.master";  
  $web.update()
 }
} 
 
---------------------- Set Master Page For Web Application ------------
----------------------------------------------------------------------- 
#Enter a web application, get all the site collections in the web app
Add-PsSnapin Microsoft.SharePoint.PowerShell
Start-SPAssignment -Global

$web = Get-SPWeb 'http://spsvr:1111/'
#$web.CustomMasterUrl = "/_catalogs/masterpage/v4_TG.master"
$web.MasterUrl = "/_catalogs/masterpage/v4_TG.master"
$web.Update()


--------------------- Set Master Page For All Subsites --------------
----------------------------------------------------------------------
#Enter a web application, get all the site collections in the web app
Add-PsSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global
$site = Get-SPSite 'http://spsvr:1111'
$topWeb = Get-SPWeb $site.Url
$site | Get-SPWeb -limit all | ForEach-Object 
{if (($_.WebTemplate -ne "SRCHCEN") -and ($_.WebTemplate -ne "SRCHCENTERLITE") -and ($_.WebTemplate -ne "SRCHCENTERFAST")) {$_.CustomMasterUrl = $topWeb.CustomMasterUrl;$_.AllProperties["__InheritsCustomMasterUrl"] = "True";$_.MasterUrl = $topWeb.MasterUrl;$_.AllProperties["__InheritsMasterUrl"] = "True";$_.AlternateCssUrl = $topWeb.AlternateCssUrl;$_.AllProperties["__InheritsAlternateCssUrl"] = "True";$_.Update();}}
Stop-SPAssignment -Global

My Masters