I've got the task to write a script in PowerShell to read out if a User has the Logon script "SLOGIC". With this code I'm able to get the information for a single User:
Write-Host "Checking for logon scripts."
$User = Read-Host -Prompt "Input the username"
Write-Host "Checking for logon script."
$ScriptPath = Get-ADUser -Identity $User -Properties Scriptpath | Select ScriptPath
If ($ScriptPath) {
Write-Host "$User'has a logon script"
Write-host $ScriptPath
} Else {
Write-Host "$User' does not have a logon script"
}
The code spits out this:
Checking for logon scripts.
Input the username: u002533
Checking for logon script.
u002533'has a logon script
@{ScriptPath=SLOGIC}
I need a command to give me a list of all Users, because I have to check this with over 500 Users. (and I don't want to do this with 500 Users manually)
Can anyone help me?
CodePudding user response:
Use the -Filter
or -LDAPFilter
parameter(s) with Get-ADUser
to retrieve only the ones that has the given term in their ScriptPath
attribute:
$scriptPathFilter = '*SLOGIC*'
Get-ADUser -Filter {scriptPath -like $scriptPathFilter} -Properties ScriptPath |ForEach-Object {
Write-Host "User '$($_.SAMAccountName)' has logon script '$($_.ScriptPath)'"
}
CodePudding user response:
I actually found a diffrent answer to my Problem. I used this Script:
import-module activedirectory Get-ADUser -filter {(scriptpath -like "SLOGIC")}