【问题标题】:Run PowerShell from Azure DevOps从 Azure DevOps 运行 PowerShell
【发布时间】:2021-02-26 14:26:08
【问题描述】:

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

我有带有 *.wsp 的文件夹。我有一个 PS 脚本。如果我在这台机器上手动操作,它工作正常。没有错误。

但是当我使用 Azure DevOps 时,我收到一条消息:

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

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

Add-PSSnapin "Microsoft.SharePoint.Powershell"

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

我已经尝试添加

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

【问题讨论】:

  • 能否提供管道中 PowerShell 步骤和 PowerShell 脚本开头的屏幕截图?
  • 附上图片。

标签: sharepoint azure-devops


【解决方案1】:

尝试在您的命令前加上Get-PSSnapIn

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

【讨论】:

  • 这是结果:[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'c:\agent\_work\_temp\78afcb5e-4bf4-43a5-b7a9-18e623fcde84.ps1'" 2021-02-28T09:41: Transcript started, output file is C:\Windows\ServiceProfiles\NetworkService\Documents\PowerShell_transcript.mu1Pd_Mk.txt 2021-02-28T09:41:Get-PSSnapin : No Windows PowerShell snap-ins matching the pattern 'Microsoft.Sharepoint.Powershell' were found. Check 2021-02-28T09:41: the pattern and then try the command again.
【解决方案2】:

Add-PSSnapin:没有为 Windows PowerShell 版本 5 注册任何管理单元。 在 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1:1 char:1

根据报错信息,powershell执行ps文件时,报错。

在 Auzre Devops Powershell 任务中,它将使用来自C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 的 powershell.exe。

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

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

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered

您可以检查Microsoft.SharePoint.Powershell是否存在。

那么你就可以使用正确的powershell.exe路径来执行ps文件了。

例如:

C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe "path/xx.ps"

【讨论】:

  • Get-PSSnapin : 找不到与模式“Microsoft.Sharepoint.Powershell”匹配的 Windows PowerShell 管理单元。
猜你喜欢
  • 2019-03-07
  • 2020-04-02
  • 2019-03-30
  • 2021-09-27
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2019-05-05
相关资源
最近更新 更多