Variable within quotes fails - powershell

I am using a variable in PowerShell to get Active Directory info.
The variable does not get the value I need in double quotes. What am I doing wrong?
The variable display correctly but in the Move-AdObject cmdlet the value
is $CN and not tok12.
Here is the code:
$CN = "tok12"
$company = "acme"
$CN
$company
Get-ADGroup $CN | Set-ADGroup -Description $company
Start-Sleep -s 1
Move-ADObject -Identity "CN=$CN,OU=UnassignedPortalTokens,OU=Portal,OU=testdomain,DC=testdomain,DC=com" -TargetPath "OU=Customers,OU=testdomain,DC=testdomain,DC=com"
Error message:
Get-ADGroup : Cannot find an object with identity: 'Kentest' under:
'DC=testdomain,DC=com'.
At C:\PSH\Newport.ps1:13 char:2
+ Get-ADGroup $company | Set-ADGroup -Description "$token"
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Kentest:ADGroup) [Get-ADGroup], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'Kentest' under: 'DC=testdomain,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADGroup

Question remains quite unclear, but OP seems to have fixed the issue by himself:
(...) one of the commands was a wrong group. The other issue was the
variable was null and I had to remove and set it again from command
line – Gary Seven

Related

Get-aduser issue

hope you are doing well
I normally use this script to return the value of extensionattribute1
$a = read-host "enter badge"
get-aduser -Filter {extensionattribute1 -eq $a } -Properties * -server "Server" | format-list extensionattribute1,Title,AccountExpirationDate,DistinguishedName,SamAccountName,enabled,Description,Orginizations,extensionattributeies
and i get the results. but sometimes i get this strange error
get-aduser : Object reference not set to an instance of an object.
At line:2 char:1
get-aduser -Filter {extensionattribute1 -eq $a } -Properties * -serve ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Get-ADUser], NullReferenceException
FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
normally it will be solved after a restart, but I want to fix it without restarting if possible.
thanks

Update user AD attributes from CSV file using powershell

I'm trying to update AD user attributes:title, physicalDeliveryOfficeName and department from CSV file using powershell. I am new to powershell so I need some help. (please, and thanks in advance)
So, the idea is that filter for a match is displayName attribute and the script I use is:
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physicalDeliveryOfficeName $($csv.physicalDeliveryOfficeName) }
But the script is returning error:
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
+ Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
+ Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
You're using the wrong variable to assign those values. In your script $csv contains the entire .csv file, but within your foreach line you're loading each individual line into $line, and it's that which you need to set your value.
So in essence, $($csv.title) equals ALL of the title fields, not just the one you're interested in. That's why you're getting the error saying it's a System.Object[] rather than System.String... because it's not one single value.
I notice you have it correct on the "$displayName = $line.displayName" line, so it's just the Set-ADUser line that needs updating, as :
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }
Edit:
Also, with something like this a useful trick is to add some Write-Output lines to show what the variable contents are, so you can check if they match what you're expecting, for instance :
Write-Output $($line.title)
which then in the case of $line.title should show you a single value, whereas if you did $($csv.title) it would return multiple values and show you where the issue is.
You code seems good for me except the set-aduser you need to replace $csv with $line
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }

PowerShell - problem with substring in a pipeline

I am trying to replace users description with a substring of his description. I want it to be just the first 10 letters. I try like this:
Get-ADUser abc -Properties description | Set-ADUser -Description "($($PSItem.Description).substring(0,10))"
Can you give me a hint how to make it work?
You never mentioned in what way it doesn't work for you but I assume it's because your SubString method never gets called but instead gets interpreted as text in your string. Try changing your line to the following instead and see if it does what you expect.
You could try it out first by just writing the output to screen rather than (potentially) updating your AD object with the wrong value.
Get-ADUser -abc -Properties Description | foreach { Write-Output "$($PSItem.Description.SubString(0,10))" }
And then run your line once you've made sure you have what you need.
Get-ADUser -abc -Properties Description | Set-ADUser -Description "$($PSItem.Description.SubString(0,10))"
this one gives good output:
Get-ADUser abc -Properties Description | foreach { Write-Output "$($PSItem.Description.SubString(0,10))" }
But this one not:
Get-ADUser abc -Properties Description | Set-ADUser -Description "$($PSItem.Description.SubString(0,10))"
it is giving error like this:
You cannot call a method on a null-valued expression. At line:1
char:71
+ ... on | Set-ADUser -description "$($PSItem.description.SubString(0,10))"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull Set-ADUser : replace At line:1 char:44
+ ... scription | Set-ADUser -description "$($PSItem.description.SubString( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (CN=abc...C=DOMAIN,DC=com:ADUser) [Set-ADUser],
ADInvalidOperationException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Listing information for all computers in an OU using PowerShell

For a lab working on PowerShell, I have to target a specific OU and list the following information in a text file.
DistinguishedName
DNSHostName
Enabled
Name
ObjectClass
ObjectGUID
SamAccountName
SID
UserPrincipleName
I've found a ton of resources online on how to do this and continuously get an error no matter how I format it.
Here is my code:
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
$Computers = Get-ADComputer -Filter '*' -SearchBase $ou
$Computers | foreach {
$_.DNSHostName
} | Out-File -Filepath "C:\Windows\Temp\Lab7.txt"
I continuously get this error no matter what syntax I use:
Get-ADComputer : The object name has bad syntax
At line:1 char:1
+ Get-ADComputer -Filter '*' -SearchBase 'OU=Testing,OU=Labs,OU=UWEC Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADComputer], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8335,Microsoft.ActiveDirectory.Management.Commands.GetADComputer
The code you posted does not match the error you posted. However, the most likely reason for the error is a missing comma in your OU:
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers DC=uwec, DC=edu'
# ^right here
Change that into
$ou = 'OU=Testing,OU=Labs,OU=UWEC Computers,DC=uwec, DC=edu'
and the problem should disappear.

How can I change the display name in Active Directory with powershell?

How can I change the Name attribute in Active Directory in powershell?
I would like to change the 'Name' row, but when I enter the following I get an error:
Set-ADuser -Identity test1 -Name Test 11
The error message:
Set-ADUser : A parameter cannot be found that matches parameter name 'Name'.
At line:1 char:28
+ Set-ADUser -Identity test1 -Name Test 11
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
You should use the cmdlet Rename-ADObject to change the name attribute of the AD object.
If you want to change multiple properties for an account in one go (say changing a users name), add the PassThru param to Set-AdUser and then pipe to Rename-ADObject:
Set-ADUser -Identity "test1" -DisplayName "DisplayName" -GivenName "GivenName" -Surname "Surname" -PassThru | Rename-ADObject -NewName "TestAccount1" -PassThru