1

In my sharepoint powershell script: I am retracting the webpart, then removing the webpart, then adding (and deploying it). When I retract it, I have to wait until it finishes before I can move on. I am doing an infinite loop and i want to catch the error. If there is error, then wait, and try again, if no error, then break and move on. The issue is that the try catch is not catching the error. (about you can't make a change when theres an ongoing process).

Does anyone know how to fix this?

Thanks.

function RETRACT()
{
    ./retractwebpart.ps1
}

function REMOVE()
{
    ./removewebpart.ps1
}

function ADD()
{
    ./addwebpart.ps1
}




RETRACT

do
{
    try 
    {
        REMOVE -ErrorAction Stop
        Break
    }
    Catch [System.Exception] 
    {
        Start-Sleep -m 1000
    }
}
while ($true)

do
{
    try 
    {
        ADD -ErrorAction Stop
        Break
    }
    Catch [System.Exception] 
    {
        Start-Sleep -m 1000
    }
}
while ($true)

Retract file

# Retracting the solution from web application http://mydomain
Write-Host "Retracting the solution from web application http://mydomain..."
Uninstall-SPSolution –Identity PDFLibrary.wsp –WebApplication http://mydomain -confirm:$false -ErrorAction Stop

Remove file

# Removing the solution from web application http://mydomain
Write-Host "Removing the solution from web application http://mydomain..."
Remove-SPSolution –Identity PDFLibrary.wsp -confirm:$false -ErrorAction Stop

Add file

# Adding the solution to SharePoint
Write-Host "Adding the solution to SharePoint..."
Add-SPSolution C:/PDFLibrary/PDFLibrary.wsp

# Deploying the solution to web application http://mydomain
Write-Host "Deploying the solution to web application http://mydomain..."
Install-SPSolution –Identity PDFLibrary.wsp –WebApplication http://mydomain –GACDeployment
4

1 回答 1

1

我试图复制您的问题并试一试。这次我删除-ErrorAction Stop了,它仍然进入了CatchBlock。诀窍可能是我认为将 Catch [System.Exception] 块放置到位(不确定,因为在尝试 3 中我删除了它-ErrorAction并且[System.Exception]它仍然进入了 Catch 块。正如 Hyper Anthony 在评论中建议的那样-ErrorAction Stop,所有命令都支持所以它应该可以工作。以下函数中的脚本故意抛出错误以测试逻辑。我认为你的 RETRACT 函数抛出错误。

Attempt1 -Without -ErrorAction

Function Remove
{
 C:\scripts\so\DeletedFiles.ps1 
}
try
{
 Remove
}
Catch [System.Exception]{
Write-Host "Unhandled Exception occurred"
}

它工作正常 - 降落到 Catch Block。

尝试 2 与-ErrorAction停止

Function Remove
{
 C:\scripts\so\DeleteFiles.ps1 
}
try
{
 Remove -ErroAction Stop
}
Catch [System.Exception]{
Write-Host "Unhandled Exception occurred"
}

它工作正常并进入 Catch 块。

尝试3

Function Remove
{
 C:\scripts\so\DeletedFiles.ps1 
}
try
{
 Remove
}
Catch{
Write-Host "Unhandled Exception occurred"
}

它仍然有效并进入 Catch 块。

经过所有这些尝试,我相信您的RETRACT函数正在抛出 try catch 块之外的错误。

于 2013-08-29T19:06:24.103 回答