posted 03/01/10

Get-ComputerType

By Fredrik Wall

I’m working on lots of Active Directory functions at the moment.
For a larger Active Directory script.

And one of the functions will get the computer type in clear text.

image

What does this have to do with Active Directory management do
ask your self right now :)

Lots of, do I say!

If you need your computers to be named like sitenamecomputertype0001
then this function will do a lot for you.

image

Just do like this:

 $compType = Get-ComputerType -computerName $computerName
 $compType = $compType.substring(0,1)
And you got an D a M or a W.
 
The function can be found here.
 
1 Comment
read more
posted 26/12/09

Updated functions

By Fredrik Wall

I have been away from my computer for a couple of days.
And I have been thinking about my code and now are
these functions updated.

Clean-Temp

I have tweaked the code.
No need for the same code twice :)

And I have added help for PowerShell 2.0.

When the function are loaded.
Try to write Get-Help Clean-Temp –detailed

Get-DotNetInfo

Uses the default browser instead of Internet Explorer.

And I have added help for PowerShell 2.0.

When the function are loaded.
Try to write Get-Help Get-DotNetInfo –detailed

More updates will be posted soon…

Technorati Tags: ,,
1 Comment
read more
posted 22/12/09

I uploaded my first script to Script Center

By Fredrik Wall

I uploaded my first script to Microsoft’s Script Center.

http://gallery.technet.microsoft.com/ScriptCenter/en-us/c5158ed5-5b82-4243-9654-9106f7ca6663

It was my Get-DotNetInfo function.

I like the repository and that you can add comments.

Please rate my function and write a comment.
Good or bad!

1 Comment
read more
posted 19/12/09

Get-DotNetInfo function

By Fredrik Wall

I made this function back in 2007.
But I did only published It in Swedish then.
This function helps me a lot when I’m working
with PowerShell and Dot Net classes.

When I have the Dot Net Class that I want
to use but not much information. Then I just
call this function with the Dot Net Class
and IE opens with the information.

# Get-DotNetInfo Function
#
By Fredrik Wall, fredrik@poweradmin.se
#
http://www.poweradmin.se/blog
#
#
Created: 2007-09-13
#
Last modified: 2009-12-18
#
Tested on: Windows XP, Windows Vista,
#
Windows 7, Windows Server 2008 R2
#
Revision history:
#
2009-12-18 ADD: Function information
#
Example of calling the function:
#
Get-DotNetInfo System.Net.Webclient

Function Get-DotNetInfo {
 
param([string]$dotnet="")
 
$objIE = New-Object -Com Internetexplorer.Application
 
$url = "http://msdn2.microsoft.com/en-us/library/" + $dotnet
 
$objIE.Navigate($url)
 
$objIE.Visible=$true
}


1 Comment
read more
posted 15/12/09

Create Lab AD Functions (Updated)

By Fredrik Wall

I wrote a script some years back in VBScript that made
Active Directory Users with “Real Swedish” names.

It’s took Firstname from one text file and Lastname from
another text file, randomly. And made random Passwords as well.

So instead of getting users like “user0001” and “user998” I got
”Anders Svensson” and “Lena Persson”. Prettys nice.

The bad part about the VBScript was that It took 10-15 min to clean
the textfiles first.

I’m a computer geek and I do lots of labs with AD so I wanted to
make a similar thing with PowerShell.

After some work I have made 2 functions that will get two
lists of common First and Last names from The Gothenburg University.

The first function are:

