Technically Speaking

June 11, 2009

Using Powershell and Subinacl.exe to Permission Printers

Filed under: PowerShell — Chris @ 3:42 pm
Tags: , ,

Hi Folks ,

 

I was given a task at work to add a global group to 100’s of printer queue’s . I knew I wanted to use powershell to do this task but was unsure how to permission printers . I came across this utility (Subinacl.exe) that I knew about but didn’t think it permissioned printer queue’s . It took just one line to accomplish this task . In minutes I permssioned 100’s of queue’s and my manager was very happy with the result .

So here is the one line :

gwmi -class Win32_Printer -comp ServerName | % { $_.Name} | % { subinacl.exe /printer \\ServerName\$_ /grant=DomainName\GroupName=F}

F means Full Control…

You can also use M for Manage Documents or P for Print .

This is a good example also of Powershell’s ability to work with command line utilities .

I thought this was cool and a super time saver . If you have any questions please comment below and I will respond pronto :)

Thanks

Chris

May 27, 2009

Introduction to Powershell Modules w/ Advanced Functions

          If you been using powershell you heard these terms Modules and Advanced functions . What are they ? Why are they important ? Why would I want to use them ? Do I need to use them ? As you can see allot of questions.

          Modules have replaced Pssnapins in V1 which was compiled code usually C#  that gave powershell more functionality .This was a way to add more cmdlets . An example of this is System Center Operations Manager (SCOM) pssnapin . When you install the console along with powershell you get the pssnapin for SCOM which gives you a bunch of cmdlets.

          Now with powershell V2 we have what are called modules . These modules can be any function that you have written in powershell. That is the beauty of modules they are basically scripted cmdlets that you create . Now anyone can create them. You don’t need to fire up a compiler and bang out C# code to get the extra functionality you need .So if you take any function your written in Powershell and changed the extension to .psm1 then you have a module .

           They are important because they can save you time by re-using specific scripts to do repetitive tasks . When you import your module its loaded into your powershell session sort of like dot sourcing your script. First you need to know where are the modules located . To find out where your modules folder is located enter this command in your powershell command prompt .

dir env:psmodulepath

psmodulepath is an environment variable. Please see this blog for more information http://tfl09.blogspot.com/2009/01/modules-in-powershell-v2.html

You should see something like this …

Name                           Value
—-                           —–
PSMODULEPATH                   C:\Documents and Settings\username\My Documents\WindowsPowerShell\Modules;

There is also a location in your %windir%\system32\Windowspowershell\modules directory.

 

You probably be using the user specific directory.

So now that you know where the modules are here is an example of a module I created :

I will call it Get-OSInFo.psm1

Here is what it looks like using Advanced Functions:

 

Function Get-OsInfo {

<#
.Synopsis
 Gets basic Os information Using WMI OperatingSystem Class.
.Description
 Gets basic Os information Using WMI OperatingSystem Class.
.Parameter ComputerName
 Name of the of the Computer you want info on.

.Example
 PS> get-OsInfo -computer serverA
.Link
    about_functions
    about_functions_advanced
    about_functions_advanced_methods
    about_functions_advanced_parameters
.Notes
NAME:      Get-OsInfo
AUTHOR:    Name
LASTEDIT:  05/26/2009

#>

param(
[Parameter(Mandatory=$true)]
[string]$computerName = “localhost”
)

gwmi -class Win32_OperatingSystem -Computer $computername

}

            Advanced Functions is new for V2 . What makes them so nice to use is the built in help . Notice the syntax. Also notice the parameter section . This is a new way to specify parameters. Note that I have a parameter called Computername default to local host and it is mandatory. You can go crazy with parameters but this is just an intro so more to come in the future . The last portion is the standard WMI call to OperatingSystem .

 

Now lets put this all together :)

Place this file in your modules folder but create a folder called get-osinfo and put the psm1 file in there. I use the same name as the script for the folder . (I am not sure if that is necessary but it is best practices ).

 

