Tuesday, September 17, 2013

More usefull powershell commands

Alternate authentication cmdlet
Sometimes it is pretty useful to be able and use alternate credentials in PowerShell command. This can be done by using Get-Credential cmdlet but a downside is that you can not specify password. Instead what will happen every time this cmdlet is ran it will produce a popup box to enter password.
A nice workaround is to store the credentials in encrypted variable such as $cred = Get-Credential Administrator this will initially pop up with authentication box where password is typed in but afterwards you can use $cred variable in your session without need to type it in again.

Run PowerShell command in background and keep control of your session for other tasks
In the situation where we need to run a cmdlet where output will take a while to be produced we can use -asjob switch. This will give us back our prompt to run additional command while our initial query will run in the background. Some of the related cmdlets with which we can control or view jobs are: Get-Job, Receive-Job, Remove-Job, Resume-Job, Start-Job, Stop-Job, Suspend-Job, Wait-Job

Output text in PowerShell
Write-Output

Input some text into powershell
Read-Host

Convert output of query to HTML
This is specifically usefully if desired output needs to be shared for wide audience. All needs to be done is convert it to HTML using ConvertTo-hmtl and output to file using Out-File to write contents into file on web server and your info is readily available for brain consumption.

Import contents of file into PowerShell for processing
Best example for this would be creating multiple users in AD from predefined csv file. We can use Import-CSV cmdlet to take a contents of csv file and pipe it into another cmdlet such as Import-CSV file.csv | New-ADUser. Please note that header of csv file should contain valid AD properties for user. Whichever properties you have, those properties will be created along with a user.

Tuesday, September 10, 2013

Basic help powershell commands that make life easier

get-help
displays help for desired commandlet

get-help -full
displays full help for desired commandlet including examples

get-help -online
if you have internet access this will display online help for any commandlets desired

update-help
downloads and installs help files from internet

show-command
opens gui for desired commandlet where you can see whole bunch of options and parameters. If desired these parameters can be filled out in gui and copied to run in PowerShell window in order to see exact syntax

get-command
this will list commands that pertain not only to PowerShell but a whole system

get-member
gets the properties and methods of objects. This is very useful to see what custom fields/methods you can use for specific cmdlet




Monday, September 9, 2013

Script to find files in size greater than desired size

Very useful when you need to find large size files on your system. This command can be run locally or on remote system as long as you have admin rights to remote system.


Get-ChildItem \\servername\c$ -Recurse -ErrorAction "SilentlyContinue" | Where-Object {$_.Length -gt 100MB} | Export-Csv c:\temp\myLargeFilesOnC_Report1.csv

Monday, September 2, 2013

Gracefully shutdown VM using PowerCLI

This is very useful for me when I need to power off my lab at home (trying to conserver energy) :)

Prerequisites for this to work is that you have PowerCLI installed on your computer as well as vm tools installed on all vm's that you would like to utilize this on

Power off single machine:

get-vm vm-name | ShutdownVMGuest

Power off group of machines:

Create a text file with list of vm's you would like to shut down with each vm in separate line such as
vm1
vm2
vm3
Let say this file is called servers.txt and is located in c:\users\mike\desktop
First we will create variable to load servers from text files:
$servers = Get-Content -Path c:\users\mike\desktop\servers.txt
second we will use previous command to shut down group of servers in the file:
get-vm $servers | Shutdown-VMGuest

Turn on group of machines: (This please be careful with as if you try to turn on too many machines at one time you could potentially overload your ESX box)

Start-VM -VM $servers