Run PowerShell from Azure DevOps



我有一个任务:使用Azure DevOps管道在远程机器上安装解决方案(wsp)。

我有一个文件夹*.wsp。我有一个ps脚本。如果我在这台机器上手动操作,效果很好。没有错误。

但是当我使用Azure DevOps时,我有一个消息:

Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At C:WindowsSysWOW64WindowsPowerShellv1.0profile.ps1:1 char:1
+ Add-PSSnapin "Microsoft.SharePoint.Powershell"

在我的PS脚本的最开始,我有这个:

Add-PSSnapin "Microsoft.SharePoint.Powershell"

我需要它,因为我在脚本中使用Uninstall-SPSolution, Remove-SPSolution, Add-SPSolution和Install-SPSolution cmdlets。

我试着添加

Add-PSSnapin "Microsoft.SharePoint.Powershell" 

到"profile.ps1",我已经尝试在powershell中运行powershell。一次又一次的误差都是一样的。我不知道如何修理它。

https://i.stack.imgur.com/UNM7R.png
https://i.stack.imgur.com/NTiWs.png
https://i.stack.imgur.com/sA6tU.png
https://i.stack.imgur.com/bK9kq.png

尝试在命令前加上Get-PSSnapIn:

if (Get-PSSnapin "Microsoft.SharePoint.Powershell") {
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

Add-PSSnapin: Windows PowerShell版本5没有注册管理插件。在C:WindowsSysWOW64 WindowsPowerShell v1.0 概要文件。ps1:1 char: 1

根据错误信息,powershell在执行ps文件时得到错误信息

在Auzre Devops Powershell任务中,它将使用C:WindowsSystem32WindowsPowerShellv1.0powershell.exe中的Powershell .exe。

用于执行powershell文件的exe路径与默认的不同。

您可以在azure devops中运行以下命令:

C:WindowsSystem32WindowsPowerShellv1.0powershell.exe Get-PSSnapin -Registered

C:WindowsSysWOW64WindowsPowerShellv1.0powershell.exe Get-PSSnapin -Registered

查看Microsoft.SharePoint.Powershell是否存在

然后你可以使用正确的powershell.exe路径来执行ps文件。

例如:

C:Windowssystem32WindowsPowerShellv1.0powershell.exe "path/xx.ps"

最新更新