32 or 64 bit version of product: 64
Operating system: Win10
32 or 64 bit OS: 64
Hey everybody, so I'm scratching at the heels of an intermediate powershell competency, but I enjoy it. I was finally able to get PShell Studio and I'm coding an app just to learn my way around and I'm stuck. I simply won't abandon it, I have to know how to do this.
Anyway, my app polls the AD for stale users or computers and populates a data grid with info (I'm showing this for context and on the off chance someone would like to tell me how to make it better), like so:
- $buttonDG_Click={
- #TODO: Place custom script here
- $ResultsDGV01.Columns.Clear()
- #$ResultsDGV01.Rows.Clear()
- $ResultsDGV01.Refresh()
- $ResultsDGV01.AutoSizeColumnsMode = 'Fill'
- $filterDate = $datetimepicker1.Value.DateTime
- $buttonDG.Text = 'Processing...'
- $buttonDG.Enabled = $False
- If ($CommandCB.SelectedItem -eq 'AD Stale Users Report') {
- $UserListLLD = Get-ADUser -Filter * -SearchBase $domain -Properties CN, UserPrincipalName, lastLogonDate, whenCreated, logoncount, LogonWorkstations | ? {
- $_.lastLogonDate -le $filterdate
- } | Sort-Object CN
- $dt = New-Object System.Data.DataTable
- $dt.Columns.Add('User Name')
- $dt.Columns.Add('User Principal Name')
- $dt.Columns.Add('Last Logon Date')
- $dt.Columns.Add('When Created')
- $dt.Columns.Add('Logon Count')
- $dt.Columns.Add('Logon Workstations')
- $ResultsDGV01.DataSource = $dt
- $progressbar1.Maximum = $UserListLLD.count
- $progressbar1.Step = 1
- $progressbar1.Value = 0
- ForEach ($User In $UserListLLD) {
- $r = $dt.NewRow()
- $r['User Name'] = $User.cn
- $r['User Principal Name'] = $User.UserPrincipalName
- $r['Last Logon Date'] = $User.lastlogondate
- $r['When Created'] = $User.whencreated
- $r['Logon Count'] = $User.logoncount
- $r['Logon Workstations'] = $User.logonworkstations
- $dt.Rows.Add($r)
- $ResultsDGV01.DataSource = $dt
- $progressbar1.PerformStep()
- #Start-Sleep -Milliseconds .8
- }
- $labelResultsCount.Text = $($UserListLLD.Count)
- }
- ElseIf ($CommandCB.SelectedItem -eq 'AD Stale Computers Report') {
- $CompListLLD = Get-AdComputer -LDAPFilter "(name=*)" -SearchBase $domain -Properties Name, Lastlogondate | Where-Object {
- $_.lastlogondate -le $filterDate
- } | Sort-Object Name
- $dt = New-Object System.Data.DataTable
- $dt.Columns.Add('Computer Name')
- $dt.Columns.Add('Last Logon Date')
- $ResultsDGV01.DataSource = $dt
- $progressbar1.Maximum = $CompListLLD.count
- $progressbar1.Step = 1
- $progressbar1.Value = 0
- ForEach ($Comp In $CompListLLD) {
- $r = $dt.NewRow()
- $r['Computer Name'] = $Comp.Name
- $r['Last Logon Date'] = $Comp.lastlogondate
- $dt.Rows.Add($r)
- $ResultsDGV01.DataSource = $dt
- $progressbar1.PerformStep()
- #Start-Sleep -Milliseconds 5
- }
- $labelResultsCount.text = $($CompListLLD.Count)
- #$datagridview1.DataSource | export-csv c:\Users\aruss\documents\datagrid.csv -NoTypeInformation
- }
- elseif ($CommandCB.SelectedItem -eq 'Test') {
- $ResultsDGV01.DataSource = Get-AdComputer -LDAPFilter "(name=*)" -SearchBase $domain -Properties Name, Lastlogondate
- }
- $buttonDG.Enabled = $True
- $buttonDG.Text = "Show In DG"
- }
- $ResultsDGV01_Click={
- #TODO: Place custom script here
- [void][System.Windows.Forms.MessageBox]::Show($ResultsDGV01.SelectedCells[0].FormattedValue,)
- }
I have tried the following as well:
- $Script:Test = $ResultsDGV01.CurrentRow.Cells["cn"].Value.ToString()
- _________________________________________________________________________
- $TBDeletedUser = $datagridview1.SelectedRows.Cells[3].Value
- ___________________________________________________________________________
- $TBDeletedUser = $datagridview1.Rows[$_.RowIndex].Cells[0].value
Thank you for your time.