question

EavenHuang avatar image
0 Votes"
EavenHuang asked EavenHuang commented

Powershell - Cannot convert value to type "System.Int32"

Dear experts,

I'm working on below script trying to replace the value of mailNickName with the prefix part of UPN value. It works great if we had EMPTY mailNickName field (where it showed <not set>). However, if there was value already existing in mailNickName field, in this example the value is test2 and the UPN prefix is test2.mcs

 Cannot convert value "test2" to type "System.Int32". Error: "Input string was not in a correct format."
 At D:\OneDrive - GTIIT\IT Dept\PowerShell\Scripts\Case_Study\Replace mailNickName with UPN prefix\test.ps1:9 char:26
 +                      if ($UPN_Prefix -ne -$_.mailNickname){
 +                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
     + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Below is the source code:

 $OUs = "OU=Eaven-Testing,OU=Users,OU=XXX,DC=edu,DC=cn"
  foreach ($OU in $OUs) {
      Get-ADUser -Filter * -SearchBase $OU -Properties samAccountName, userPrincipalName, mailNickname, Enabled | 
          Where-Object { $_.Enabled -eq $True} |
              ForEach-Object{
                  $parts = $_.userPrincipalName -split '@'
                  $UPN_Prefix = $parts[0]
                  if ($_.mailNickname){
                      if ($UPN_Prefix -ne -$_.mailNickname){
                          $was = $_.mailNickName
                          Set-ADUser -Identity $_.samAccountName -Replace @{mailNickname="$UPN_Prefix"}
                      }
                      else{
                          Write-host "$UPN_Prefix already exists, no change will be made on it."
                      }
                         
                  }
                  else{
                      $was = "EMPTY"
                      Set-ADUser -Identity $_.samAccountName -Add @{mailNickname="$UPN_Prefix"}
                  }
                  [PSCustomObject]@{
                      Identity = $_.samAccountName
                      UPN = $_.userPrincipalName
                      NicknameWas = $was
                      NickNameNow = $parts[0]
                  }
              } | Export-Csv -NoType 'C:\tmp\userPrincipalName_vs_mailNickname.csv'
  }

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered EavenHuang commented

Hi,

You should remove the extra - prepended to $_.mailNickname in LIne 9.

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ian,

Nice to see you again and thanks again for your comment. Good catch and now it works just perfect!!

0 Votes 0 ·