I’m trying to change the IPPhone attribute. The script will take the output of OfficePhone strip everything bu the last 4 chars and put the last 4 digets into IPPhone field.When I run the script I get the following error
"You cannot call a method on a null-valued expression. At line:1 char:110 + ... ne | ForEach { $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone.Length - 4 ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Code
Get-ADUser -Filter * -SearchBase 'OU=Test,OU=Employees,DC=domain,DC=us' -Properties OfficePhone | ForEach { $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone.Length - 4, 4) } Set-ADUser -Identity $_.SamAccountName -Replace @{ipPhone=$phoneNumber} }
If I run
Get-ADUser -Filter * -SearchBase 'OU=Test,OU=Employees,DC=domain,DC=us' -Properties OfficePhone | ForEach { $phoneNumber = $_.OfficePhone.Substring($_.OfficePhone) }
I error
You cannot call a method on a null-valued expression. At line:1 char:109 + ... one | ForEach {$phoneNumber = $_.OfficePhone.Substring($_.OfficePhone) } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Cannot convert argument "startIndex", with value: "(800) 555-1212 x1161", for "Substring" to type "System.Int32": "Cannot convert value "(800) 555-1212 x1161" to type "System.Int32". Error: "Input string was not in a correct format."" At line:1 char:109 + ... one | ForEach {$phoneNumber = $_.OfficePhone.Substring($_.OfficePhone) } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Thoughts?
submitted by /u/kenfury
[link] [comments]
The post Null-Value error on Get-ADUser appeared first on How to Code .NET.