📜 ⬆️ ⬇️

3 administrative scripts

It is necessary to revive the public, so some administrative scripts.

1. Get network settings and configs of domain computers
2. Change the default gateway on domain computers
3. Get mailbox statistics

1. Get network settings and configs of domain computers.


Bypasses domain computers and collects information from them. At the exit of the full object is thrown. The computer must be turned on of course. The script contains an example of working through WMI. Run from domain.
')
#     $cred = Get-Credential <anchor>habracut</anchor> $Clients = Get-ADComputer -Filter * | Sort-Object name | Select-Object name $CompData = @() foreach ($cli in $Clients) { if (( Test-Connection $cli.Name -Count 2 -Quiet ) -eq 'True') { if ($cli.name -ne $env:COMPUTERNAME) { #   WMI $os = Get-WmiObject -ComputerName $cli.name Win32_OperatingSystem -Credential $cred $net = Get-WmiObject -Class Win32_NetWorkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $cli.name -Credential $cred $nic = Get-WmiObject -Class Win32_NetWorkAdapter -ComputerName $cli.name -Credential $cred | where {$_.adaptertype -eq 'Ethernet 802.3'} | where { $_.NetEnabled -eq 'True'} $disk = Get-WmiObject -Class Win32_DiskDrive -ComputerName $cli.name -Credential $cred $props = [ordered]@{ Name=$os.CSName OSName=$os.Caption OSSN=$os.SerialNumber OSVersion=$os.Version OSSystemDirectory=$os.SystemDirectory OSBuild=$os.BuildNumber OSMUI=$os.MUILanguages OSBootDevice=$os.BootDevice NICNetConnectionID=$nic.NetConnectionID NICName=$nic.Name NICDeviceID=$nic.DeviceID NICAdapterType=$nic.AdapterType NICIndex=$nic.Index NICInterfaceIndex=$nic.InterfaceIndex NICMAC=$nic.MACAddress NICManufacturer=$nic.Manufacturer NICNetEnabled=$nic.NetEnabled NICPhysical=$nic.PhysicalAdapter NICProductName=$nic.ProductName NICServiceName=$nic.ServiceName NICSpeed=$nic.Speed NetDHCPEnabled=$net.DHCPEnabled NetIPAddress=$net.IPAddress NetDefaultGateway=$net.DefaultIPGateway DiskName=$disk.Caption DiskStatus=$disk.Status DiskDeviceId=$disk.deviceid DiskSerialNumber=$disk.serialnumber DiskSize=$disk.size } $obj = New-Object -TypeName PSObject -Property $props $CompData += $obj } } } $CompData 

2. Change the default gateway


We modify the previous script so that it changes the default gateway. (Be sure to check everything in your environment before running it on a live system!) It uses WMI calls to change the gateway. through Povercell, you need to remove and re-create the adapter, which will lead to loss of communication with the computer. Tested on Powershell 4 and 3, win 2012 and 8.

 $cred = Get-Credential $OldGateway = '192.168.30.1' $NewGateway = '192.168.30.50' $Clients = Get-ADComputer -Filter * | Sort-Object name | Select-Object name $CompData = @() foreach ($cli in $Clients) { if (( Test-Connection $cli.Name -Count 2 -Quiet ) -eq 'True') { if ($cli.name -ne $env:COMPUTERNAME) { $os = Get-WmiObject -ComputerName $cli.name Win32_OperatingSystem -Credential $cred $net = Get-WmiObject -Class Win32_NetWorkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $cli.name -Credential $cred $nic = Get-WmiObject -Class Win32_NetWorkAdapter -ComputerName $cli.name -Credential $cred | where {$_.adaptertype -eq 'Ethernet 802.3'} | where { $_.NetEnabled -eq 'True'} $disk = Get-WmiObject -Class Win32_DiskDrive -ComputerName $cli.name -Credential $cred $props = [ordered]@{ Name=$os.CSName OSName=$os.Caption OSSN=$os.SerialNumber OSVersion=$os.Version OSSystemDirectory=$os.SystemDirectory OSBuild=$os.BuildNumber OSMUI=$os.MUILanguages OSBootDevice=$os.BootDevice NICNetConnectionID=$nic.NetConnectionID NICName=$nic.Name NICDeviceID=$nic.DeviceID NICAdapterType=$nic.AdapterType NICIndex=$nic.Index NICInterfaceIndex=$nic.InterfaceIndex NICMAC=$nic.MACAddress NICManufacturer=$nic.Manufacturer NICNetEnabled=$nic.NetEnabled NICPhysical=$nic.PhysicalAdapter NICProductName=$nic.ProductName NICServiceName=$nic.ServiceName NICSpeed=$nic.Speed NetDHCPEnabled=$net.DHCPEnabled NetIPAddress=$net.IPAddress NetDefaultGateway=$net.DefaultIPGateway DiskName=$disk.Caption DiskStatus=$disk.Status DiskDeviceId=$disk.deviceid DiskSerialNumber=$disk.serialnumber DiskSize=$disk.size } $obj = New-Object -TypeName PSObject -Property $props $CompData += $obj } } } #$CompData ################################################################ #   foreach($Co in $CompData) { if ($Co.NetDefaultGateway = $OldGateway) { $Co.Name $Co.netdefaultgateway $NWCards = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $co.Name -Credential $cred | where{ $_.ipenabled -eq $true } foreach ($nwc in $NWCards) { $gateway = $nwc.defaultipgateway if ($gateway -eq $OldGateway) { $nwc.setgateways($NewGateway) } } } } 

