Jump to content

Recommended Posts

Posted

Afternoon,

 

I'm trying to get a powershell script working that will take an AD Group and then for each member it will pull some AD attributes, take a custom attribute, convert it and write it back to a separate atrribute. Hopefully that makes sense.

 

Here's the script:

 

[color=#000000][font=Helvetica] [/font][/color][color=#A82D00][font=Helvetica]$users[/font][/color][color=#696969][font=Helvetica]=[/font][/color][color=#0000FF][font=Helvetica]get-adgroupmember[/font][/color][color=#000000][font=Helvetica] [/font][/color][color=#8B0000][font=Helvetica]"IT Support"[/font][/color][color=#000000][font=Helvetica] [/font][/color][color=#696969][font=Helvetica]|[/font][/color][color=#000000][font=Helvetica] [/font][/color][color=#0000FF][font=Helvetica]ForEach-Object[/font][/color][color=#000000][font=Helvetica] {[/font][/color][color=#000000][font=Helvetica][color=#A82D00]$username[/color] [color=#696969]=[/color] [color=#A82D00]$_[/color][color=#696969].[/color]SamAccountName[/font][/color]
[color=#8A2BE2][font=Helvetica][color=#A82D00]$user[/color][color=#000000] [/color][color=#696969]=[/color][color=#000000] [/color][color=#0000FF]Get-ADUser[/color][color=#000000] [/color][color=#000080]-Identity[/color][color=#000000] [/color][color=#A82D00]$username[/color][color=#000000] [/color][color=#000080]-Properties[/color][color=#000000] [/color]givenName[color=#696969],[/color]surname[color=#696969],[/color]SIMSPID[color=#696969],[/color]PrimaryCardID[color=#696969],[/color]PAXTONID[/font][/color]
[color=#000000][font=Helvetica][color=#A82D00]$c1[/color] [color=#696969]=[/color] [color=#A82D00]$user[/color][color=#696969].[/color]PrimaryCardID[color=#696969].[/color]ToString()[color=#696969].[/color]substring([color=#800080]6[/color][color=#696969],[/color][color=#800080]2[/color])[/font][/color]
[color=#000000][font=Helvetica][color=#A82D00]$c2[/color] [color=#696969]=[/color] [color=#A82D00]$user[/color][color=#696969].[/color]PrimaryCardID[color=#696969].[/color]ToString()[color=#696969].[/color]substring([color=#800080]4[/color][color=#696969],[/color][color=#800080]2[/color])[/font][/color]
[color=#000000][font=Helvetica][color=#A82D00]$c3[/color] [color=#696969]=[/color] [color=#A82D00]$user[/color][color=#696969].[/color]PrimaryCardID[color=#696969].[/color]ToString()[color=#696969].[/color]substring([color=#800080]2[/color][color=#696969],[/color][color=#800080]2[/color])[/font][/color]
[color=#000000][font=Helvetica][color=#A82D00]$c4[/color] [color=#696969]=[/color] [color=#A82D00]$user[/color][color=#696969].[/color]PrimaryCardID[color=#696969].[/color]ToString()[color=#696969].[/color]substring([color=#800080]0[/color][color=#696969],[/color][color=#800080]2[/color])[/font][/color]
[color=#A82D00][font=Helvetica]$convertedcard[color=#696969]=[/color][color=#8B0000]"[/color]$c1[color=#8B0000] + [/color]$c2[color=#8B0000] + [/color]$c3[color=#8B0000] + [/color]$c4[color=#8B0000]"[/color][/font][/color]
[color=#A82D00][font=Helvetica]$cardnum[color=#000000] [/color][color=#696969]=[/color][color=#000000] [/color][color=#696969][[/color][color=#006161]convert[/color][color=#696969]]::[/color][color=#000000]toint64([/color]$convertedcard[color=#696969],[/color][color=#800080]16[/color][color=#000000])[/color][/font][/color]
[color=#000000][font=Helvetica][color=#0000FF]Set-ADUser[/color] [color=#000080]-Identity[/color] [color=#A82D00]$username[/color] [color=#000080]-replace[/color] @{PAXTONID[color=#696969]=[/color][color=#8B0000]"[/color][color=#A82D00]$cardnum[/color][color=#8B0000]"[/color]}[/font][/color]
[color=#000000][font=Helvetica]}[/font][/color][color=#000000][font=Helvetica] [/font][/color]

 

The idea is that this will populate AD and then allow Salamander to sync it through to Net2 for the card ID's.

 

However when I run the script I get an error that says "You cannot call a method on a null-valued expression"

 

Does anyone have any ideas?

Posted
What line number is returning the "You cannot call a method on a null-valued expression" error and what line is that in the code you have posted?
Posted
What line number is returning the "You cannot call a method on a null-valued expression" error and what line is that in the code you have posted?

 

$c1 = $user.PrimaryCardID.ToString().substring(6,2)

$c2 = $user.PrimaryCardID.ToString().substring(4,2)

$c3 = $user.PrimaryCardID.ToString().substring(2,2)

$c4 = $user.PrimaryCardID.ToString().substring(0,2)

 

 

Each of those lines are returning the error. It is almost as though the script is not reading the user and user attributes form the previous lines in the script.

Posted

So if you add $user above those four lines, does $user contain a user object or is it null?

 

I don't know if the forum has done it or your code is really squashed together like that.

$users=get-adgroupmember"IT Support"

won't work.

$users=get-adgroupmember "IT Support"

will work but

$users = Get-ADGroupMember "IT Support"

looks much nicer

Posted

## Add converted card number to users AD objects ##

## Import Active Directory PS Module

 

import-module activedirectory

 

## Set script Variables

 

$users=get-adgroupmember "IT Support" | ForEach-Object {

$username = $_.SamAccountName

$user = Get-ADUser -Identity $username -Properties givenName,surname,SIMSPID,PrimaryCardID,PAXTONID

 

if($_.PrimaryCardID -eq $null) {

Write-Host "AD Attributes missing for $($_.givenName) $($_.surname)"

}else{

 

$c1 = $user.PrimaryCardID.ToString().substring(6,2)

$c2 = $user.PrimaryCardID.ToString().substring(4,2)

$c3 = $user.PrimaryCardID.ToString().substring(2,2)

$c4 = $user.PrimaryCardID.ToString().substring(0,2)

$convertedcard="$c1 + $c2 + $c3 + $c4"

$cardnum = [convert]::toint64($convertedcard,16)

Set-ADUser -Identity $username -replace @{PAXTONID="$cardnum"}

}

}

 

Hopefully that is a bit clearer. I have also added an if function and interestingly it does not give a first and last name but does show "AD Attributes are missing for"

Posted (edited)
You want to use $user.PrimaryCardID, not $_.PrimaryCardID. $_ in your ForEach-Object is referring to each item in the pipeline output by Get-ADGroupMember (which won't have the PrimaryCardID properties). Edit - Just realised it's only your PrimaryCardID null check where you've done this. Are you sure all of your users have a PrimaryCardID value because otherwise calling ToString() on it may return that error Edited by fordea
Posted (edited)

That should be

 

[color=#333333]if($user.PrimaryCardID -eq $null)
{[/color][color=#333333]Write-Host "AD Attributes missing for $user.givenName) $user.surname)"[/color]

 

as $_ (which contains basic user details from the Get-ADGroupMembers cmdlet), doesn't have a PrimaryCardID, givenName or surname properties but $user should do (I assume).

 

EDIT: What fordea said.

Edited by David44
Posted

So I have made those changes. I now have the name being shown with AD Attributes missing, so that is now working.

 

The problem now is that I get Exception calling "ToInt64" with "2" argument(s): "Additional non-parsable characters are at the end of the string." and in the handful of users in the group I am targetting, they have different PrimaryCardID numbers but are getting populated with the same PAXTONID

Posted

It's telling you there is something wrong with the string $convertedcard. Check what value $convertedcard contains. I expect the problem is with this line...

$convertedcard="$c1 + $c2 + $c3 + $c4"

change it to

$convertedcard="$c1$c2$c3$c4"

as at the moment $convertedcard is going to include the plus signs and you can't convert plus signs to an int.

 

- - - Updated - - -

 

I love a happy ending. What was it?

Posted
It's telling you there is something wrong with the string $convertedcard. Check what value $convertedcard contains. I expect the problem is with this line...
$convertedcard="$c1 + $c2 + $c3 + $c4"

change it to

$convertedcard="$c1$c2$c3$c4"

as at the moment $convertedcard is going to include the plus signs and you can't convert plus signs to an int.

 

- - - Updated - - -

 

I love a happy ending. What was it?

 

Yeah so basically I rewrote that part of the script into one line and then it was happy really.

 

Thanks for the help

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...