3 administrative script
Need to revive the public, so a little administrative scripts.
1. To network settings and configs domain computers
2. To change the default gateway on domain computers
3. To obtain statistics on mailboxes
the
Bypasses the domain computers and collects information. The output is thrown the object. The computer must be switched on of course. The script contains an example of the work via WMI. To run the domain.
the
the
Modify the previous script so that it changed the default gateway. (be sure to check everything in your environment before running it on a live system!) Uses to change the gateway calls via WMI because of PowerShell need to delete and re-create the adapter that will lead to the loss of communication with the computer. Tested on Powershell 4 and 3, 2012 and win 8.
the
the
Collects data on top of boxes by size, the top drawers on the number of emails top drawers for remote eiteman (purge 2 basket has not yet occurred, but the deletion of emails by the user), after the forms "beautiful" the report in HTML and sends the mail and put it in daddy. Used as a monthly reporter of the state of the database established in task sheduler. To run on the exchange server. Tested on Exchange 2010.
the
All on the holidays.
Article based on information from habrahabr.ru
1. To network settings and configs domain computers
2. To change the default gateway on domain computers
3. To obtain statistics on mailboxes
the
1. To network settings and configs domain components.
Bypasses the domain computers and collects information. The output is thrown the object. The computer must be switched on of course. The script contains an example of the work via WMI. To run the domain.
the
# the requested creditably domain admin
$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) {
#get information via 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.The netconnectionid key
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
the
2. To change the default gateway
Modify the previous script so that it changed the default gateway. (be sure to check everything in your environment before running it on a live system!) Uses to change the gateway calls via WMI because of PowerShell need to delete and re-create the adapter that will lead to the loss of communication with the computer. Tested on Powershell 4 and 3, 2012 and win 8.
the
$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.The netconnectionid key
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
################################################################
# Change the gateway
foreach($Co in $CompData) {
if ($Co.NetDefaultGateway = $OldGateway) {
$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)
}
}
}
}
the
3. To obtain statistics on mailboxes
Collects data on top of boxes by size, the top drawers on the number of emails top drawers for remote eiteman (purge 2 basket has not yet occurred, but the deletion of emails by the user), after the forms "beautiful" the report in HTML and sends the mail and put it in daddy. Used as a monthly reporter of the state of the database established in task sheduler. To run on the exchange server. Tested on Exchange 2010.
the
<#
by default chooses the top 25 mailboxes
data saving the report to disk is disabled
#>
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
# the thickest boxes
$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
}
# top remote no 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 for the number of letters, too many indicator that the user dumps everything in a pile and need automatic rules
$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
}
##### The HTML generating
Write-Verbose 'producing HTML fragment'
$fragTopItems = $TopItems | ConvertTo-Html -As table-Fragment-PreContent '<h2>Top boxes for the number of letters (items count)</h2>' | Out-String
$fragTopSizes = $Size | ConvertTo-Html -As table-Fragment-PreContent '<h2>Top mailboxes by size</h2>' | Out-String
$fragTopDelItems = $Del | ConvertTo-Html -As table-Fragment-PreContent '<h2>Top drawers for remote messages (the purge was not yet)</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 'producing HTML'
$Date = Get-Date
if ($SaveReport = $true) {
$rep = ConvertTo-HTML -head $head-PostContent $fragTopSizes, $fragTopDelItems, $fragTopItems -PreContent "<h1>Report mailbox $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>Report mailbox $Date</h1>" | Out-String
Write-Verbose "Sending e-mail"
$params = @{'To'=$ReportMail1
'From'='bot@geomex.local'
'Subject'="Mailbox report for $Date"
'Body'=$Body
'BodyAsHTML'=$True
'SMTPServer'='mail.domain.the local'}
Send-MailMessage @params -Encoding $encoding
All on the holidays.