Rexxer

Some tips for me and other

Powershell

Powershell + Get installed software

get-ciminstance win32_product | select name | sort-object name

Powershell + Mikrotik backups

Source: https://habrahabr.ru/post/351108/ #зададим политику сразу, чтобы не вспминать команду при переносе Set-ExecutionPolicy remotesigned -scope currentuser #установка и импорт библиотеки для ssh Install-Module -Name Posh-SSH Import-Module posh-ssh $curDir = $MyInvocation.MyCommand.Definition | split-path -parent function bkprtr { param($ipaddr) #тут указываем логин и пароль к роутерам с правами на ftp и ssh. $secpasswd = ConvertTo-SecureString “Ваш_пароль_к_роутеру” -AsPlainText -Force […]

Powershell + Users in OU but not in the groups

import-module ActiveDirectory #$groups = ‘group1′,’group2′,’group3’ $groups = ‘mygroup’ $notpresent = @() $ourset = @{} foreach ($group in $groups) { $members = (get-group $group).members foreach ($member in $members) { if ( !$ourset.contains($member.distinguishedname) ) { $ourset.($member.distinguishedname) = $true } } } get-recipient -OrganizationalUnit MyOU | foreach { if ( !$ourset.contains($_.distinguishedname) ) { $notpresent += $_ } } […]

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%