Showing posts with label WildCards. Show all posts
Showing posts with label WildCards. Show all posts

Tuesday, November 22, 2011

How to find “Stopped” windows Services, whose start up mode is set to “Automatic” using PowerShell.


Hi,
I called these services as “Problematic services”. The start-up mode of these services are “Automatically” so that whenever windows starts or reboot these services should start automatically.
So logically if any of windows services start mode is set to “Automatic” then the Window Service should be running not “stopped”
we are using WMI query language to find these “Problematic Services”
Cmdlet use: Get-WmiObject, Where-Object , Select-Object, Format-List
we are using Win32_Service WMI call to retrieve the list  and status of the services.
   1: Get-WmiObject win32_Service

above command will show the list of all windows services.

Service_1

Let’s check the all properties of a single “Service”

Get-WmiObject win32_Service  | select -First 1 | fl *

we are piping the result of “Get-WmiObject win32_service” to select-Object cmdlet and choosing -first 1 single Service and then piping further to “Format-List” cmdlet and using * wildcard to show everything.

In below properties we are interested in Name,Status, StartMode, and State property of services.

Service_2
Get-WmiObject win32_Service  | where {$_.StartMode -eq "Auto" -and $_.State -eq "stopped"}


we are piping the previous command to Where-Object cmdlet and filtering to show  only those services whose “StartUp” mode is “Automatic” and “Status” is “Stopped”

oh!!! we have three problematic services. Okkk!!!

Service_3

Let’s filter a little bit more
Get-WmiObject win32_Service  | where {$_.StartMode -eq "Auto" -and $_.State -eq "stopped"} |  Select Name,StartMode,State


now we are piping all the previous command to Select-Object cmdlet and choose to show only Name, StartMode, and State property.

Now it seems little bit formatted.

Now you can start these service manually .

Service_4

Thanks

Aman Dhally

Friday, November 18, 2011

Copy Files and Folders using PowerShell

 

 

Hi,

Copy files and folders this is the task which every Admin or even a end user performs daily.

How we can Copy files and folder using PowerShell?

Quite Easy, we just need to use Copy-Item cmdlet with few Switch Parameters that all.

Cmdlet used : Copy-Item

:::: Copy Files

Lets create a a new folder to Backup files currently I have  one Folder named “Folder_A” which contain Data and we will create a new folder named as “Backup_A” , and then copy data from “Folder_A” to “backup_A”

Files-1

let create a new folder named “Backup_A”

Files-2

Now we have two folders “Folder A” and “Backup_A”

Files-3

now copy a file from “Folder_A” to “backup_A” ,

command is: Copy-Item D:\Demo\Folder_A\Book.txt -Destination D:\Demo\Backup_A

file successfully copied.

Files-4

:::: Copy Txt file only

-Include

if we want to copy specific type of files only then we can use –Include switch Parameter followed by the type .

-Verbose 

We can use –Verbose switch to see which files are getting copied

command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Include *.txt –Verbose

see the screenshot, command copied the .Txt files only

Files-5

:::: Exclude files to copying

-Exclude

If we are using Copy-Item cmdlet in a backup script it would be good we don’t backup Music,Video files on server. As a Ideal Policy we should not fill our SAN, Server storage with the backup of users music and videos.

to exclude these files to be copied use –Exclude switch parameter followed by file types example *.mp3,*.mp4 etc

in my Folder_A i have few MP3 audio and few MP4 video files, lets stop them copying to our Backup_A folder.

Files-6

note: * means all

Command: Copy-Item D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Exclude *.mp3,*.mp4

When i search for *mp3 and  *mp4 no result were found and you can also see there is not a single MP3 and MP4 in “Backup_A” folder.

Files-7

:::: Copy Folders

the command is same like copying file but rather then specify file we need to specify folder

Command : Copy-Item D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\

Folder Copied

Files-8

BUTTTTTTTT !!! wait…!!!!!!!!!!!!!

Check the folder again, Recurse_Testing have some Sub-folders which are not copied in backup_A

Files-9

-Recurse

to Copy All folder including sub-folders use –Recurse while copying

Command is:

Copy-Item -Recurse D:\Demo\Folder_A\Recurse_Testing -Destination D:\Demo\Backup_A\  -Verbose

Files-10

let check if it copied subfolder also.

Seems Identical isn’t.

Files-11

:::: Copy all files and Folders

To copy all file and folder we use * wildcard which means all , and –Recurse to copy sub-directories also and we also use –force do that if there any file exists with same name on backup folder it should get overwrites.

command : Copy-Item -Recurse D:\Demo\Folder_A\* -Destination D:\Demo\Backup_A -Force –Verbose

Files-12

lets check if these two folders are identical.

Seems like TWINS isn't ;-)

Files-13

Hope it is useful to someone .

Thanks

Aman Dhally

Thursday, August 25, 2011

Wildcards in PowerShell

 

Hi Guys,

Before start learning PowerShell first we need to clear or learn some basic logics and basic  steps of scripting, that’s why I decide to learn about wildcards first. So Lets Start

Question: what are wildcards?

Answer: A wildcard character can be used to substitute for any other character or characters in a string.

Question: Which wildcards are available in PowerShell and what are there meaning?

Answer: The wildcards are available in PowerShell are * ? and []

Wildcard Description
* matches Zero or More characters
? match Exactly one character
[a-z] matches a range of characters
[abc] matches specified character

 

Examples

Wildcard *

Lets Run the following command: Dir * it shows all file and folders in current Directory 

 wildcards-1

Let run another command: Dir *.* it will show all files in current directory. *.* means all file with any file extension

wildcards-2

Lets search for all .mp3 files in our current directory.: Dir *.mp3

wildcards-3

Now lets search for all file and folders which starts with B: Dir b* 

Capture4

-----------------------------------------------------------------------------------------------

Wildcard ?

Now let try our another wildcard character ? , in this example we are searching for a .txt file which names end with at and we forget what  is the name: dir ?at.txt   this command return all file which has at.txt at the end.

wildcards-5

Let See the another Example, in this example i am looking for a txt whose name is 3 character long. Dir ???.txt

wildcards-6

Now lets join our both wildcards and search for all .mp3 and .mp4 available in our current directory: dir *.mp?

wildcards-7

-----------------------------------------------------------------------------------------------

Wildcard [] matches specify range of characters

In [] wildcard we can specify a range of characters to match , for example if we specify [a-d] then it means all alphabets between A-D (A,B,C,D) or if we specified [1-8] then it means match all digits between 1-8 (1,2,3,4,5,6,7,8)

Dir: [a-z]at.txt  this command check for all file start with a-z character and at.txt at the end

wildcards-8

In another example lets search for all folder starts whose name start from 1-6

Dir [1-6]

wildcards-9

-----------------------------------------------------------------------------------------------

Wildcard [] matches specify characters

In this wildcard rather then provide a range we specify which characters we want to match. for example we want to match character start with b,c,d then we can specify it as [acd] then it matched only files or folders starts with A,C,D and ignore everything else.

For example: dir [bcl]ook.txt , this command look for all files start with B,C,L and have ook.txt after it

wildcards-10

-----------------------------------------------------------------------------------------------

 

I hope this article helps you to understand Wildcards a little bit :)

Thanks for reading it

Regards

Aman Dhally

 

Note: Dir command is an alias of Get-Childtem in PowerShell