function getFNames {
# Get a list of Swedish Firstnames from Gothenburg University
param ($fNameFile)
if(!(test-path -path $fNameFile)){
    if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient  }
    $url="http://spraakbanken.gu.se/statistik/lbfnamn.phtml"

    $fNames = $WebClient.DownloadString($url)  -replace "[0-9]","" -replace "<td>",""
-replace "<tr>","" -replace "</td>","" -replace "</tr>","" -replace "<b>",""
-replace "</b>","" -replace "<td class=""le"">","" -replace "<table>",""
-replace "</table>","" -replace "<br />","" -replace "<hr />",""
-replace "<body>","" -replace "</body>","" -replace "<html>",""
-replace "</html>","" -replace "<table width=""%""><td width=""%"">",""
-replace "<td class=""lastupd"">Senaste ändring <b class=""date"">  okt  av lbtekn@svenska.gu.se</a>.",""
-replace "<!doctype html public ""-//wc//dtd html . transitional//en"">",""
-replace "<head>","" -replace "</head>","" -replace "<h>","" -replace "</h>",""
-replace "<title>","" -replace "</title>","" -replace "<tr class=""headrow"">",""
-replace "   <meta http-equiv=""Content-Type"" content=""text/html; charset=iso—"">",""
-replace "   <link rel=""STYLESHEET"" href=""/css/statistik.css"" type=""text/css"">",""
-replace "Vanliga förnamn, ordnade efter frekvens.",""
-replace "       Rang   Pojknamn           Frekvens   Rang   Flicknamn          Frekvens",""
-replace "\s{3,}", " "
    $fNames = $fNames -split (" ")

    foreach ($fName in $fNames) {
        $fName | Out-File $fNameFile -append
    }
    }
}
You will call It like this:$fNameFile = "c:\scripts\fname.txt"getFNames $fNameFile

or simply

getFNames 'c:\scripts\fname.txt'

First of all It will look for the file with all the first names.
If It can’t find It, It will download It from the server at
Gothenburg University and clean It.

Now You Got a text file with lots of names (1000).

And the other function are:

function getLNames {
# Get a list of Swedish Lastnames from Gothenburg University
param ($lNameFile)
if(!(test-path -path $lNameFile)){
    if ($WebClient -eq $null) {$Global:WebClient=new-object System.Net.WebClient  }
    $url="http://spraakbanken.gu.se/statistik/lbenamn.phtml"

    $lNames = $WebClient.DownloadString($url)  -replace "[0-9]","" -replace "<td>",""
-replace "<tr>","" -replace "</td>","" -replace "</tr>","" -replace "<b>",""
-replace "</b>","" -replace "<td class=""le"">","" -replace "<table>",""
-replace "</table>","" -replace "<br />","" -replace "<hr />","" -replace "<body>",""
-replace "</body>","" -replace "<html>","" -replace "</html>",""
-replace "<table width=""%""><td width=""%"">",""
-replace "<td class=""lastupd"">Senaste ändring <b class=""date"">  okt  av lbtekn@svenska.gu.se</a>.",""
-replace "<!doctype html public ""-//wc//dtd html . transitional//en"">",""
-replace "<head>","" -replace "</head>","" -replace "<h>","" -replace "</h>",""
-replace "<title>","" -replace "</title>","" -replace "<tr class=""headrow"">",""
-replace "   <meta http-equiv=""Content-Type"" content=""text/html; charset=iso—"">",""
-replace "   <link rel=""STYLESHEET"" href=""/css/statistik.css"" type=""text/css"">",""
-replace "Vanliga förnamn, ordnade efter frekvens.",""
-replace "       Rang   Pojknamn           Frekvens   Rang   Flicknamn          Frekvens",""
-replace "\s{3,}", " "
-replace " efternamn,frekvensordning Vanliga efternamn, i frekvensordning Rang Namn Frekvens ",""
    $lNames = $lNames -split (" ")

    foreach ($lName in $lNames) {
        $lName | Out-File $lNameFile -append
    }
    }
}

 

And this one will give you the same as the other function.

You will call It like this:$lNameFile = "c:\scripts\lname.txt"getLNames $lNameFile

or simply

getLNames 'c:\scripts\lname.txt'

Now you got 1000 first names and 1000 last names to make random names of.

    $firstName = Get-Content $fNameFile | Get-Random -ErrorAction SilentlyContinue
    $lastName = Get-Content $lNameFile | Get-Random -ErrorAction SilentlyContinue

$firstName + + $lastName

I will post more of my script tommorow. This was todays function gift from me :)

 

3 Comments
read more
posted 15/12/09

A Christmas special

By Fredrik Wall

It’s soon Christmas and the first snow has landed
here in Stockholm.

snowman-icon 

So I have decided to do a Christmas special.
I know, my friend DXter does a Geek special
for Christmas. My Christmas special will be a
PowerShell function special.

presentblue-icon

I will post a function/day until Christmas eve.
And I will also give all of my readers a special gift
on Christmas eve…

 

Technorati Tags: ,,
No Comments
read more
posted 14/12/09

Clean Temp Function (Updated)

By Fredrik Wall