3. Get mailbox statistics


Collects statistics of top boxes by size, top boxes by the number of letters, top boxes by deleted items (purge basket 2 has not yet happened, just deleting letters by the user), then it forms a “beautiful” HTML report and sends it to the mail and adds it to the daddy. It was used as a monthly reporter of the state of the base, installed in the task scheduler. Run on the exchange server. Tested on Exchange 2010.

 <#     25        #> Param ( [int]$TopCount = 25, [boolean]$SaveReport = $false, [string]$ReportPath = 'C:\Reports\MailboxReport.html', [boolean]$SendReport = $true, [string]$ReportMail1 = 'admin@mail.local' ) # check snapin present $EX2010 = $false $snaps = Get-PSSnapin foreach( $snap in $snaps ) { if ($snap.Name -eq 'Microsoft.Exchange.Management.PowerShell.E2010') { $EX2010 = $true } } if ($EX2010 -eq $false) { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 } $boxes = Get-Mailbox | Get-MailboxStatistics | sort TotalItemSize -Descending $TotalSize = $boxes | sort TotalItemSize -Descending | select -First $TopCount #    $Size = @() foreach ($box in $TotalSize) { $propsTotalSize = [ordered]@{ DisplayName=$box.DisplayName User=$box.LastLoggedOnUserAccount Database=$box.Database TotalSize=$box.TotalItemSize ItemCount=$box.ItemCount DeletedItemCount=$box.DeletedItemCount TotalDeletedSize=$box.TotalDeletedItemSize } $obj = New-Object -TypeName PSObject -Property $propsTotalSize $Size += $obj } #    purge $Deleted = $boxes | sort TotalDeletedItemSize -Descending | select -First $TopCount $Del = @() foreach ($box in $Deleted) { $propsDeleted = [ordered]@{ DisplayName=$box.DisplayName User=$box.LastLoggedOnUserAccount Database=$box.Database TotalSize=$box.TotalItemSize ItemCount=$box.ItemCount DeletedItemCount=$box.DeletedItemCount TotalDeletedSize=$box.TotalDeletedItemSize } $obj = New-Object -TypeName PSObject -Property $propsDeleted $Del += $obj } #    ,               $Top = $boxes | sort ItemCount -Descending | select -First $TopCount $TopItems = @() foreach ($box in $Top) { $propsTopItems = [ordered]@{ DisplayName=$box.DisplayName User=$box.LastLoggedOnUserAccount Database=$box.Database TotalSize=$box.TotalItemSize ItemCount=$box.ItemCount DeletedItemCount=$box.DeletedItemCount TotalDeletedSize=$box.TotalDeletedItemSize } $obj = New-Object -TypeName PSObject -Property $propsTopItems $TopItems += $obj } ##### HTML generating Write-Verbose 'HTML fragment producing' $fragTopItems = $TopItems | ConvertTo-Html -As table -Fragment -PreContent '<h2>     (items count)</h2>' | Out-String $fragTopSizes = $Size | ConvertTo-Html -As table -Fragment -PreContent '<h2>   </h2>' | Out-String $fragTopDelItems = $Del | ConvertTo-Html -As table -Fragment -PreContent '<h2>     (purge   )</h2>' | Out-String Write-Verbose 'definiting CSS' $head = @' <style> body { background-color:#ffffff; font-family:Tahoma; font-size:12pt; } td, th { border:1px solid black; border-collapse:collapse; } th { color:white; background-color:black; } table, tr, td, th { padding: 2px; margin: 0px } table { font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif; font-size: 14px; border-radius: 10px; border-spacing: 0; text-align: center; } th { background: #BCEBDD; color: white; text-shadow: 0 1px 1px #2D2020; padding: 10px 20px; } th, td { border-style: solid; border-width: 0 1px 1px 0; border-color: white; } th:first-child, td:first-child { text-align: left; } th:first-child { border-top-left-radius: 10px; } th:last-child { border-top-right-radius: 10px; border-right: none; } td { padding: 10px 20px; background: #F8E391; } tr:last-child td:first-child { border-radius: 0 0 0 10px; } tr:last-child td:last-child { border-radius: 0 0 10px 0; } tr td:last-child { border-right: none; } </style> '@ Write-Verbose 'HTML producing' $Date = Get-Date if ($SaveReport = $true) { $rep = ConvertTo-HTML -head $head -PostContent $fragTopSizes, $fragTopDelItems, $fragTopItems -PreContent "<h1>    $Date</h1>" | Out-String $rep | Out-File $ReportPath } # Sending Email to admins if ($SendReport = $true) { $encoding = [System.Text.Encoding]::UTF8 $body = ConvertTo-HTML -head $head -PostContent $fragTopSizes, $fragTopDelItems, $fragTopItems -PreContent "<h1>    $Date</h1>" | Out-String Write-Verbose "Sending e-mail" $params = @{'To'=$ReportMail1 'From'='bot@geomex.local' 'Subject'="Mailbox report $Date" 'Body'=$Body 'BodyAsHTML'=$True 'SMTPServer'='mail.domain.local'} Send-MailMessage @params -Encoding $encoding } 

All with the May holidays.

Source: https://habr.com/ru/post/327980/


All Articles