等待远程Powershell脚本完成

时间:2019-04-25 09:05:26

标签: powershell

我正在创建一个powershell脚本,以将Dynamics NAV应用程序对象导入我的Dynamics NAV 2018数据库。 微软为此提供了一个PS cmdlet-Import-NAVApplicationObjects-但是我很难等待命令完成。

我有一个执行以下操作的调用脚本

$PSSession = New-PSSession -ComputerName $TargetMachine -Credential $MyCredential
Invoke-Command -Session $PSSession -ArgumentList $Database_Name_user, $MyCredential -ScriptBlock {
    $process = Start-Process powershell.exe -Verb RunAs -Wait -PassThru -ArgumentList "-File `"C:\Users\User\Desktop\Database\Import1.ps1`" $args[0]"
    $process.WaitForExit()
    if ($process.ExitCode -ne 0) {
        throw $process.StandardError.ReadToEnd()
    }
}

我的Import1.ps1上的脚本$TargetMachine如下

param(
[String]$Database_Name_user
)

try {
    $AllFiles = "C:\Users\User\Documents\AllFiles.txt"

    $Modules = "C:\GIT\Loading Components\NAVModuleImport.ps1" 
    $OutputBuffer = import-module $Modules

    Import-NAVApplicationObject -Path $AllFiles -DatabaseServer "SQLSRV001" -DatabaseName $Database_Name_user -SynchronizeSchemaChanges "No" -ImportAction "Overwrite" -Confirm:$false | Out-Null

}
catch{
    $_ | Out-File "C:\Users\User\Documents\Import1.txt"
}

文件AllFiles.txt的大小为220 MB,包含7700多个Dynamics NAV应用程序对象(表,页面,代码单元等)。 当我启动直接从Import-NAVApplicationObject中存储的远程计算机执行$TargetMachine的脚本时,一切正常运行,并且该过程最多需要10到15分钟,如预期的那样。

如第一个代码示例中所示调用脚本时,输出停止一分钟,然后说一切都完成了。

感谢您的帮助,谢谢。

编辑:解决方案

我注意到如图所示的脚本正在运行,Import-NAVApplicationObjects cmdlet刚刚失败。 当我在远程计算机上提升Powershell进程并运行导入时,该cmdlet尝试对数据库进行NT-Authority \ Anonymous身份验证。 然后,我将打开远程PSSession的用户的凭据传递给Import1.ps1,并使用了导入cmdlet的参数-UserName-Password。 遗憾的是,此过程再次 失败。

现在,我尝试与我想用于身份验证,更改密码等的用户一起使用,并且可以正常工作!密码包含分号;,显然import cmdlet对此不满意。

所以我的简单解决方案是:从密码中删除分号。

0 个答案:

没有答案