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,#Akshay@domain.com,#,#Akshay Nangare

Please suggest something

  • it should be SPFieldUserValue, is the spelling correct ? – Gautam Sheth Jan 25 '17 at 6:39
  • Actual while writing the question I had make that mistake. Sorry for that. I will update question again – Akshay Dattatray Nangare Jan 25 '17 at 6:51
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, privacy policy and cookie policy

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