About Me

My photo
a Dynamic and Energetic guy.....

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



Tuesday, May 6, 2014

Get Data from a Forum / Discussion Board in SharePoint

======================FORUM, TITLE column==============

SPQuery query = new SPQuery();
query.Query = string.Concat(
    "<Where>",
        "<Eq>",
            "<FieldRef Name='IsRootPost'/>",
            "<Value Type='Number'>1</Value>",
        "</Eq>",
    "</Where>",
    "<OrderBy>",
        "<FieldRef Name='DiscussionTitle' Ascending='TRUE' />",
    "</OrderBy>");
query.ViewFields = "<FieldRef Name='DiscussionTitle' />";
query.ViewFieldsOnly = true;

SPListItemCollection _items = [MyList].GetItems(query);'

My Masters