Now that we have the module file in the correct place open up your powershell command prompt.

We are going to import the module …

import-module get-osinfo.psm1

Like I said before this is like dot sourcing a script (. .\scriptname.ps1). Now that we imported it lets check it out ..

Before we do I wanted to point out that you can have an umlimited number of functions in this psm1 file . So what if you got a bunch of functions for a third party how would you know what functions are in this file ?

A somewhat easy way would be to use this command (This is after you imported the module )

get-command -commandtype Function

CommandType     Name                                                     Definition
———–     —-                                                     ———-
Function        A:                                                       Set-Location A:
Function        B:                                                       Set-Location B:
Function        C:                                                       Set-Location C:
Function        cd..                                                     Set-Location ..
Function        cd\                                                      Set-Location \
Function        Clear-Host                                               $space = New-Object System.Management.Au
Function        D:                                                       Set-Location D:
Function        Disable-PSRemoting                                       …
Function        E:                                                       Set-Location E:
Function        Enable-PSRemoting                                        …
Function        F:                                                       Set-Location F:
Function        G:                                                       Set-Location G:
Function        Get-OsInfo                                               …
Function        H:                                                       Set-Location H:

You can see we see our get-Osinfo function listed .

 

Now that we know that is the function we need …we need to know how to run it (Remember the built in help for the function we created ?? )

Well lets get help the same way you would get help on any of other function or cmdlet.

help get-osinfo -det

NAME
    Get-OsInfo

SYNOPSIS
    Gets basic Os information Using WMI OperatingSystem Class.
SYNTAX
    Get-OsInfo [-computerName] [<String>] [-Verbose] [-Debug] [-ErrorAction [<ActionPreference>]] [-WarningAction [
    nce>]] [-ErrorVariable [<String>]] [-WarningVariable [<String>]] [-OutVariable [<String>]] [-OutBuffer [<Int32>
    ameters>]
DETAILED DESCRIPTION
    Gets basic Os information Using WMI OperatingSystem Class.
PARAMETERS
    -computerName
        Name of the of the Computer you want info on.

    <CommonParameters>
        This cmdlet supports the common parameters: -Verbose, -Debug,
        -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
        -OutBuffer and -OutVariable. For more information, type,
        “get-help about_commonparameters”.

    ————————– EXAMPLE 1 ————————–

 

    PS> get-OsInfo -computer serverA
REMARKS
    To see the examples, type: “get-help Get-OsInfo -examples”.
    For more information, type: “get-help Get-OsInfo -detailed”.
    For technical information, type: “get-help Get-OsInfo -full”.

 

Isn’t that great !!!

Lets run it …

Get-Osinfo -computername ServerA

(Notice how the parameter variable computername is used …)

You can even pipe this to the select cmdlet  like so

Get-Osinfo -computername ServerA | select Name,Organization,BuildNumber

 

To sum up … Modules helps you create specific functions to help you with different tasks . Microsoft made these scriptable so that any one can create them . By using advanced functions you get the built in help and special parameter settings for your functions .

It doesn’t stop here … there are also module manifests . Also you can enable different functions in your modules for lets say security reasons . This was just an introduction.

 

I hope you enjoyed reading this blog :)

 

Chris

May 5, 2009

Powershell Using $OFS

Filed under: PowerShell — Chris @ 8:03 pm
Tags: ,

Hello ,

$OFS which is a special variable in powershell . OFS stands for Output field separator . You would use this to separate objects in your array for example :

 

PS H:\> $numbers = 1,2,3,4,5
PS H:\> $numbers

1
2
3
4
5

 

What if you wanted to separate these let say with a semi colon . You can use the $OFS variable..

PS H:\> $ofs = “;” ;[string]$numbers
1;2;3;4;5 

 

This could be useful lets say if your getting users out of AD to setup an e-mail in outlook .

Not to bad … :)

For More info :

http://blogs.msdn.com/powershell/archive/2006/07/15/What-is-OFS.aspx 

 

Have Fun !

Chris

January 28, 2009

