title: How to execute powershell script in sitecore from remote with parameter
body: <p>I am having a curious case about Sitecore Powershell Extensions. I have created a .net based Cmdlet which I can execute after using import command with required parameters.
  Now I want to automate this cmdlet by scheduling it in external system as I don't trust the Sitecore Scheduler. So for that I write a new function using ISE in Sitecore and saved as 'Powershell Script' item.
  Following is the code:</p>
  
  <pre><code>    function Execute-Auto-Site-Backup {
      &lt;#
          .SYNOPSIS
              Backups the given site path while looking for delta based on last execution time.
  
          .PARAMETER Item
              Item to sync, Usually a Root Item, Need to specify Sitecore Guid of Root Item.
  
          .PARAMETER ItemList
              Specifies the list of item which need to be excluded, It is in pipe separated GUIDs.
  
          .PARAMETER UserName
              Sitecore Username for taret webservice "sitecore\admin"
  
          .PARAMETER Password
              Sitecore Password
  
          .PARAMETER HostURL
              Sitecore instance Host URL
  
          .PARAMETER DBName
              Sitecore target Database name, if not passed default is "master"
  
          .PARAMETER LangName
              Sitecore Source Language name, if not passed default is "en"
  
          .PARAMETER IncludeChildren
              Indicate if need to copy the all children till nth level of indicated root item, if not passed default is false which means it will execute for single item
  
          .PARAMETER BackupMode
              Indicate if need to create versions always or just update the existing latest version, if not passed default is true which means it will create version in the target item
  
          .PARAMETER VersionNumber
              Sitecore Version number of Item to get, Default will be the Latest if not provided. In case of multiple item it will always take latest version.
  
          .PARAMETER LookupMode
              To use History Engine, if not passed default is false which means always look in iterative mode from passed root node.
  
          .PARAMETER DateTime
              If LookupMode is History Engine than this parameter will define since when to look, ideally this is last time the backup job has run. Date Time should be in following format and as per server time zone '1/20/2015 3:30:00 PM'
  
          .EXAMPLE    
              BackupMode as version(non-update) always and LookupMode is single item(non-history)
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","false","true","0","false","")
  
          .EXAMPLE        
              BackupMode as version(non-update) always and LookupMode is iterative(non-history) in childrens
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","true","true","0","false","")
  
          .EXAMPLE
              BackupMode as update(non-version) always and LookupMode is iterative(non-history) in childrens
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","true","false","0","false","")
  
          .EXAMPLE
              BackupMode as update(non-version) always and LookupMode is single item(non-history)
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","false","false","0","false","")
  
  
  
          .EXAMPLE        
              BackupMode as version(non-update) always and LookupMode is history(non-iterative)- most used way and preferred for automate backups
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","","true","0","true","1/20/2015 3:30:00 PM")
  
          .EXAMPLE
              BackupMode as update(non-version) always and LookupMode is history(non-iterative)
  
              [string[]]$paracs=@("{7589EBFF-FB47-41A0-8712-E34623F5518E}","","sitecore\admin","target","http://10.0.0.5/","master","en","","false","0","true","1/20/2015 3:30:00 PM")
              #&gt;
      [CmdletBinding()]
      param(
          [ValidateNotNullOrEmpty()]
          [string]$Item,
  
          [string]$ItemList,
  
          [ValidateNotNullOrEmpty()]
          [string]$UserName,
  
          [ValidateNotNullOrEmpty()]
          [string]$Password,
  
          [ValidateNotNullOrEmpty()]
          [string]$HostURL,
  
          [ValidateNotNullOrEmpty()]
          [string]$DBName,
  
          [ValidateNotNullOrEmpty()]
          [string]$LangName,
  
          [string]$IncludeChildren="false",
  
          [string]$BackupMode="true",
  
          [string]$VersionNumber,
  
          [string]$LookupMode="false",
  
          [string]$DateTime
      )    
      $path=$AppPath+"bin\PG.SharedSitecore.AssemblyTools.CoreSync.dll"
      Import-Module $path -Verbose
      [string[]]$paracs=@($Item,$ItemList,$UserName,$Password,$HostURL,$DBName,$LangName,$IncludeChildren,$BackupMode,$VersionNumber,$LookupMode,$DateTime)
      Get-CoreSyncBackup -args $paracs
  }
  </code></pre>
  
  <p>Now I want to call this function from remote, I tried using Powershell RemoteAutomation.asmx &amp; PowerShellWebService.asmx but not useful and there is very less documentation about same.
  For example i tried to write windows PS file which i can schedule in Job server or any other scheduler as most of them support powershell scripts follows:</p>
  
  <pre><code>    $page=New-WebServiceProxy -Uri "http://audit.brand.com/console/services/RemoteAutomation.asmx"
  $BackupFunction=@"
  Execute-Script "master:/sitecore/system/Modules/PowerShell/Script Library/CoreSync/SyncorBackupFunc/";
  Execute-Auto-Site-Backup "{7589EBFF-FB47-41A0-8712-E34623F5518E}" "" "sitecore\admin" "b" "http://audit.brand.com/" "master" "en" "true" "true" "" "" ""
  "@
  $returnVar=""
  $page.ExecuteScriptBlock("sitecore\admin","b",$BackupFunction,$returnVar)
  $returnvar
  </code></pre>
  
  <p>I also tried following which doesn't worked</p>
  
  <pre><code>    $page=New-WebServiceProxy -Uri "http://audit.brand.com/console/services/RemoteAutomation.asmx"
  $BackupFunction=@"
  master:/sitecore/system/Modules/PowerShell/Script Library/CoreSync/SyncorBackupFunc/
  "@
  $returnVar=""
  $page.ExecuteScript("sitecore\admin","b",$BackupFunction,$returnVar)
  $returnvar
  </code></pre>
  
  <p>Please guide me where I am wrong..</p>