如何使用here-strings正确运行powershell脚本?

时间:2019-07-15 15:12:41

标签: python shell powershell sharepoint escaping

我正在使用Sharepoint服务。我的python脚本运行powershell脚本并下载文件。我有以下Powershell脚本,用于从Sharepoint下载文件:

[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[String]$UserName,
[Parameter(Mandatory=$True,Position=2)]
[String]$Password,
[Parameter(Mandatory=$True, Position=3)]
[String]$FileUrl,
[Parameter(Mandatory=$True, Position=4)]
[String]$DestinationFilePathName
)

try
{
    # Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM.
    Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location)
    Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location)

    # Create a credential object using the username and password supplied.
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, (ConvertTo-SecureString $Password -AsPlainText -Force))
    $client = New-Object System.Net.WebClient 
    $client.Credentials = $Creds
    $client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    Write-Host "Downloading file" $FileUrl "to" $DestinationFilePathName -ForegroundColor Green
    $client.DownloadFile($FileUrl, $DestinationFilePathName)
    $client.Dispose()
}
catch
{
    # Error occurred.
    $_.Exception.Message
    $exitcode = -1
}
exit $exitcode

在python中,powershell脚本由以下代码调用:

...
shell_script = "powershell -ExecutionPolicy Unrestricted -Command \"& %s %s %s %s %s\"" % (self.config['path_shell_download_script'], user_email, self.password, self.config['url_download_from_sp'], self.config['sp_extract_path'])
        if subprocess.call(shell_script, shell=True) != 0:
            raise Exception("Error while retrieving file: %s." % (self.config['url_download_from_sp']))
...

一切正常,此脚本可以正确使用我的凭据。但是,当我尝试使用管理员凭据运行脚本时,它不起作用。管理员密码存在问题,因为密码类似于-@'M @ + 12w-zT!3Lv)

我如何正确地在密码中转义此符号?

0 个答案:

没有答案