• Tag Archives Scripting
  • Learning PowerShell

    Don Jones’ keynote basically said, if you do not learn PowerShell, you will not be able to function as an Windows IT administrator in 5 years time or so… After what was told on the MVP summit, about Microsofts commitment to PowerShell, and how all new products will be based on it, he foresees are very dim future for admins who do not know it.

    One of his suggestions was to read a PowerShell help file every day… Just go through the help files one by one, you will most  likely not remember all of it, but if you read enough, some point might stick ;-)

    So on the way home I decided to try to write a little PowerShell script that would display a help file, and automatically store the the number of the helpfile that I have reached. (So I do not have to keep track of it myself)

    This is the most basic version, I came up with it on the plane home from the Forum Meet.

    [int]$HelpNr = Get-Content -Path c:\scripts\test.ini
    $vhelp = Get-Help * | Get-Help
    $i=0
    foreach ($vvhelp in $vhelp){
    $i++
    if ($i -eq $HelpNr) {$vvhelp}
    }
    $HelpNr++
    Set-Content -Path c:\scripts\test.ini -Value $HelpNr


    It has to be said though, that this is hardcoded to look for a file “c:\scripts\test.ini ” which is where it keeps track of the number of help files you have gone through.
    If you are going to test the program, in this version you have to create the .ini file, and just put a 1 in the first line of it.



  • Playing around with profiles.

    In PowerShell you have the option create custom profiles that you can use, in the profiles you can create different aliases, change the prompt and a lot of other things, today we will look at adding some info to prompt. First we need to open profiles.ps1 , this file can be put in the WindowsPowershell folder under System32, or in the users \My Documents\WindowsPowerShell. If you are running vista, and want to play around with your profiles.ps1 file, I would put it in the \My Documents\WindowsPowerShell folder, since you have permission to write to files in that dir. (You can read more about profiles here
    Well ok… on with the coding.. Lets say you want to add some basic info to your prompt.
    When you start up powershell for the first time, your prompt will be defined as

     “PS “ + $(get-location) + ">”

    When you open your profile.ps1 file (There is a template in C:\Windows\System32\WindowsPowerShell\v1.0\Examples\profile.ps1) you will find a function called
    “ prompt”, this is where you will make the following changes.
    The simplest alteration you can make is changing PS to your name

    “Xenophane “ + $(get-location) + ">”
    Will give a prompt like this “Xenophane C:\Users\xen\_”

    What if you want to know something about the system at the prompt, is that possible…. Of course it is…
    Let say you want to know how many processes you are running


    function prompt
    {
    "Processes " + (Get-Process).Count+ " " + $(get-location) + "> "
    }

    Will give you a prompt like this: “ Processes 71 C:\Users\ctn\_”

    But it is very annoying to see that info on the same line as your cursor, so lets add the info on the line over the cursor, and add some more information.


    function prompt
    {
    [wmi]$Global:Cdrive=Get-WmiObject -query "Select * from win32_logicaldisk where deviceid='c:'"
    #Define Cdrive as global variable, which is type cast as a wmi
    $processcount=(Get-Process).Count
    #Define variable processcount as number of processes running
    $cpuload=(Get-WmiObject win32_processor).loadpercentage
    #cpuload is defined as a variable
    $diskinfoC=" Free C: "+"{0:N2}" -f (($global:Cdrive.freespace/1gb)/($global:cdrive.size/1gb)*100)+"%"
    #diskinfoC is defined, {0:N2} is .NET for 2 decimal places, -f is a the "format" property of the string object.
    # Then the amount of free space on drive C is divided by the size of drive C multiplies by 100 to get
    #the percentage of free space.
    $time=Get-WmiObject -class Win32_OperatingSystem
    #Define $time is object Win32_OperatingSystem
    $t=$time.ConvertToDateTime($time.Lastbootuptime)
    #Define #t as the result of the convertion of LastBootuptime into a readable dateTime format.
    [TimeSpan]$uptime=New-TimeSpan $t $(get-date)
    #Typecast the variable $uptime as a [timespan], the timespan cmdlet in powershell is a way to do date arithmetic in ps
    #This way we can compare our variable $t (Which was Lastbootuptime) and the current time Get-date cmdlet
    $up="$($uptime.days)d $($uptime.hours)h $($uptime.minutes)m $($uptime.seconds)s"
    #define variable $up to display the uptime in days/hours/minuted/seconds
    $TextToDisplay="CPU:"+$cpuload+"% Processes:" + $processcount + $diskinfoC+ " " + $diskinfoG+ " "+ ([char]0x25b2)+$up +" "+(Get-Date -format g)
    #define one variable that contains all the data we collected so far.
    Write-Host -fore red $TextToDisplay
    #Write the information to the screen in red
    Write-Host "My PoSh:" $(get-location) -nonewline
    #Display the regular prompt.
    }

    One other thing to be aware of is that PowerShell by default will not let you run scripts, so in order to run scripts like profile.ps1 you need to loosen up the security, to get help you can type the command “get-help about_signing” in a powershell command prompt. I have chosen to use RemotedSigned which will let me run scripts locally on the machine. But you can read about the different security levels in the help file.

    Profile txt file



  • Getting info from the eventlog with PowerShell

    Started playing around with PowerShell, so I thought I would post a few simple things on the blog, maybe you won’t find it interesting, but it is a good way for me to remember, so BLAH!.

    First in order to get information from the eventlog you call the cmdlet called:
    get-eventlog

    Then you need to tell it what you want to get.
    get-eventlog -logname Application

    Which will get you the Application log on the local machine.

    That will list the entire Application eventlog to the screen, which is usually a lot of data. So we want to be able to pick specific data out of the eventlog, and here is a way to do it.

    get-eventlog -logname Application -newest 50 | where {$_.EventID -eq "1"}

    What this does is, it gets the Application eventlog, and gives me an object with the newest 50 entries, i pipe this object into another function called “where”,  I tell “where” to show me all objects where EventID is equal to 1. I this case you see I use $_.EventID, the $_ in powershell represents the object the as being passed into the new function, so in this case $_ represents the newest 50 values in the Application Eventlog.