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 ?

Pierre.Vriens
  • 1,159
  • 34
  • 15
  • 19
user879
  • 267
  • 2
  • 7
  • 21
  • 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`? – Andrew Domaszek Mar 30 '17 at 12:28

2 Answers2

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"}
jscott
  • 24,484
  • 8
  • 79
  • 100
  • You rockstar!! It worked... I feel im learning fast.. faster. Really appreciate your kind help.. – user879 Mar 30 '17 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"}
Aravinda
  • 1,101
  • 5
  • 12
  • 30