1

I have a small script that refreshes a users profile. Occasionally I need to run the script on a large number of users which takes some time because I need to type each one individually.

PS C:\> . .\Scripts\CustomScript.ps1
PS C:\> FunctionCall "user1"
PS C:\> FunctionCall "user2"

Also not as important but sometimes a function will fail if and report an error. Can that text be captured?

  • I was able to solve this by including the function calls within the .ps1 file and I was able to capture the errors using a try catch block around the failing line of code. – Woodsy Feb 28 '14 at 16:19
0

Perhaps something like:

Start-SPAssignment -Global
Start-Transcript
$web = Get-SPWeb ...
$web.Users | % {
  FunctionCall $_
}
End-Transcript
Stop-SPAssignment -Global
0

I was able to solve this by doing the following:

instead of this

PS C:\> . .\Scripts\CustomScript.ps1
PS C:\> FunctionCall "user1"
PS C:\> FunctionCall "user2"

I opened the ps1 file and added the function calls to the file as so

Function FunctionCall ([string]$username) {
    try{
        //Do Something
    } catch {
        //Write error to log file
    }
}
Write-Host "Starting Script..."
FunctionCall "user1"
FunctionCall "user2"
Write-Host "Finished"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.