Replacing property with variable

%3CLINGO-SUB%20id%3D%22lingo-sub-910365%22%20slang%3D%22en-US%22%3EReplacing%20property%20with%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-910365%22%20slang%3D%22en-US%22%3E%3CDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EHi%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EI've%20made%20a%20CustomPSObject%20with%20many%20properties%20(36%20for%20now)%20for%20new%20AD%20User.%20I%20want%20to%20pass%20through%20all%20of%20those%20and%20apply%20them%20in%20order%20to%20update%20a%20user%20information.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EI%20was%20wondering%20if%20it's%20possible%20to%20replace%20the%20property%20name%20following%20the%20%22-%22%20with%20a%20variable.%20Here's%20a%20part%20of%20my%20script%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%24UserObjectProperty%20%3D%20%24UserObject%7C%20Get-Member%20-MemberType%20NoteProperty%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%24UserObjectProperty%20%7C%20ForEach-Object%7B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%24PropertyToCheck%20%3D%20%24_.name%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%24NewPropertyValue%20%3D%20%24UserObject.(%24_.name)%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%24PreviousInfo%20%3D%20Get-ADUser%20%24UserObject.SamAccountName%20-Properties%20*%20%7CSelect-Object%20%24PropertyToCheck%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3ESet-Aduser%20%24UserObject.SamAccountName%20-%24PropertyToCheck%20%22%24NewPropertyValue%22%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%7D%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3ESo%20%24PropertyToCheck%20can%20be%20anything%20like%20Description%20or%20Title%20or%20Manager%20for%20example.%20I'm%20able%20to%20get%20the%20PreviousInfo%20for%20logging%20purpose%20fine.%20It's%20crashing%20in%20the%20last%20step%20for%20now.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EIs%20there%20a%20way%20to%20avoid%20having%20to%20put%20each%20different%20property%20with%20it's%20own%20line%20of%20Set-ADUser%3F%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EHoping%20I'm%20clear%20enough%20to%20start%20with.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22public-DraftStyleDefault-block%20public-DraftStyleDefault-ltr%22%3E%3CSPAN%3EThanks%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-910365%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-910726%22%20slang%3D%22en-US%22%3ERe%3A%20Replacing%20property%20with%20variable%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-910726%22%20slang%3D%22en-US%22%3E%3CP%3EJust%20create%20a%20simple%20function%20that%20accepts%20a%20parameter%20for%20the%20property%20name%20and%20another%20one%20for%20the%20value%2C%20then%20pass%20them%20along%20to%20the%20function.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor
Hi,
I've made a CustomPSObject with many properties (36 for now) for new AD User. I want to pass through all of those and apply them in order to update a user information.
 
I was wondering if it's possible to replace the property name following the "-" with a variable. Here's a part of my script:
 
$UserObjectProperty = $UserObject| Get-Member -MemberType NoteProperty
$UserObjectProperty | ForEach-Object{
$PropertyToCheck = $_.name
$NewPropertyValue = $UserObject.($_.name)
$PreviousInfo = Get-ADUser $UserObject.SamAccountName -Properties * |Select-Object $PropertyToCheck
Set-Aduser $UserObject.SamAccountName -$PropertyToCheck "$NewPropertyValue"
}
 
So $PropertyToCheck can be anything like Description or Title or Manager for example. I'm able to get the PreviousInfo for logging purpose fine. It's crashing in the last step for now.
Is there a way to avoid having to put each different property with it's own line of Set-ADUser?
 
Hoping I'm clear enough to start with.
Thanks,
1 Reply
Highlighted

Just create a simple function that accepts a parameter for the property name and another one for the value, then pass them along to the function.