The future of Microsoft Flight Simulator

Filed under: Microsoft Flight Simulator — Chris @ 7:46 pm
Tags:

Everyone has heard the news about the layoffs happening at Microsoft and I even seen news relating to laying off the Aces division which includes the Flight Simulator program. If this is true it is truly sad. Its one of the oldest products of Microsoft since 1982 and even older then that starting with Sublogic . I hope this is not the end.

 

What do you have to say about it ?

Here is a link that has some information about this . Click-here.

 

Thanks

Chris

January 6, 2009

ScreenCast Powershell ISE Modifying the Console

Filed under: Powershell ISE — Chris @ 10:46 am
Tags: ,

I created a small screen cast on how to modify the Powershell ISE thru your profile . The Windows Powershell Team has a blog about this here .

The screen cast is here .

 

Please let me know if you like these.

 

Thanks

 

Chris

January 5, 2009

Just Some quick Notices

Filed under: PowerShell — Chris @ 2:08 pm
Tags: ,

This has been mentioned on other blogging sites but I wanted to put some links in my blog to some of this stuff.

Scott Herold Blog on Vmware and PowerGui

http://www.vmguru.com/index.php/articles-mainmenu-62/scripting/74-getting-started-with-powershell-and-powergui-in-your-virtual-infrastructure

Microsoft Powershell team with a little Screencast action :

http://blogs.msdn.com/powershell/archive/2009/01/05/ps-dir-a-d-the-screencast.aspx

A really cool series on Regex stuff if you don’t know what this is then please check it out :

http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2008/12/28/regular-expression-webcast-series.aspx

Get the latest version of Powergui :

Version 1.6

http://www.powergui.org/downloads.jspa

 

A word about the  Powershell ISE and PowerGui . I use PowerGui … I think that Powershell ISE is great but I wish that Microsoft would just let the 3rd party companies handle this . I know that Powershell ISE is a CTP app right now and I’m sure it will be great but Powergui is just awesome . I think Quest does a great job with these types of things .

Just my .02 cents .

Happy Post Holidays

Chris

December 31, 2008

New Microsoft Site in beta “Microsoft Answers.”

Filed under: Random — Chris @ 7:13 pm
Tags:

I just thought I pass this on I ran into this site still in beta looks like called Microsoft Answers where any User (at any level of experience) can find answers for there computer problems. With all the information out there on different subjects about Windows this is just another site to add to your collection .

Click to go to Site.

A nice feature I find is RSS feeds that you can add to your reader so you get the latest information . I know your going to say so what all sites have these … well it still s nice to have :)

 *** Update this is for Windows Vista looks like … I thought it might be for all editions .. Not !

Happy New Year !

Chris

December 27, 2008

Advanced Functions with Powershell –Having some Fun!

Filed under: PowerShell — Chris @ 8:22 pm
Tags: ,

Hi Folks ,

Since Powershell V2 CTP3 is out and an important feature called Advanced Functions is a totally new way to create Functions should make your scripts easier . Easier being the key word so far I’m trying to get used to them. In order to learn this new style I decided to go back to an earlier blog I did called Playoff Sunday with powershell. So I decided to do this script over with the new Advanced Function format . I will explain it after I show the script here :


<#
.Synopsis
Outputs random football Score for each team entered on the pipeline
.Description
This script outputs a random score for each football team entered into the pipeline
.Parameter Team
Team
.Example
PS> "Cowboys","Giants" | .\get-footballscore.ps1 | ft -auto


.ReturnValue
[String]


.Link
about_functions
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters


.Notes
NAME: Get-FootballScore.ps1
AUTHOR: Chris Federico
LASTEDIT: 12/27/2008


#>


param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[string]$Team
)


