如何使用参数从远程在sitecore中执行powershell脚本

How to execute powershell script in sitecore from remote with parameter

我有一个关于 Sitecore Powershell Extensions 的奇怪案例。我创建了一个基于 .net 的 Cmdlet,我可以在使用带有所需参数的导入命令后执行它。 现在我想通过在外部系统中安排它来自动化这个 cmdlet,因为我不信任 Sitecore Scheduler。为此,我在 Sitecore 中使用 ISE 编写了一个新函数并保存为 'Powershell Script' 项。 以下是代码:

    function Execute-Auto-Site-Backup {
    <#
        .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")
            #>
    [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
}

现在我想从远程调用这个函数,我尝试使用 Powershell RemoteAutomation.asmx & PowerShellWebService.asmx 但没有用,而且关于它的文档也很少。 例如,我尝试编写 windows PS 文件,我可以在作业服务器或任何其他调度程序中安排它,因为它们中的大多数都支持 powershell 脚本如下:

    $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

我也试过下面的方法,但没用

    $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

哪里不对请指点..

您是否尝试过应用 this post on my blog?

中的解决方案

它应该允许您在服务器上执行任意脚本块。

从那时起,我们发布了一个 SPE Remoting 模块,您可以在本地计算机上使用该模块与 Sitecore 的远程实例进行交互。

在此处阅读更多相关信息: https://sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/content/remoting.html