Hello, everybody,
I'm still quite new to Powershell and am trying to create a GUI to disable AD users. What I've done so far:
From an input field user names are written into a Datagridview and in the second column it is shown whether the user exists and if so whether he is activated or deactivated.
As a next step I would like to read out the user names of the column "Username" by clicking on a button in order to process them further. And this is exactly where I'm getting stuck. I have read some instructions to similar things but I lack the knowledge to derive from them at my request. Always only how to process selected cells, rows or columns. I don't want to select anything manually but automatically check and process all users in the Username column.
Thanks in advance
Stephen
Select specified Column Cell Value in Datagridview
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Select specified Column Cell Value in Datagridview
Yeah, I found that too, but I just can't figure out how to do it. Maybe I'm just dumb. Do I have to start with foreach?
Of course this code doesn't work but I want to show where I'm not getting any further. $Userstatus and $Aduser are declared above in the code and their result is entered in the columns.
Code: Select all
Foreach (Cell.Value in Datagridview1.Column[Username]) {
if ($Userstatus -eq $true) {
Disable-ADAccount -Identity $_.ADUser}
else { ... }
Re: Select specified Column Cell Value in Datagridview
No. Enumerate "rows" not columns.
Code: Select all
$datagridView1.Rows |
ForEach-Object{
Write-Host $_.Cells['Username'].Value
}
Re: Select specified Column Cell Value in Datagridview
You are my hero! This looks like what I was missing and I think I got the point now and can go on with the next step. Thank you very much!
Re: Select specified Column Cell Value in Datagridview
Hi, it's me again. After a few days I finally had time to work on the script again. So far everything works as desired, but I would also like to check what has been done in a text box.
For users who don't exist this fits so far. But as soon as the user actually exists, I get line breaks and blank lines in the output. So something seems to confuse the output of the variables.
Output:
For users who don't exist this fits so far. But as soon as the user actually exists, I get line breaks and blank lines in the output. So something seems to confuse the output of the variables.
- $buttonDisable_Click={
- #TODO: Place custom script here
- $datagridview1.Rows |
- ForEach-Object {
- $DisableUsername = $_.Cells['Username'].Value
- if ($_.Cells['Accountstatus'].Value -like 'Enabled')
- {
- $richtextbox1.AppendText("$DisableUsername has been disabled `n") | Out-String
- }
- else
- {
- $richtextbox1.AppendText("$DisableUsername already disabled or doesn't exist `n")
- }
- }
- }
Re: Select specified Column Cell Value in Datagridview
The best way to add line to a textbox is this:
$richtextbox1.Lines += "$DisableUsername has been disabled"
If you have line feeds in your original data then fix the original data.
$richtextbox1.Lines += "$DisableUsername has been disabled"
If you have line feeds in your original data then fix the original data.