How to run powershell script against domain? - powershell

I am trying to run the following powershell command through my application using C#
Get-ADUserResultantPasswordPolicy user1
It returns the values for user1 on domain1 which is my current domain.
when I try to read the values for a user2 on domain2:
Get-ADUserResultantPasswordPolicy domain2\user2
its throwing exception
"Cannot find an object with identity:'user2' under:'DC=domain2,DC=com'.
Is there away to point powershell to the other domains and read the data on that domain?

Use the -partition parameter:
Specifies the distinguished name of an Active Directory partition. The distinguished name must be one of the naming contexts on the current directory server. The cmdlet searches this partition to find the object defined by the Identity parameter.
The following two examples show how to specify a value for this parameter.
-Partition "CN=Configuration,DC=EUROPE,DC=TEST,DC=CONTOSO,DC=COM"
-Partition "CN=Schema,CN=Configuration,DC=EUROPE,DC=TEST,DC=CONTOSO,DC=COM"
Read more here: https://technet.microsoft.com/en-us/library/ee617255.aspx?f=255&MSPPError=-2147217396

You can use the -Server parameter with the fully qualified domain name of the domain controller on domain you want to access.
Get-ADUserResultantPasswordPolicy -Identity "USER1" -Server "DC1.YourDomain.com"

Related

Set default server in powershell for AD related commands

Recently my powershell scripts require to explicitly say which domain I want to connect to. Is it necessary to write this for each command? Or can I set it somehow once in the beginning of the script.
Instead of
Get-ADUser -Server server otherparameters
could I write in the beginning something like
Set-default server to connect to
?
Is it necessary to write this for each command?
No!
You can specify a default parameter value for a parameter belonging to one or more cmdlets by assigning it to the $PSDefaultParameterValues automatic variable:
$PSDefaultParameterValues['*-AD*:Server'] = 'mydc.mydomain.tld'
Any cmdlet you subsequently invoke that matches the *-AD* pattern by name and has a -Server parameter will now implicitly have 'mydc.mydomain.tld' bound to the -Server parameter unless an argument is explicitly passed.
In other words: next time you invoke Get-ADUser rsterba, PowerShell now calls Get-ADUser rsterba -Server 'mydc.mydomain.tld' instead.
For more information about $PSDefaultParameterValues and how it works, see the about_Parameters_Default_Values help topic

Get-Help shows different outputs on different systems

When I use the following cmdlet:
Get-Help Get-ADUser -Parameter identity
On Windows 7 with RSAT installed connecting to Windows Server 2012 R2, I get the following output:
-Identity <ADUser>
Specifies an Active Directory user 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=Europe,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
The cmdlet searches the default naming context or partition to find the object. If two or more objects are found
the cmdlet returns a non-terminating error.
This parameter can also get this object through the pipeline or you can set this parameter to an object instance
This example shows how to set the parameter to a distinguished name.
-Identity "CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com"
This example shows how to set this parameter to a user object instance named "userInstance".
-Identity $userInstance
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
However when I use it on a Windows Server 2012 R2 or 2016 with WMF 5.1 installed I only get the following:
-Identity <ADUser>
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
Any idea what I'm doing wrong?
Get-ADUser command comes from a 'system module' that can be different from an OS to an other even with the same version of WMF and even if the version number shown by 'Get-Command Get-ADUser' is the same (1.0.0.0) ... so help content may be different too.
By the way, I get this result on my Windows 2012 R2 with WMF 5.1
-Identity <ADUser>
Specifies an Active Directory user object by providing one of the following property values. The identifier in
parentheses is the LDAP display name for the attribute. The acceptable values for this parameter are:
-- A Distinguished Name
-- A GUID (objectGUID)
-- A Security Identifier (objectSid)
-- A SAM Account Name (sAMAccountName)
The cmdlet searches the default naming context or partition to find the object. If two or more objects are found,
the cmdlet returns a non-terminating error.
This parameter can also get this object through the pipeline or you can set this parameter to an object instance.
Required? true
Position? 1
Default value
Accept pipeline input? True (ByValue)
Accept wildcard characters? false
You may try Update-Help to download latest PowerShell help files (if your server is connected to Internet...).

Get-ADForest - Could not find a forest identified by: "xxxx"

Here's the command I run:
$Credentials = Get-Credential # Feed in credentials for some.domain.com
Get-ADForest -Server some.domain.com -Credential $Credentials
And it reports:
Get-ADForest : Could not find a forest identified by: 'some.domain.com'
I've tried all kinds of different things for the -Server parameter, including using the hostname.domain.name.com and nothing works. Is it possible it is being blocked?
However, if I run a:
Get-ADUser username -Server 'some.domain.com' -Credential $Credentials
It returns the value...
First of all, you need to go through the documentation for Get-ADForest to understand what might be happening in your case:
The Get-ADForest cmdlet gets the Active Directory forest specified by
the parameters. You can specify the forest by setting the Identity or
Current parameters..........
To retrieve the forest of the local computer or current logged on user
(CLU), set the Current parameter to LocalComputer or LoggedOnUser. When
you set the Current parameter, you do not need to set the Identity
parameter.......
When the Current parameter is set to LocalComputer or LoggedOnUser,
the cmdlet uses the Server and Credential parameter values to
determine the domain and the credentials to use to identify the domain
of the forest according to the following rules......
-- If the Server and Credential parameters are specified:
The domain is set to the domain of the specified server and the cmdlet
checks to make sure that the server is in the domain of the
LocalComputer or LoggedOnUser. Then the credentials specified by the
Credential parameter are used to get the domain. An error is
returned when the server is not in the domain of the LocalComputer or
LoggedOnUser.
I believe the error which you're receiving is because the last line is not being satisfied. Please verify the same in your case because it seems the server which you're specifying is either not in the domain of the LocalComputer or the LoggedOnUser.
First of all, the two examples you mention, are two different CMDlets. Let's not confuse the two of them.
About Get-ADForest:
Judging upon the information from this fact sheet about Get-ADForest
Would it help you to use -Identity "some.domain.com" instead? From what i can gather, either you use the -Identity switch(which is mandatory), or you pipe an object to the CMDlet.
The Identity switch takes these values:
A fully qualified domain
A GUID (objectGUID)
A DNS host name
A NetBIOS name
Test:
Get-ADForest -Identity 'some.domain.com' -Credential $Credentials

