Powershell Script Help

Ok, perfect.. That worked great! Last question (I hope).. How can I specify multiple OU's in one script that it will scan instead of having several different scripts with one OU listed?
 
At home so can't test it but give that a shot, just add a semi-colon whenever you want to start the next OU, e.g:

-OUPath 'OU=A,dc=corp,dc=org;OU=B,dc=corp,dc=org'
 

Attachments

  • Detect-LocalAdmin.ps1.txt
    5.5 KB · Views: 3
Awesome, thank you! What about specifying to scan multiple OU's rather than having just one OU per script so I don't have to run several different scripts? We have about 5 different OU's.
 
Awesome, thank you! What about specifying to scan multiple OU's rather than having just one OU per script so I don't have to run several different scripts? We have about 5 different OU's.
Apparently I like posting duplicate posts :p I will check this out tomorrow when I am at work. Thanks again man!
 
Are you using single or double quotes when you specify the OUs?

If using different quotes doesn't help, just go into the script and change the line that says:

$OUList = $OUPath -split ';'

Just change the ; to whatever symbol you want that won't be used (e.g. | or # or ^) and try again using that symbol as the new delimiter. e.g. if you change it to ^ then use that symbol as your new OU separator in place of the ;
 
Hmm.. not sure if that is working correctly. Here's what I listed out:
[string]$OUList = $OUPath -split ';' 'ou=desktops,ou=resources,ou=mkt,ou=ls,ou=amr,ou=ia,dc=corp,dc=org';'ou=laptops,ou=resources,ou=mkt,ou=ls,ou=amr,ou=ia,dc=corp,dc=org';
But that doesn't appear to be functioning correctly. I receive an error : The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.
+ CategoryInfo : ParserError: :)) [], ParseException
+ FullyQualifiedErrorId : MissingEndParenthesisInFunctionParameterList
 
Wait, how are you entering your OU info when you run the script?

You need to enclose the whole thing in a single set of quotes. e.g.

"OU=A,dc=corp,dc=org;OU=B,dc=corp,dc=org"
 
It still doesn't appear to be working. I've tried messing around with the OUList and OUPath variable but it's not liking it. Here's what I have listed out:
#[Parameter(Mandatory)]
[string]$OUList = $OUPath -split ';' 'ou=desktops,ou=resources,ou=mkt,ou=ls,ou=amr,ou=ia,dc=corp,dc=org;ou=laptops,ou=resources,ou=mkt,ou=ls,ou=amr,ou=ia,dc=corp,dc=org',
[string]$LogFilePath = 'C:\Powershell\LocalAdmins\Log\desktop_log.log',
[string]$SmtpServer = 'server@domain.com',
[string]$EmailFrom = 'from@domain.com,
[string]$EmailSubject = 'New Local Administrator Detected',
[string]$EmailRecipient = 'email@domain.com'

When I try to run the script, it keeps saying "The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property."
 
Are you entering that string in the script itself? That's what's going wrong if so.

If you want to hard-code it in the script you'd do it like in the attached. I've removed the [Mandatory] tags, so now all you need to do is follow these steps to run successfully:

1. Change the [string]$Server = 'DC01.corp.org' line in the script to reflect your actual DC (line 25)
2. (if required) change the email parameters in the script to whatever they should be to (lines 27-30)

After doing that, you should be able to just run the script without specifying any other parameters. E.g. from the powershell prompt, type ".\Detect-LocalAdmin.ps1" like so:
Code:
PS C:\Users\s0ul> .\Detect-LocalAdmin.ps1

And that should be it.
 

Attachments

  • Detect-LocalAdmin.ps1.txt
    5.6 KB · Views: 1
Back
Top Bottom