无法使用批处理文件或任务计划程序调用 Web 请求运行 powershell 脚本

问题描述 投票:0回答:0

当我们自己运行脚本时,它可以完全独立运行,但是当我们尝试使用任务调度程序时,我们每次都会得到无效的用户名或密码。

这是脚本:

`$apiUri = "https://na2.docusign.net/restapi"
# Download a document from an envelope
# This script uses the envelope_id stored in ../envelope_id.
# The envelope_id file is created by example eg002SigningViaEmail.ps1 or
# can be manually created.
 
 
# Obtain your Oauth access token
$accessToken = Get-Content .\ds_access_token.txt
 
# Obtain your accountId from demo.docusign.net -- the account id is shown in
# the drop down on the upper right corner of the screen by your picture or
# the default picture.
$accountId = Get-Content .\API_ACCOUNT_ID
 
#Variables <3
 
 
# Check that we have an envelope id
 
$headers = @{
    'Authorization' = "Bearer $accessToken";
    'Content-Type'  = "application/json";
  }
 
 
$outputFileExtension = ".pdf"
 
$fromDate = ((Get-Date).AddHours((-21)).ToString("yyyy-MM-ddThh:mm:ssK"))
try{
$(Invoke-RestMethod `
    -Uri "${apiUri}/v2.1/accounts/${accountId}/envelopes" `
    -Method 'GET' `
    -Headers @{
    'Authorization' = "Bearer $accessToken";
    'Content-Type'  = "application/json";
  } `
    -Body @{ "from_date" = ${fromDate}; "Status" = "Completed" }).envelopes |Select-Object -property envelopeId -ExpandProperty envelopeId| out-file -filepath .\envelopes.txt 
 
}
catch{
Write-Host "error with token"
 
 
}
foreach($line in [System.IO.File]::ReadLines(".\envelopes.txt"))
{
 
$envelopeId = $line
#documentID
Invoke-RestMethod `
  -Uri "${apiUri}/v2.1/accounts/${accountId}/envelopes/${envelopeId}/documents" `
  -Method 'GET' `
  -Headers $headers |Select-Object -Property EnvelopeDocuments -ExpandProperty EnvelopeDocuments|Select-Object -expandproperty documentId -skipLast 1 |Out-File .\docID.txt 
# ***DS.snippet.0.end
#DocumentName
Invoke-RestMethod `
  -Uri "${apiUri}/v2.1/accounts/${accountId}/envelopes/${envelopeId}/documents" `
  -Method 'GET' `
  -Headers $headers |Select-Object -Property EnvelopeDocuments -ExpandProperty EnvelopeDocuments | Select-Object -ExpandProperty name -SkipLast 1 |Out-File .\docName.txt
 
$IDList = New-Object System.Collections.ArrayList
$NameList = New-Object System.Collections.ArrayList
  foreach($line in [System.IO.File]::ReadLines(".\DocID.txt"))
  {
 
 
$IDList.add($line)
 
#Write-Host Added $Line to ID List
 
 
  }
  #Write-Output $IDList
 
  foreach($line in [System.IO.File]::ReadLines(".\DocName.txt"))
  {
    $NoExt = $Line.Substring(0, $Line.Length - 4)
    $NameList.add($NoExt)
 
#Write-Host Added $NoExt to Name List
 
 
  }
  #Write-Output $NameList
 
 
$DN = $Namelist.Count
$outputFileExtension = ".PDF"
While($DN -ge 0)
{
$DocID = $IDList[$DN]
$DocName = $NameList[$DN]
 
 
#Write-Host $IDList[$DN]
#Write-Host $NameList[$DN]
 
    Invoke-RestMethod `
    -Uri "${apiUri}/v2.1/accounts/${accountId}/envelopes/${envelopeId}/documents/${DocID}" `
    -Method 'GET' `
    -Headers $headers `
    -UseDefaultCredentials `
    -OutFile C:\PowershellPdfs\${DocName}${outputFileExtension}
    #Real output 
 
    #test output
    #-OutFile O:/Data/Docusign/v
 
    $DN = $DN - 1
 
 
 
 
 
 
 
}
 
 
 
 
}
 
Write-Output "Done."
 
 
`
 
 

日志是:


PS>$global:?
True
**********************
Windows PowerShell transcript end
End time: 20230322094117
**********************
**********************
Windows PowerShell transcript start
Start time: 20230322094323
Username: HLB-PORTAL-002\_svc_ichan_hlb
RunAs User: HLB-PORTAL-002\_svc_ichan_hlb
Configuration Name: 
Machine: HLB-PORTAL-002 (Microsoft Windows NT 10.0.17763.0)
Host Application: powershell.exe -ExecutionPolicy Bypass -File C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\launcher.ps1
Process ID: 6300
PSVersion: 5.1.17763.3770
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.3770
BuildVersion: 10.0.17763.3770
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is .\logs\launcher.log
Welcome to the DocuSign PowerShell Launcher

Quickstart Enabled, please wait


Enters the pulldocs statement
0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

0
0
Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Invoke-RestMethod : The user name or password is incorrect.
At C:\Users\_svc_ichan_hlb\Desktop\Docusign-Document-Pull-master\PullDocs.ps1:100 char:5
+     Invoke-RestMethod `
+     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Done.

