Posts

Showing posts from April, 2012

Scripting Games 2012 Advanced Event 10

Advanced Event 10 :

<#
.DESCRIPTION
    This script creates csv log file for Processor counter set every 2 seconds (10 snapshots).
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/13/the-2012-scripting-games-advanced-event-10-create-a-csv-log-file.aspx
#>
param (
[string[]]$Computers=$env:computername
)
foreach ($computer in $computers)
{
    $filepath = $env:userprofile + "\Documents\" + $Computer + "_processorCounters.csv"
        Get-Counter -ListSet processor -ComputerName $Computer |
        Get-Counter -ComputerName $Computer -SampleInterval 2 -MaxSamples 10 |
        Export-Counter -path $filepath -FileFormat CSV -Force
}

Scripting Games 2012 Advanced Event 9

Advanced Event 9 :


<#
.DESCRIPTION
    This script perform inventory and export data to xml.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/12/the-2012-scripting-games-advanced-event-9-perform-an-inventory.aspx
#>

function Create-XML
{
Param ([Parameter(Mandatory=$true,
   ValueFromPipeline=$true)]$object)

@"
<ComputerInfo>
"@
    foreach ($p in $object |Get-Member -type *Property )
    {
        $Name  = $p.Name
        $Value = $Object.$Name   
@"
`t<$Name>$Value</$Name>`n
"@
     }
@"
</ComputerInfo>
"@
}

function HumanReadSize
{
    Param ([long]$Size)
if ($Size -ge 1Gb )
{
return ("{0:N2}" -f ($Size / 1Gb) + " GigaBytes")
}
elseif ($Size -ge 1Mb )
{
return ("{0:N2}" -f ($Size / 1Mb ) + " MegaBytes")
}
elseif ($Size -ge 1Kb )
{
return ("{0:N2}" -f ($Size / 1Kb ) + " KiloBytes")
}
elseif (($Size -ge 0 ) -and ($Size -lt 1Kb))
{
return ("{…

Scripting Games 2012 Advanced Event 8

Advanced Event 8 :

<#
.DESCRIPTION
    This script can enable and (or) disable wireless or ethernet adapter to prevent bridging on laptop computers.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/11/the-2012-scripting-games-advanced-event-8-enable-network-adapters.aspx
#>

function get-netadapter
{
  param ([string]$nettype= "%", $isEnabled=$null)
    if ($isenabled -eq $null)
    {
      $filter ="name like '$nettype'"
    }
    else
    {
      $filter ="name like '$nettype' and netenabled=$isenabled"
    }   
   return Get-WmiObject win32_networkadapter -Filter $filter
}

function enableadapter
{
  param ($enable=$true, $neta, $prev=$false)
    #enable adapters
    if ($enable)
    {
      if ($neta.count -gt 1)
      {

         foreach ($adapter in $neta)
         {
            do
            {
                $choice = Read-Host $adapter.description  " Enable Yes/No ?"
            }
            until…

Scripting Games 2012 Advanced Event 7

Advanced Event 7 :

<#
.DESCRIPTION
    This script reports latest events from all event logs.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/10/the-2012-scripting-games-advanced-event-7-search-windows-logs.aspx
#>
$obj=@()
Get-WinEvent -ea silentlycontinue -ListLog * -Force | where { $_.isEnabled -eq $true } `
         | foreach {
                        if (!(($_.logtype -eq "analytical") -or ($_.logtype -eq "debug")))
                        {

                            $obj += (Get-WinEvent -ea silentlycontinue -LogName $_.logname -MaxEvents 1 `
                                                     | Select-Object TimeCreated,Logname,ID,Message)  

                        }
                        else
                        {
                            $debevents=@()
                            $debevents = (Get-WinEvent -ea silentlycontinue -LogName $_.logname -Force -Oldest `
                                                    | Sele…

Scripting Games 2012 Advanced Event 6

Advanced Event 6 :

<#
.DESCRIPTION
    This script reports information about multiple servers uptime.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/09/2012-scripting-games-advanced-event-6-compute-uptime-for-multiple-servers.aspx
.PARAMETER Computers
    Name of the remote computer, if omitted localhost will be used.
.EXAMPLE
    event6.ps1
    To collect information about error events from local computer, run the script without parameters.
.EXAMPLE
    event6.ps1 "computer1","computer2"
    To collect information about error events on remote computers.
#>

param (
[string[]]$Computers=$env:computername
)
[datetime]$Reptime=(Get-Date -UFormat %D).tostring() + " 08:00:00"
Foreach ($server in $computers)
{
    $os= Get-WmiObject -ea silentlycontinue Win32_OperatingSystem -ComputerName $server
    #check if $os is not null
    if (!($os -eq $null))
    {
       $reboot =$os.ConvertToDateTime($os.LastBootUpTime)

        $obj  = New-Obj…

Scripting Games 2012 Advanced Event 5

Advanced Event 5:

<#
.DESCRIPTION
    This script reports information about error events on local and remote computers.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/06/2012-scripting-games-advanced-event-5-list-errors.aspx
.PARAMETER Computers
    Name of the remote computer, if omitted localhost will be used.
.EXAMPLE
    event5.ps1
    To collect information about error events from local computer, run the script without parameters.
.EXAMPLE
    event5.ps1 "computer1","computer2"
    To collect information about error events on remote computers.
#>
param (
[string[]]$Computers=$env:computername
)

    $identity  = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object System.Security.Principal.WindowsPrincipal( $identity )
if (!($principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator )))
{
    write-host "Warning: You're not running this script using elevated shell. You wil…

Scripting Games 2012 Advanced Event 4

Advanced Event 4:


function HumanReadSize
{
    Param ([long]$Size)
if ($Size -ge 1Gb )
{
return ("{0:N2}" -f ($Size / 1Gb) + " GigaBytes")
}
elseif ($Size -ge 1Mb )
{
return ("{0:N2}" -f ($Size / 1Mb ) + " MegaBytes")
}
elseif ($Size -ge 1Kb )
{
return ("{0:N2}" -f ($Size / 1Kb ) + " KiloBytes")
}
elseif (($Size -ge 0 ) -and ($Size -lt 1Kb))
{
return ("{0:N0}" -f ($Size) + " Bytes")
}
}

function Get-FolderSize {
<#
.SYNOPSIS
    This script reports information about folder space.
.DESCRIPTION
    This script reports information about folder space.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/05/the-2012-scripting-games-advanced-event-4-determine-folder-space.aspx
.PARAMETER Path
    Path of the root folder.
.EXAMPLE
    "c:\windows"| get-foldersize 
.EXAMPLE
    get-foldersize "c:\windows" | Sort-Object Folder
#>

[CmdletBinding()]
param([Parameter(Mandatory =…

Scripting Games 2012 Advanced Event 3

Advanced Event 3:

<#
.DESCRIPTION
    This script create log information that updates information about OS,User ...
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/04/2012-scripting-games-advanced-event-3-create-a-log-that-updates.aspx
#>
$os= Get-WmiObject Win32_OperatingSystem
$comp = Get-WmiObject win32_ComputerSystem
$net = Get-WmiObject Win32_NetworkLoginProfile | sort -descending LastLogon | select * -first 1
$prn = Get-WmiObject win32_printer -Filter "Default=True"
$mappeddrives=Get-WmiObject win32_logicaldisk -filter "DriveType=4" | `
              Select-Object @{Name="Drive Letter";Expression={$_.DeviceID}}, `
                            @{Name="Resource Path";Expression= {$_.ProviderName}}
$output = New-Object PSObject
$output | Add-Member noteproperty LastReboot ($os.ConvertToDateTime($os.LastBootUpTime))
$output | Add-Member noteproperty ComputerName (($comp.DNSHostname) + "." + ($comp.Domain))
$outp…

Scripting Games 2012 Advanced Event 2

Advanced Event 2:

<#
.DESCRIPTION
    This script reports information about remote and local services.
.LINK
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/03/2012-scripting-games-advanced-event-2-find-information-about-remote-and-local-services.aspx
.PARAMETER Computername
    Name of the remote computer, if omitted localhost will be used.
.PARAMETER User
    User account in form of "domain\user" with administrative permissions.
.EXAMPLE
    event2.ps1
    To collect information about local services from local computer, run the script without parameters.
.EXAMPLE
    event2.ps1 computer1 domain1\administrator
    To collect information about services running on remote computer, run the script with remote comuter name ex.Computer1 and username with administrative credential
#>
##
# Start of Script
##
param (
[string]$Computername=$env:computername,
[string]$User
)
try
{
    If (($Computername -match $env:computername) -or ($Computername -match "localh…

Scripting Games 2012 Advanced Event 1

Scripting Games 2012 are almost over, only waiting for judges to grade submitted scripts. I'll publish all of mine scripts for advanced events. Here is my script for advanced event 1:

http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/02/the-2012-scripting-games-advanced-event-1-review-a-coworker-s-script.aspx

for ($i = 1 ; $i -le 10 ; $i++)

{
start-sleep 1
$notepad = Get-Process notepad -erroraction silentlycontinue
$notepad
}

The processing of Group Policy failed. Event ID 1096

In my case there was a machine with bad sectors on hard disk and group policy settings were failing to apply. In System Event Log there was error event id 1096 :

The processing of Group Policy failed. Windows could not apply the registry-based policy settings for the Group Policy object LocalGPO. Group Policy settings will not be resolved until this event is resolved. View the event details for more information on the file name and path that caused the failure.
After imaging the hard disk with new one (bad sector free), group policy settings were still failing to apply. Since this machine had bad sectors, there was a problem with local policy file. So, after renaming registry.pol into registry.bak from c:\Windows\System32\GroupPolicy\Machine, and refreshing the policy with gpupdate /force, group policy settings started to apply and error event 1096 was history from System event log.

2012 Scripting Games

Image
If you missed the first week, there is one more week left for submitting scripts. Join the fun !