Process
{


#reset all values ;
$intMaxNumofScores = 0
$intTypeofScore = 0
$s = 0
$FinalScore = 0


#create random max number of scores from 1-7
$intMaxNumofScores = get-random -min 1 -max 5


for ($s = 1;$s -le $intMaxNumofScores;$s++)
{


$intTypeofScore = get-random -min 1 -max 3




switch ($intTypeofScore)
{
1 {$intTypeofScore = 7} #Touchdown
2 {$intTypeofScore = 3} #FieldGoal
3 {$intTypeofScore = 2} #Safety
}
$FinalScore = $FinalScore + $intTypeofScore


}
$obj = New-Object PsObject
$obj | Add-Member NoteProperty Team([string]$Team)
$obj | Add-Member NoteProperty Score([int]$FinalScore)
Write-Output $obj
}#Process

If you break it up into parts its easier to understand . the First part is what is called the in line help (What is in between the <# and #> tags). Now you can provide your own help for your functions and link them to the common parameters already included in other cmdlets like -erroraction or -verbose …

So by providing the above inline help I can then use this to get help :

get-help .\get-footballScore.ps1 -full

NAME
D:\Development\scripts\powershell\Get-FootballScore.ps1

SYNOPSIS
Outputs random football Score

SYNTAX
D:\Development\scripts\powershell\Get-FootballScore.ps1 [-Team] [] [-Verbose] [-Debug] [-ErrorAction []] [-WarningAction [ ce>]] [-ErrorVariable []] [-WarningVariable []] [-OutVariable []] [-OutBuffer []] []

DETAILED DESCRIPTION
This script outputs a random score for each football team entered into the pipeline

PARAMETERS
-Team

Required? true
Position? 0
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters?
This cmdlet supports the common parameters: -Verbose, -Debug,
-ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer and -OutVariable. For more information, type,
“get-help about_commonparameters”.

INPUT TYPE

RETURN TYPE
[String]

NOTES

NAME: Get-FootballScore.ps1
AUTHOR: Chris Federico
LASTEDIT: 12/27/2008

————————– EXAMPLE 1 ————————–

PS> “Cowboys”,”Giants” | .\get-footballscore.ps1| ft -auto

RELATED LINKS
about_functions
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters

That is some cool Stuff what you can probably do is create a template with the the different parts so you don’t have to create them .

The next part is to declare your parameters . There are different ways to do this depending on how many parameters your passing into the function [parameter(position=0), what position they are in , what type [string] [int] , If the parameters are being passed in from the pipeline ValueFromPipeline=$true, You can also declare a default parameter I believe this way [cmdletdefaultparameter] (?). This is something I will need to get used too . Some of it was already there but other syntax I need to get used too.

The rest of the script I’m basically doing a bunch of random numbers . Its based on the # of scores randomly between 1 and 7 times . You can play with this a bit . Its not scientific at all but the maint thing was that I wanted to practice passing in parameters from the pipeline using advanced functions because this is the way powershell is going . There are so many more things new in V2 CTP 3 … Remoting ,Eventing … Creating Jobs against a bunch of Machines is very promising . Its like parallel processing almost .

Here is an example of a Cowboys vs Giants Score :
“Gaints”,”Cowboys” |D:\Development\scripts\powershell\Get-FootballScore.ps1 | ft -auto

Team Score
—- —–
Gaints 16
Cowboys 13

Subtle but you get the idea ….

Happy Holidays !
Chris

December 24, 2008

Powershell V2 CTP3 Out !

Filed under: PowerShell, V2 CTP3 — Chris @ 12:55 am
Tags:

If you would like to test it out go here .

A bunch of new features : cmdlets, Ability to create advanced functions . This caught my eye :


get-hotfix -comp Server01 -cred domain\yourusername

Having the -cred parameter is optional if your current user name has rights . If your using the -cred it prompts you for the password . How cool is that ?

When was the last time you used a scripting language that got you hot fixes on a remote machine in one line?

Chris

December 21, 2008

Its Christmas Time !

Filed under: Random — Chris @ 4:23 pm
Tags:

Check out the The 12 Guido Days Of Christmas . You can support them by purchasing this song on Itunes.

Happy Holidays!

Chris

Next Page »

Blog at WordPress.com.