0

I'm using PowerShell Script to get user login name

try {
    $CreatedByUser=""
    $CreatedBy= $($version["Author"])

    if($CreatedBy -ne $null){
       $CreatedByObj = New-Object Microsoft.Sharepoint.SPFieldUserValue($w,$CreatedBy)

       $CreatedByUser = $CreatedByObj.User.LoginName
    }
}
catch {
    $CreatedByUser=""
}

Please check value for CreatedBy variable is

411;#Akshay Nangare,#domain\Akshay,#[email protected],#,#Akshay Nangare

Please suggest something

2
  • it should be SPFieldUserValue, is the spelling correct ? Commented Jan 25, 2017 at 6:39
  • Actual while writing the question I had make that mistake. Sorry for that. I will update question again Commented Jan 25, 2017 at 6:51

2 Answers 2

1

Try below code:

try
{       
    $CreatedByUser = $version.CreatedBy.User.LoginName      
}
catch
{
    $CreatedByUser=""
}
1

Please note that your catch block will never catch an exception as it will throw a non-terminating error. For this to work as you expect it to you should change the global ErrorActionPreference variable:

try
{  
    $ErrorActionPreference = "Stop" 
    $CreatedByObj = New-Object Microsoft.Sharepoint.SPFieldUserValue($w,$CreatedBy)
    $CreatedByUser = $version.CreatedBy.User.LoginName
}
catch
{
    $CreatedByUser=""
}
finally {
    $ErrorActionPreference = "Continue"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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