简体   繁体   中英

PowerShell - Use variable in another cmdlet

I want to create a variable with the office phone number of an AD user and use this variable in another cmdlet.

$a = Get-AdUser -Filter "name -eq 'User1'" -Properties OfficePhone | FT OfficePhone | Out-String
Set-ADUser User2 -EmailAddress $a

I tried this one, but it doesn't work. Can someone help me?

Best regards

$a = (Get-ADuser User1 -Properties OfficePhone).OfficePhone
Set-ADUser User2 -EmailAddress $a

The -Properties parameter of Get-ADUser tells the cmdlet to include the properties you specify in addition to the other properties it already includes, such as DistinguishedName,Enabled,GivenName, etc.

Because of this you have to specify which property you want saved to the $a variable by wrapping the cmdlet in parentheses and using a period followed by the property name. This is actually the same thing as:

$a = Get-ADUser User1 -Properties OfficePhone
$b = $a.OfficePhone
Set-ADUser User2 -EmailAddress $b

You want to select the specific property of the User1 object, in this case OfficePhone. Please try that and let me know if it works as I don't currently have the Active Directory modules installed.

$a = Get-AdUser -Filter "name -eq 'User1'" -Properties OfficePhone | Select-Object OfficePhone
Set-ADUser User2 -EmailAddress $a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM