Rexxer

Some tips for me and other

Powershell

Powershell + Replace user’s manager

$userobj = Get-ADObject -SearchBase “DC=local, DC=com” -Filter {objectClass -eq “user” -and objectCategory -eq “person” -and manager -eq “CN=manager,DC=local,DC=com”} -Properties name, manager, department foreach ($user in $userobj) { Set-ADObject -Identity $user -replace @{“manager”=”CN=manager2,DC=local,DC=com”} write-host $user.name, $user.manager }

Powershell: send and read email

In your Gmail account we have to turn the feature “Less secure app” on. To read mail I used the fine tool for PS: https://github.com/nikoblag/Gmail.ps Script example: #============================================================== # Mail function global:Send-Email ($recipient,$SSubject,$BBody) { $email = “your.mail@gmail.com” $pass = “password” $smtpServer = “smtp.gmail.com” $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $email, $($pass | ConvertTo-SecureString -AsPlainText -Force) $From […]

Powershell + MySQL

Interesting script and MySQL connector for powershell: Script: Param( [Parameter( Mandatory = $true, ParameterSetName = ”, ValueFromPipeline = $true)] [string]$Query ) $MySQLAdminUserName = ‘username’ $MySQLAdminPassword = ‘password’ $MySQLDatabase = ‘MySQL-DB’ $MySQLHost = ‘MySQL-Host’ $ConnectionString = “server=” + $MySQLHost + “;port=3306;uid=” + $MySQLAdminUserName + “;pwd=” + $MySQLAdminPassword + “;database=”+$MySQLDatabase Try { [void][System.Reflection.Assembly]::LoadWithPartialName(“MySql.Data”) $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection […]

Powershell + Get users from specified groups

$Groups = “ip”,”team”,”Managment”,”mott” ForEach ($Group in $Groups) { write-host “#” write-host “Group:” $Group write-host “===============================” $gr = Get-ADGroupMember -identity $group echo $gr.name write-host “===============================” }

Powershell + Logoff inactive users

We have shared PCs and some people leave their sessions locked. Their audio processes (Skype, players, etc …) still work and it’s the problem. I wrote the script which runs at a user logon and logoff these inactive users. function Get-Sessions { $queryResults = query session $starters = New-Object psobject -Property @{“SessionName” = 0; “UserName” […]

Powershell + Time elapsed

$sw = [Diagnostics.Stopwatch]::StartNew() ping -l 10 192.168.2.2 $sw.Stop() $sw.Elapsed $res = “Elapsed: ” + $sw.Elapsed.Hours + ” hours ” + $sw.Elapsed.Minutes + ” minutes ” + $sw.Elapsed.Seconds + ” seconds” echo $res

Powershell + turn on system protection

enable-computerrestore -drive “c:\” vssadmin resize shadowstorage /on=c: /for=c: /maxsize=3%