I am creating a script that will schedule the removal and addition of existing members in a particular group. I am able to create the new tasks, but I run into an error when launching the arguments.
I have no idea how to add an argument to a powershell script.
You really want to declare a Param() block versus using $args. You then use parameters exactly the same as with any other PowerShell command, either providing the -parameterName or listing arguments positionally.
I added the param block to the add and remove scripts. But when I run the command I receive the following error:
C:\WINDOWS\system32>Powershell.exe -File c:\ps\remove.ps1 -user1 'joe schmoe'
Remove-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\ps\remove.ps1:6 char:44
+ Remove-ADGroupMember "Users" $username
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADG
roupMember
It looks as if the argument is now passing to the script, but something in the script isn’t using the $username variable the way I think it should work. Any suggestions?
I think you’d be better off using named parameters with all of your commands, rather than positional ones. That’s considered a best practice.
You need to validate whether $username has anything in it - I suspect it doesn’t based on your error.
You might also consider “Learn PowerShell Scripting in a Month of Lunches.” You’re starting down a kind of bad path in how you’re coding and it’d help get you in a better place, and help you understand the shells debugging tools for problems like this one.
$username has nothing in it. When I test the command outside of the script and use write-host to verify the variables it shows nothing. If I was to replace the $user1 variable with a name the command runs without issue. So it would seem that $user1 is the broken part of the script.
I have the book sitting on my desk, I just havn’t opened it yet. Haven’t had time. I guess now is a good time to start.
I moved the $username variable process over to the “Vacation.ps1” script. This allowed me to bypass that process after the fact.
Now the issue I have is being able to schedule the tasks to run without being logged in.