This is a small but effective function.
It will delete most of the stuff in your temp dir.
 

Use with close option to close all open programs
before you clean the temp dir. It will not delete
open/locked files.
drive-slim-dos-icon 

The function can also be downloaded from the Download section.
http://poweradmin.se/blog/download

 
# Clean Temp Function
# By Fredrik Wall, fredrik@poweradmin.se
# http://www.poweradmin.se/blog
#
# Last modified: 2009-12-14
# Tested on: Windows 7, Windows Server 2008 R2
# Revision history:
# 2009-12-14    ADD: -ErrorAction SilentlyContinue
#                ADD: Close option
# Example of calling the function:
# Clean-Temp
# Clean-Temp 'Close'
#
function Clean-Temp {
    param($status)
    if ($status -eq 'close') {
        # Some programs that often uses temp dir for temp files
        process iexplore* | kill
        process outlook* | kill
        process word* | kill
        process excel* | kill
        process msn* | kill

        $myTemp = "$Env:temp"
        Get-Childitem $myTemp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        $myTemp = $null
    }
    else  {
        $myTemp = "$Env:temp"
        Get-Childitem $myTemp | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        $myTemp = $null
    }
}

 

Technorati Tags: ,,
No Comments
read more
posted 13/12/09

Product Type Function

By Fredrik Wall

This is a small function that will help you with

scripts when you need to know if you are on a Workstation or not.

The script can also be downloaded in the Download section.

http://poweradmin.se/blog/download/?did=2

# Product Type Function
# By Fredrik Wall, fredrik@poweradmin.se
# http://www.poweradmin.se/blog
#
# Last modified: 2009-12-11
# Tested on: Windows 7, Windows Server 2008 R2
# Revision history:
# 2009-12-11    ADD: Error handling
# Example of calling the function:
# switch (ProductType)
#{
#    'Workstation' {
#        Write-Host "Workstation"
#        }
#    'Domain COntroller' {
#        Write-Host "Domain Controller"
#    }
#    'Server' {
#        Write-Host "Server"
#    }
#    
#}
#
function ProductType {
# 1 = Workstation, 2 = Domain Controller, 3 = Server

$Error.Clear()

$product = Get-WmiObject -Class Win32_OperatingSystem

if ($Error.Count -gt 0) {
    Write-Host "An error occured while trying to determine ProductType"
}
else {
    switch ($product.producttype)
    {
        1 {"Workstation"}
        2 {"Domain Controller"}
        3 {"Server"}
        default {"Not a known Product Type"}
    }
}
}
No Comments
read more
posted 13/08/09

Ping-Status function

By Fredrik Wall

I use this function when I’m writing scripts that will
take information from Servers and Clients when
they are on the network.

function Ping-Status {
########################################################################
# Ping-Status function
# Created: 2009-07-21
# By: Fredrik Wall, fredrik[dot]wall[at]riverpoint[dot]se
########################################################################
param ($mysrv)
$pingstatus = Get-WmiObject -Query "SELECT StatusCode FROM win32_PingStatus
WHERE ADDRESS = '$mysrv'"
      if ($pingstatus.StatusCode -eq 0) {
      $true
      }
      else
      {
      $false
      }
}
2 Comments
read more
posted 28/07/09

Get-OSVersion

By Fredrik Wall

This is my Get-OSVersion function.

 

function Get-OSVersion {
########################################################################
# Function: Get Operating System Version
# Version: 1.0
# Updated: 2009-07-28
# Code By: Fredrik "Dalle" Wall, Riverpoint AB (fredrik[dot]wall[at]riverpoint[dot]se)
########################################################################
$osver = Get-WmiObject win32_operatingsystem
$osver = $osver.version.substring(0,3)

 switch ($osver)
    {
        '5.1' {$osver = "Windows XP"}
        '5.2' {$osver = "Windows Server 2003"}
        '6.0' {
            $path=”HKLM:\Software\Microsoft\Windows NT\CurrentVersion”
            $reg=Get-ItemProperty $path
            $prodName = $reg.ProductName
            if ($prodName -match "Windows Vista") {$osver = "Windows Vista"}
            if ($prodName -match "2008") {$osver = "Windows Server 2008"}
        }
        '6.1' {$osver = "Windows 7"}
        default {$osver = "Unknown"}
    }
    $osver
}
2 Comments
read more