Hi Brothers & Sisters,
Please help- I am trying to disable bunch of computers from a text file but stuck at a point where I need to concatenate system name with a “$” sign. Below is the script:
Not sure what you are doing with the +$ there, but it’s not needed or desired. Your foreach loop is setting $psitem to the current computer identity from the computer.txt file input. You should just need that variable, no concatenation necessary assuming that your computer.txt input file contains valid computer identities.
Guess the objects within $Computer are getting treated as simple text, that is why it is getting executed successfully after concatenating $ symbol which is then treated as a computer object.
Please refer to below error while script execution using only $PSItem. I am also copying the code which was successfully executed after adding $ to the computer name.
Disable-ADAccount : Cannot find an object with identity: ‘4584XP-LT’ under: ‘DC=sccm,DC=com’.
At C:\Users\Administrator\Desktop\Move-Computer.ps1:4 char:5
Ah, the error explains it. Based on the error you provided, your input file does not contain a valid Identity for your computer accounts.
Disable-ADAccount accepts the following for the Identity parameter
-Identity
Specifies an Active Directory account object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute.
Distinguished Name
Example: CN=SaraDavis ,CN=Users,DC=corp,DC=contoso,DC=com
GUID (objectGUID)
Example: 599c3d2e-f72d-4d20-8a88-030d99495f20
Security Identifier (objectSid)
Example: S-1-5-21-3165297888-301567370-576410423-1103
SAM Account Name (sAMAccountName)
Example: saradavis
You are using, for example, 4584XP-LT in your input file; however, in AD computer objects sAMAccountName do not look like that. The sAMAccountName always ends with a $. That is why appending $ to then end of your input value works. By doing so you are making it a valid sAMAccountName to be used as the Identity parameter.
Ok, so on the concatenation, you just need to not use the + sign inside of your “”. “” will automatically evaluate variables, so just put your variable inside of “” with the $ at the end like below.