我们试过-usedefaultcredentials 我们找不到任何其他信息

这里是定时任务设置

enter image description here

它调用运行 powershell 脚本的批处理文件

enter image description here

这是批处理文件: enter image description here

这是导致文件拉取脚本的启动脚本

$ErrorActionPreference = "Stop" # force stop on failure

$configFile = ".\settings.json"


if ((Test-Path $configFile) -eq $False) {
    Write-Output "Error: "
    Write-Output "First copy the file '.\config\settings.example.json' to '$configFile'."
    Write-Output "Next, fill in your API credentials, Signer name and email to continue."
}

Start-Transcript -Path .\logs\launcher.log -Append
# Check that we have an email address stored after running the 2 Admin code example
# in case the file was created before - delete it


# Get required environment variables from .\config\settings.json file
$config = Get-Content $configFile -Raw | ConvertFrom-Json
function isCFR {
    $response = New-TemporaryFile
    $accessToken = Get-Content .\ds_access_token.txt
    $accountId = Get-Content .\API_ACCOUNT_ID

    Invoke-RestMethod `
        -Uri "https://api.docusign.net/restapi/v2.1/accounts/$accountId/settings" `
        -Method 'GET' `
        -Headers @{
        'Authorization' = "Bearer $accessToken";
        'Content-Type'  = "application/json";
    } `
        -OutFile $response
    $env:CFR_STATUS = Select-String -Pattern '"require21CFRpt11Compliance":"true"' -Path $response
}


function  checkOrgId {

    if ($config.ORGANIZATION_ID -eq "{ORGANIZATION_ID}" ) {
        Write-Output "No Organization Id in the config file. Looking for one via the API"
        # Get required environment variables from .\config\settings.json file
        $accessToken = Get-Content .\ds_access_token.txt

        $base_path = "https://api.docusign.net/management"

        $response = New-TemporaryFile
        Invoke-RestMethod `
            -Uri "$base_path/v2/organizations" `
            -Method 'GET' `
            -Headers @{
            'Authorization' = "Bearer $accessToken";
            'Content-Type'  = "application/json";
        } `
            -OutFile $response

        $organizationId = $(Get-Content $response | ConvertFrom-Json).organizations[0].id

        $config.ORGANIZATION_ID = $organizationId
        write-output $config | ConvertTo-Json | Set-Content $configFile
        Write-Output "Organization id has been written to config file..."
        Remove-Item $response

    }
}


function startLauncher {


        # Load via Quickstart
        if ($config.QUICKSTART -eq "true" ) {
            if ($null -eq $firstPassComplete) {
                Write-Output ''
                Write-Output "Quickstart Enabled, please wait"
                write-Output ''
                powershell.exe -ExecutionPolicy Bypass -Command .\code_grant.ps1 -clientId $($config.INTEGRATION_KEY_AUTH_CODE) -clientSecret $($config.SECRET_KEY) -apiVersion $("eSignature")


                if ((Test-Path "./ds_access_token.txt") -eq $true) {
                    Write-Output "Enters the pulldocs statement"
                    powershell.exe -ExecutionPolicy Bypass  -Command .\PullDocs.ps1 -clientId $($config.INTEGRATION_KEY_AUTH_CODE) -clientSecret $($config.SECRET_KEY) -apiVersion $("eSignature")

                    # This is to prevent getting stuck on the
                    # first example after trying it the first time
                    $firstPassComplete = "true"

                    
                }
                else {
                    Write-Error "Failed to retrieve OAuth Access token, check your settings.json and that port 8080 is not in use"  -ErrorAction Stop
                }
            }
        }
    }

    function launchJWT {


        # Load via Quickstart
        if ($config.QUICKSTART -eq "true" ) {
            if ($null -eq $firstPassComplete) {
                Write-Output ''
                Write-Output "Quickstart Enabled, please wait"
                write-Output ''
                powershell.exe -Command .\jwt.ps1 -clientId $($config.INTEGRATION_KEY_JWT) -apiVersion $("eSignature")


                if ((Test-Path "./ds_access_token.txt") -eq $true) {
                    powershell.exe -Command .\PullDocs.ps1

                    # This is to prevent getting stuck on the
                    # first example after trying it the first time
                    $firstPassComplete = "true"

                    
                }
                else {
                    Write-Error "Failed to retrieve OAuth Access token, check your settings.json and that port 8080 is not in use"  -ErrorAction Stop
                }
            }
        }
    }

Write-Output "Welcome to the DocuSign PowerShell Launcher"
#launchJWT
startLauncher

This is the folder

powershell scheduled-tasks
© www.soinside.com 2019 - 2024. All rights reserved.