Search This Blog

Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Thursday, 31 August 2017

Export unique event log in text file using Microsoft.PowerShell

Example 1: Get event logs on the local computer

In the below give below result

  • Get the ten most recent entries from a specific event log on the local computer, This command gets the ten most recent entries from the Application event log.
  • Get error, warning events from a specific event log. E.g. "Error"
  • Get all errors in an event log that occurred during a specific time frame
  • Get the unique entries from a specific event log on the local computer.
  • Get the MachineName, Source, EntryType, Message properties from entries from a specific event log on the local computer.
  • Export the EventLog in text file to specific server location.

Script

$AfterDate = (Get-Date -Date '26/8/2017');
$Message = Get-EventLog  -LogName "Application" -EntryType Error, Warning -Newest 10  -Source "BizTalk Server" -After $AfterDate -ComputerName "localhost" 
$Message = $Message | Format-List -Property MachineName, Source, EntryType, Message 
$Message = $Message | select -uniq;
$Message >> \\ServerName\c$\Users\harshal.chaudhari\SystemErrors.txt

Result


MachineName : ServerName
Source      : BizTalk Server
EntryType   : Warning
Message     : An error occurred that requires the BizTalk service to terminate. The most common causes are the 
              following:
               1) An unexpected out of memory error.
               OR
               2) An inability to connect or a loss of connectivity to one of the BizTalk databases. 
               The service will shutdown and auto-restart in 1 minute. If the problematic database remains 
              unavailable, this cycle will repeat.

Rerefence



Thursday, 17 August 2017

Identify if IIS is Running on a Server

PowerShell Script

Identify IIS is running on Remote server.

$vm = "localhost"
$iis = get-wmiobject Win32_Service -ComputerName $vm -Filter "name='IISADMIN'"
if($iis.State -eq "Running")
{
 Write-Host "IIS is running on $vm"
}
else
{
 Write-Host "IIS is not running on $vm"
}

Friday, 28 July 2017

PowerShell Run as Administrator

Script

Start-Process powershell -Verb runAs

DSC Powershell Examples

Create File using DSC PowerShell Script - Part III

PowerShell DSC is one of my favorite topics to teach, because the technology is simply amazing. Usually I do not have enough time with a customer to teach all of the built-in resources. I would guess that the Script resource is one of the least understood.This sample has tested on PSVersion 4.0.

Step 1. Script Resource Log File Example

Here is an elegantly simple use of the Script resource. We have send Node (Server) Name and file contents as parameter to DSC Script. The code is its own documentation. Study it carefully.

configuration WinLogAutoGen
{ 
    param(
    
        [parameter(Mandatory=$true)]
        [string]    $Server,

        [parameter(Mandatory=$true)]
        [string]    $Contents
    )

    #import the module of DSC for using DSCResources.
    Import-DscResource -ModuleName PSDesiredStateConfiguration;

    node $Server
    {
        # use the file resource to create file.
        File windowsupdatelog{
            DestinationPath  = 'C:\windows\DynamicParameterDemo.log';
            Ensure = 'Present';
            Contents = $Contents ; 
        }
    }
 
}

#Create mof file
WinLogAutoGen -OutputPath D:\dsc-mof


#Apply mof File
Start-DscConfiguration -Wait -Verbose -Force -Path D:\dsc-mof

Notice the Write-Verbose statements. These are important for logging and output when the resource runs.

Step 2. Output



Step 3. Verify Output

Step for Verification.
  1. Connect to Server.
  2. Verify the content of DynamicParameterDemo.log file. PowerShell Command as below

Get-Content C:\Windows\DynamicParameterDemo.log

Note: We can verify the mof file content as well, PowerShell Command as below

Get-Content D:\dsc-mof\localhost.mof
IMP : Target nodes(Server) can apply only one MOF file.

Reference

Your Turn

Now it is your turn. Take this sample and implement your own script resources. Leave a comment below telling what you accomplished.

Best wishes on your adventure learning DSC!

Create File using DSC PowerShell Script - Part I

PowerShell DSC is one of my favorite topics to teach, because the technology is simply amazing. Usually I do not have enough time with a customer to teach all of the built-in resources. I would guess that the Script resource is one of the least understood.This sample has tested on PSVersion 4.0.

Step 1. Script Resource Log File Example

Here is an elegantly simple use of the Script resource. We could make it more complex in many ways, but I prefer to start out with a simple use case first. The code is its own documentation. Study it carefully.

configuration WinLogAutoGen{
    param()
    #import the module of DSC for using DSCResources.
    Import-DscResource -ModuleName PSDesiredStateConfiguration;
    # use the file resource to create file.
    File windowsupdatelog{
        DestinationPath  = 'C:\Windows\windowsupdate2.log';
        Ensure = 'Present';
        Contents = 'Dev';
    }
 }
#Set mof file path in other word where to generate mof file.
WinLogAutoGen -Outputpath D:\Harshal;
#Run mof file in other word apply mof configuration to server.
Start-DscConfiguration -Wait -Verbose -Force -path D:\Harshal;

Notice the Write-Verbose statements. These are important for logging and output when the resource runs.

Step 2. Output



Reference

Your Turn

Now it is your turn. Take this sample and implement your own script resources. Leave a comment below telling what you accomplished.

Best wishes on your adventure learning DSC!

PowerShell Export-CSV

Script

Get-Service | Export-CSV c:\service.csv

Thursday, 27 July 2017

Windows PowerShell Useful Commands

Starting in Windows PowerShell 5.0, results of the Get-Command cmdlet display a Version column by default. A new Version property has been added to the CommandInfo class.

Commands using Get-Command

Command Description
Get-Help Displays information about Windows PowerShell commands and concepts.

Command Prompt Vs Windows Powershell

CMD PowerShell
Taskbar menu. Typing "cmd" Taskbar menu. Typing "powershell"
CMD is legacy in other word CMD is old. PowerShell as a shell is clearly intended as a replacement for CMD.
CMD does not run PowerShell commands PowerShell does run or use CMD commands.
The command shell is a separate software program that provides direct communication between the user and the operating system. PowerShell was actually built as several things: A mature , extensible automation platform and a modern administration shell. Windows PowerShell is a Windows command-line shell designed especially for system administrator.
CMD is used to create and edit batch (.bat) files (also called scripts). PowerShell is used to create and edit script (.ps1) files.

Creating a NuGet Package Feed to Host Artifacts

Step-by-Step Guide: Creating a NuGet Package Feed to Host Artifacts 🔹 Step 1: Create a C# Class Library and Generate NuG...

Recent Post