Specify domain controller with get-aduser in powershell

Get-ADUser -identity $ntaccount1 -properties name, samaccountname, mail, enabled, passwordlastset
Is it possible, when looking up the user account information in powershell, to specify a domain controller to use? We have some DC's that get the data faster than others.
From Get-Help Get-ADUser -Parameter *
-Server <string>
Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a
corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain
Services, Active Directory Domain Services or Active Directory Snapshot instance.
Domain name values:
Fully qualified domain name
Examples: corp.contoso.com
NetBIOS name
Example: CORP
Directory server values:
Fully qualified directory server name
Example: corp-DC12.corp.contoso.com
NetBIOS name
Example: corp-DC12
Fully qualified directory server name and port
Example: corp-DC12.corp.contoso.com:3268
The default value for the Server parameter is determined by one of the following methods in the order that they are listed:
-By using Server value from objects passed through the pipeline.
-By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive.
-By using the domain of the computer running Powershell.
The following example shows how to specify a full qualified domain name as the parameter value.
-Server "corp.contoso.com"
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
I know that this is a bit of an old question, but I would like to expand on the answer given, to aid anyone else who had a similar query.
The following allows you to define a specific Domain Controller, which the entire of a script would be able to use... Why might you want to do this when the -server parameter is available to Get-ADUser, New-ADUser, Set-ADObject, etc?
Well I put together a script that creates an AD user, sets multiple properties and creates an exchange mailbox - However, one set of properties revolves around the RDS properties on a 2008 R2 user account, which cannot be set from within New-ADUser. I had to create a function that calls ADSI and uses psbase.invokeSet to update the settings. There is no parameter setting for -server that I'm aware of.
This in itself wouldn't be a big deal, but the script also creates an Exchange mailbox for the user. As my Exchange server is in different AD Site from my workstation, the user account gets created on my local DC, but the mailbox isn't set, because the DC in the same site as the Exchange server hasn't yet received a replicated copy of the new user account.
The solution I found is as follows and is courtesy of http://www.joseph-streeter.com/?p=799
Having loaded import-module activedirectory, you'll have access to AD options in the New-PSDrive commandlet which among everything else allows you to define a new Active Directory Provider to work with.
New-PSDrive -Name <<NameofYourChoice>> -PSProvider ActiveDirectory -Server <<DC Server>> -Root "//RootDSE/" -Scope Global
Once created, you can then change the working Provider with the following command.
CD <<NameofYourChoice>>:
To view the existing list of Providers, type Get-PSDrive. AD is the default Active Directory Provider created when using the ActiveDirectory commandlet. You should also see your newly created Provider.
So for instance if my remote DC is called RemoteDC I would run:
New-PSDrive -Name RemoteAD -PSProvider ActiveDirectory -Server RemoteDC -Root "//RootDSE/" -Scope Global
to create a new Provider called RemoteAD. If I then run:
CD RemoteAD:
All further active directory related commands in the script or the active shell will work with the new Provider RemoteAD. If I would need to change back to my original Provider, I'd simply type
CD AD:
Hope someone finds this useful...
This is what i use:
Get-ADUser -server dcservername.domain.local -identity username

Weird Quest PowerShell error

"Move-QADObject : 0000202B: RefErr: DSID-031007EF, data 0, 1 access points"
I get this error when I try the following:
Move-QADObject -identity $results_ep.dn -NewParentContainer "OU=Users,OU=AB,DC=domain,DC=local" -Credential $cred_ep
I am running this script from domain A, and the target domain where the object should be moving is domain B (hence the credentials).
Anyone know whats up?
The default tree in Active Directory is not OU=Users,dc=domain,dc=com but rather cn=Users,dc=domain,dc=com ( CN= not the OU= for Users).
Try change OU=Users with CN=Users
EDIT:
Reading better your question I see just now that you want move object to a different domain,
from get-help move-qadobject:
... omissis ...
Use this cmdlet to move an object between containers within an Active Directory domain (the cmdlet cannot move an object to a different domain).
... omissis ...
Read here to do it with Quest.ActiveRoles.ADManagement sanp-in.
If you are in a Windows Server 2008 R2 DC you can move in a different domain using Windows PowerShell module named Active Directory:
Move-ADObject -Identity "CN=Mary Vick,OU=Accounting,DC=SS64,DC=com" -TargetPath "OU=Accounting,DC=Europe,DC=SS64,DC=com" -TargetServer "server64.europe.SS64.com"
Better of all I think is using native Microsoft commandline like ADMT