5

I have created a small script to transfer value of Active directory pc attribute called pssfulllocation from one pc to anoter pc..

To achieve this I have to use typecasting + splitting..

Here is the script which works

$oldpc=read-host "Enter old pc name"
$newpc=read-host "Enter new pc name"
$my=Get-ADComputer -Identity $oldpc -Properties * | select pssfulllocation 

  $itemcast=[string]$my
  $b = $itemcast.Split("=")[1]    
  $c=$b.Split("}")[0]

Set-ADComputer -identity $newpc -Replace @{pSSFullLocation="$c"}

and the output will nicely do the intended job.. in this manner.. and this is the desired result..

enter image description here

But if i don't use typecasting + splitting as per the below script--

$oldpc=read-host "Enter old pc name"
$newpc=read-host "Enter new pc name"
$my=Get-ADComputer -Identity $oldpc -Properties * | select pssfulllocation 
Set-ADComputer -identity $newpc -Replace @{pSSFullLocation="$my"}

the out put is below.. which is not what i want..

enter image description here

In a nutshell if i don't use typecasting + splitting output will be added as @{pSSFullLocation=C/BRU/B/0/ADM/1 but it should only be added as C/BRU/B/0/ADM/1 as per this:

enter image description here

I feel typecasting + splitting should be a workaround and not the proper method.. Any other powershell way to achieve this without using typecasting + splitting ?

1
  • Have you checked what Get-ADComputer -Identity $oldpc -Properties pSSFullLocation | Get-Member returns? Perhaps you want something like (Get-ADComputer -Identity $oldpc -Properties pSSFullLocation).somethingreturnedbyget-member? Mar 30, 2017 at 12:28

2 Answers 2

5

Don't do this:

$my=Get-ADComputer -Identity $oldpc -Properties * | select pssfulllocation 

You don't Select-Object to access an attribute's value. Note the -Object, not -Attribute, in the cmdlet name. Instead, collect the object returned by Get-AdComputer then use the attribute directly.

$my = Get-ADComputer -Identity $oldpc -Properties *
$psfl = $my.pSSFullLocation
Set-ADComputer -identity $newpc -Replace @{pSSFullLocation="$psfl"}
1
  • You rockstar!! It worked... I feel im learning fast.. faster. Really appreciate your kind help..
    – user879
    Mar 30, 2017 at 14:10
1

Or even this could do the job "-expandproperty"

$oldpc=read-host "Enter old pc name"
$newpc=read-host "Enter new pc name"

$my=Get-ADComputer -Identity $oldpc -Properties * | select -expandproperty pssfulllocation 

Set-ADComputer -identity $newpc -Replace @{pSSFullLocation="$my"}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .