Thursday, April 26, 2012

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 "localhost"))
{
        Get-WmiObject -ErrorAction Stop win32_service -ComputerName $Computername | Select-Object __Server,name,Startmode,State,Startname | Export-Csv myservicestatus.csv -NoTypeInformation
}
    else
{
       
        Get-WmiObject -ErrorAction Stop win32_service -ComputerName $Computername -Credential $user | Select-Object __Server,name,Startmode,State,Startname | Export-Csv myservicestatus.csv -NoTypeInformation
}
}
catch [Exception]
{
  if ($Error[0].Exception.Message -match "Access denied.")
    {
        Write-host "Current windows credential do not allow for access to WMI on $computername. Please run with administrative credentials." 
 
    }
}

No comments:

Post a Comment