Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 如何成功运行两个不同的脚本Powershell_Function_Powershell - Fatal编程技术网

Function 如何成功运行两个不同的脚本Powershell

Function 如何成功运行两个不同的脚本Powershell,function,powershell,Function,Powershell,我有两个不同的代码块。一个块针对端点运行,另一个块针对不同的端点运行。如果它们单独运行,则运行良好。但我需要让它们在一个文件中成功运行。有人能帮我吗 第一个街区: [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" $url = "https://FIRSTENDPOINT.com/Integration-Server/XXXXXXIntegrations?requestJobDescription={""type

我有两个不同的代码块。一个块针对端点运行,另一个块针对不同的端点运行。如果它们单独运行,则运行良好。但我需要让它们在一个文件中成功运行。有人能帮我吗

第一个街区:

[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"

$url = "https://FIRSTENDPOINT.com/Integration-Server/XXXXXXIntegrations?requestJobDescription={""type"":""file"",""credentials"":{""partnerUserID"":""XXXXX"",""partnerUserSecret"":""XXXXXX""},""onReceive"":{""immediateResponse"":[""returnRandomFileName""]},""inputSettings"":{""type"":""combinedReportData"",""filters"":{""startDate"":""2018-01-01""}},""outputSettings"":{""fileExtension"":""pdf"",""includeFullPageReceiptsPdf"":""False"",""fileBasename"":""ExpenseReimbursementReport""}}"

$template = '<#if addHeader == true>
    Employee Name, Amount, Status, Report Date, Employee Email, Report ID<#lt>
</#if>
    <#list reports as report>
    <#setting date_format="MM/dd/yyyy">
    <#setting locale="en_US">
    <#assign total = report.total/100>
    ${report.submitter.fullName},<#t>
    ${total},<#t>   
    ${report.status},<#t>
    ${report.submitted?date("yyyy-MM-dd HH:mm:ss")},<#t>
    ${report.accountEmail},<#t>
    ${report.reportID}<#lt>
</#list>'

$encode = [System.Web.HttpUtility]::UrlEncode($template)

Invoke-RestMethod -ContentType 'application/json' -Method Post -Uri $url'&template='$encode -OutVariable temp

$Data=$temp.split(",")
$var= @{}

$i=0
foreach ($item in $Data)
{$var[$i] = "https://XXXXX.com/Integration-Server/XXXXIntegrations?requestJobDescription={""type"":""download"",""credentials"":{""partnerUserID"":""XXXXX"",""partnerUserSecret"":""XXXXXX""},""fileName"":"+$item+"}"
$output = "C:\files\$item"
Invoke-RestMethod -ContentType 'application/json' -Method Post -Uri $var[$i] -Outfile $output
$i++
编辑:

当我尝试在同一个文件中运行它们时,无论是作为一个块还是作为函数,都会抛出大量错误。在第12个错误之后,我不得不停止它,因为它只是不断创建空文件,每次都会抛出错误:

get-content : Cannot find path 'C:\files\Box Sync' because it does not exist.
At C:\Users\USER\Documents\ExpenseReimbursement_Powershell\Expense_GetReports.ps1:65 char:16
+ $fileContent = get-content -Raw $fileName
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\files\Box Sync:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Exception calling "GetBytes" with "1" argument(s): "Array cannot be null.
Parameter name: chars"
At C:\Users\USER\Documents\ExpenseReimbursement_Powershell\Expense_GetReports.ps1:66 char:1
+ $fileContentBytes = [System.Text.Encoding]::Default.GetBytes($fileCon ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception calling "ToBase64String" with "1" argument(s): "Value cannot be null.
Parameter name: inArray"
At C:\Users\USERNAME\Documents\ExpenseReimbursement_Powershell\Expense_GetReports.ps1:67 char:1
+ $fileContentEncoded = [System.Convert]::ToBase64String($fileContentBy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

最直接的方法是将整个第二个代码块放在第一个代码块之后的同一个文件中

在这一点上,程序将自然地从第一个流到第二个

但是,由于每个块似乎都代表一个特定的操作,因此这可能是一个将每个块放入函数中以封装它的好机会

这还允许您将代码转换为一种“模板”,您可以将可能更改的值作为参数传入,然后获得一组很好的可重用操作集,并将其作为一个操作集调用。我现在还不打算谈这个,但这是需要考虑的

我不完全确定第一个块应该做什么(作为一个高级操作),但我将猜测并将其描述为“后集成结果”,因此我将调用此函数
Register IntegrationResult
(这遵循PowerShell
动词-名词
命名约定,使用经批准的动词和单数名词,但您可以随意命名函数):

那么,这有什么意义呢?现在,当您想一个接一个地调用时,您的代码如下所示:

Register-IntegrationResult
Send-ImportantReport
Register-IntegrationResult -JobID 1234
Send-ImportantReport -Path C:\Reports
如果你需要做不止一次,你只需直呼其名

作为将来的更改,您可能希望添加参数,这样可以传入可以更改的部分,如凭据、URL或本地文件路径。然后您的调用可能如下所示:

Register-IntegrationResult
Send-ImportantReport
Register-IntegrationResult -JobID 1234
Send-ImportantReport -Path C:\Reports

如果它们没有成功运行,会发生什么?错误消息?Hi-Lit,请参见上面的编辑。谢谢。它只从文件位置C:\files获取2个pdf,但会查找所有其他文件。但是当我单独运行它们时,它运行良好。请忽略注释和编辑。Briantist提供了正确的答案。谢谢。谢谢你的提示,但我确实试着在一个文件中作为函数运行它,结果出现了错误。如果我一个文件接一个文件单独运行脚本,那么它就会“崩溃”请看上面的“编辑”。事实上,这回答了我的问题。我意识到我在其中一个函数中输入了一个错误,这会使我的所有功能都失效。非常感谢您的帮助。@LilithMae很好,很高兴您解决了这个问题!我不确定您目前使用的编辑器是什么,但PowerShell ISE带有Windows,适合编写ng PowerShell,但最近免费和开放源代码变得更加流行,甚至是Microsoft的推荐。它具有强大的功能,其代码格式将使它更容易以有助于遵循代码的风格编写,包括括号/大括号的位置等。希望这能有所帮助!
Register-IntegrationResult -JobID 1234
Send-ImportantReport -Path C:\Reports