Tuesday, October 30, 2007

Stand alone registry functions library


The following file (also included below) contains 20 stand alone registry functions to Read,Write,Delete,Test registry keys/values from local or remote computer. Here's a list of the functions:
Get-RegBinary
Get-RegDWord
Get-RegDefault
Get-RegExpandString
Get-RegMultiString
Get-RegQuadWord
Get-RegString
Get-RegValueKind
New-RegSubKey
Remove-RegSubKey
Remove-RegSubKeyTree
Set-RegBinary
Set-RegDWord
Set-RegDefault
Set-RegExpandString
Set-RegMultiString
Set-RegQuadWord
Set-RegString
Test-RegSubKey
Test-RegValue
 
For all functions, values for registry hive can be one of the enum values for
[Microsoft.Win32.RegistryHive]. To get a list of possible values type:
[enum]::getnames([Microsoft.Win32.RegistryHive])

Values for registry value kind can be one of the enum values for
[Microsoft.Win32.RegistryValueKind]. To get a list of possible kind values type:
[enum]::getnames([Microsoft.Win32.RegistryValueKind])

NOTE: get/set the CurrentUser hive on a remote server is N/A
Your comments/suggestions will be much appreciated. Please report any bugs to this post in the comments below.
Also, ping remote computers before running remote registry functions. You can read more about it here.
To load all functions in one step, "dot source" the file in the console or in your PowerShell profile. Type:
. <path_to_file.ps1>

Some code examples (using positioned parameters):
# create new sub key
New-RegSubKey . CurrentUser software\CustomRegKey
# set the default value
Set-RegDefault . CurrentUser software\CustomRegKey "This is the Default value";
# create String value
Set-RegString . CurrentUser software\CustomRegKey RegString "Shay Levi";
# create Multiple string value
Set-RegMultiString . CurrentUser software\CustomRegKey RegMultiString @("Power","Shell","Rocks!");
# create binary value
Set-RegBinary . CurrentUser software\CustomRegKey RegBinary @([char[]]"PowerShell");
# create dword value
Set-RegDWord . CurrentUser software\CustomRegKey RegDWord 123;
# create expand value
Set-RegExpandString . CurrentUser software\CustomRegKey RegExpandString "Username is %USERNAME%";
# create quadword value
Set-RegQuadWord . CurrentUser software\CustomRegKey RegQuadWord 10;
# test existence of a key named "notexist", locally, under hkcu:\softaware, returns $false
Test-RegSubKey . CurrentUser software\notexist

reg

Now, get the values:
# get the default value
PS > Get-RegDefault . CurrentUser software\CustomRegKey
This is the Default value
PS > # Get String value
PS > Get-RegString . CurrentUser software\CustomRegKey RegString
Shay Levi
# Get Multiple string value, returns array object
PS > Get-RegMultiString . CurrentUser software\CustomRegKey RegMultiString
Power
Shell
Rocks!
# Get binary value
PS > Get-RegBinary . CurrentUser software\CustomRegKey RegBinary
80
111
119
101
114
83
104
101
108
108
 
# Get Expand String value, not expanded
PS > Get-RegExpandString . CurrentUser software\CustomRegKey RegExpandString
Username is %USERNAME%
# Get Expand String value, expanded
PS > Get-RegExpandString . CurrentUser software\CustomRegKey RegExpandString -expand
Username is ShayL
# Get quadword value
PS > Get-RegQuadWord . CurrentUser software\CustomRegKey RegQuadWord;
10
#Get non existent value name, returns the default string defind by $defaultValue
PS > Get-RegString . CurrentUser software\CustomRegKey NonExistValueName "my default"
my default
 
####################################################
function Get-RegString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}
###############################################################################
# Function: Set-RegString
# Description: Create/Update the specified registry string value
# Return Value: True/false respectively
function Set-RegString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [string]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value, [Microsoft.Win32.RegistryValueKind]::String);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Get-RegMultiString
# Description: Gets an array strings (REG_MULTI_SZ)
# Return Value: Array object
function Get-RegMultiString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}
###############################################################################
# Function: Set-RegMultiString
# Description: Create/Update the specified registry as strings array  (REG_MULTI_SZ)
# Return Value: True/false respectively
function Set-RegMultiString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [String[]]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::MultiString);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Get-RegBinary
# Description: Gets the registry value (REG_BINARY)
# Return Value: Array object
function Get-RegBinary{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    $subKey.GetValue($valueName,$defaultValue);
}
###############################################################################
# Function: Set-RegBinary
# Description: Create/Update the registry value (REG_BINARY)
# Return Value: True/false respectively
function Set-RegBinary{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [byte[]]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::Binary);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Set-RegDWord
# Description: Create/Update the registry value (REG_DWORD)
# Return Value: True/false respectively
function Set-RegDWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [double]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::DWord);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Get-RegDWord
# Description: Gets the registry value (REG_DWORD)
# Return Value: registry dword value
function Get-RegDWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}
###############################################################################
# Function: Set-RegExpandString
# Description: Create/Update the registry value (REG_EXPAND_SZ)
# Return Value: True/false respectively
function Set-RegExpandString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [string]$value       
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::ExpandString);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Set-RegExpandString
# Description: Get the registry value (REG_EXPAND_SZ)
# Return Value: registry value expanded or not based on -expand switch
function Get-RegExpandString{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value",
        [switch]$expand
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    if($expand){
        $subKey.GetValue($valueName,$defaultValue);
    } else {
        $subKey.GetValue($valueName,$defaultValue,[Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames);
    }
}
###############################################################################
# Function: Get-RegQuadWord
# Description: get the registry value (REG_QWORD)
# Return Value: registry value
function Get-RegQuadWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [object]$defaultValue="Your default value"
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($valueName,$defaultValue);
}
###############################################################################
# Function: Set-RegExpandString
# Description: Get the registry value (REG_QWORD)
# Return Value: True/false respectively
function Set-RegQuadWord{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName,
        [long]$value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.SetValue($valueName, $value,[Microsoft.Win32.RegistryValueKind]::QWord);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: Get-RegDefault
# Description: Get the registry default value
# Return Value: registry default value
function Get-RegDefault{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $subKey.GetValue($null);
}
###############################################################################
# Function: Set-RegDefault
# Description: Set the registry default value
# Return Value: True/false respectively
function Set-RegDefault{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        $value   
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName,$true);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    #$regKey.SetValue($null, $value,[Microsoft.Win32.RegistryValueKind]::String);
    $subKey.SetValue($null, $value,[Microsoft.Win32.RegistryValueKind]::String);
    if($?) {$true} else {$false}
}
###############################################################################
# Function: New-RegSubKey
# Description: Create the registry key
# Return Value: True/false respectively
function New-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    [void]$regKey.CreateSubKey($keyName);
    if($?) {$true} else {$false}
}
################################################################################
# Function: Remove-RegSubKey
# Description: Delete the registry key
# Return Value: Throws error in case the key doesnt exist
function Remove-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $regKey.DeleteSubKey($keyName,$true);
}
###############################################################################
# Function: Remove-RegSubKeyTree
# Description: Delete the registry key tree
# Return Value: None
function Remove-RegSubKeyTree{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $regKey.DeleteSubKeyTree($keyName);
}
###############################################################################
# Function: Get-RegValueKind
# Description: Get the registry value type (e.g, string,dword etc)
# Return Value: None
function Get-RegValueKind{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $regVal=$subKey.GetValueKind($valueName);
    if(!$regVal){
        write-error "The specified registry value does not exist.";
        return;
    } else {
        $regVal;
    }
}
###############################################################################
# Function: Test-RegSubKey
# Description: Test the existence of the registry key
# Return Value: True/false respectively
function Test-RegSubKey{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){$false}  else {$true}
}
###############################################################################
# Function: Test-RegValue
# Description: Test the existence of the registry value
# Return Value: True/false respectively
function Test-RegValue{
    param(
        [string]$server = ".",
        [string]$hive,
        [string]$keyName,
        [string]$valueName
    )
    $hives = [enum]::getnames([Microsoft.Win32.RegistryHive])
    if($hives -notcontains $hive){
        write-error "Invalid hive value";
        return;
    }
    $regHive = [Microsoft.Win32.RegistryHive]$hive;
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($regHive,$server);
    $subKey = $regKey.OpenSubKey($keyName);
    if(!$subKey){
        write-error "The specified registry key does not exist.";
        return;
    }
    $regVal=$subKey.GetValue($valueName);
    if(!$regVal){$false} else {$true}
}
































































































Saturday, October 27, 2007

Update Rollup 5 for Exchange Server 2007

 

The fifth rollup for Exchange 2007 has been officially released and can be downloaded here. The new rollup is a cumulative update and replaces all previous update rollups (1-4).

A complete list of changes for this update can be found here.

File Attributes helper functions

 

This file attributes functions returns True/False if the operation you're running succeeded or not. All functions are  Each function gets a file path and an attribute name as parameters.

Attribute names can be one of the [System.IO.FileAttributes] enum type names. I didn't check all of them but here's the full list:

ReadOnly
Hidden
System
Directory
Archive
Device
Normal
Temporary
SparseFile
ReparsePoint
Compressed
Offline
NotContentIndexed
Encrypted

 

function Get-FileAttribute{
    param($file,$attribute)
    $val = [System.IO.FileAttributes]$attribute;
    if((gci $file -force).Attributes -band $val -eq $val){$true;} else { $false; }
}

 

function Set-FileAttribute{
    param($file,$attribute)
    $file =(gci $file -force);
    $file.Attributes = $file.Attributes -bor ([System.IO.FileAttributes]$attribute).value__;
    if($?){$true;} else {$false;}
}

 

function Clear-FileAttribute{
    param($file,$attribute)
    $file =(gci $file -force);
    $file.Attributes -= ([System.IO.FileAttributes]$attribute).value__;
    if($?){$true;} else {$false;}
}

 

Testing the functions:

# create test file
$file = new-item -path . -name testFile.txt -type file;

write-host "`nIs file ReadOnly? " -nonewline
Get-FileAttribute $file.FullName ReadOnly
"attributes: " +(gci $file.FullName -force).attributes;

write-host "`nSet file as ReadOnly. Action succeeded? " -nonewline
Set-FileAttribute $file.fullname readonly
"attributes: " +(gci $file.FullName -force).attributes;

write-host "`nSet file as Hidden. Action succeeded? " -nonewline
Set-FileAttribute $file.FullName hidden
"attributes: " +(gci $file.FullName -force).attributes;

if(Get-FileAttribute $file.FullName hidden){
    write-host "`nFile is marked as Hidden"
    if(Clear-FileAttribute $file.FullName hidden){
        write-host "File is no longer Hidden"
    } else {
        write-host "error clearing hidden attribute"
    }
}

# delete test file
remove-item .\testFile.txt -force

 

The output:

Is file ReadOnly? False
attributes: Archive

Set file as ReadOnly. Action succeeded? True
attributes: ReadOnly, Archive

Set file as Hidden. Action succeeded? True
attributes: ReadOnly, Hidden, Archive

File is marked as Hidden
File is no longer Hidden
attributes: ReadOnly, Archive

 


A modified template/version of the functions with error handling:

function Do-FileAttribute{
    param(
        [string] $file=$(throw "Invalid path."),
        [string] $attribute=$(throw "Invalid attribute.")
    )
   
    # check if the file exists
    if(-not (test-path -path $file)){
        write-error "Invalid file path";
        return;
    }
   
    # check if the attribute name is valid
    $attrNames = [enum]::getNames([System.IO.FileAttributes]);
    if($attrNames -notcontains $attribute){
        write-error "Invalid attribute name. Possible values: $([string]::Join(', ',$attrNames))."
        return;
    }


    ###
    code for get/set/clear operation
    ###

}

Friday, October 26, 2007

IIS Data Mining with Log Parser 2.X

 

header_spotlight

In this video session, you will learn the Log Parser Basics; Input Formats, Output Formats; Functions; Output Templates. You'll also see you how to build on Log Parser; scripting with LogParser.dll, C# Interop, so as some advanced features: new 'CHART' output format, CheckPoint.

The session is presented by Alexis Eller, Microsoft's IIS Program Manager.

New E-book: Windows Server 2008

 

w08wnwc_cover

The following links provide snippets from a new book: Windows Server 2008: What’s New / What’s Changed by Greg Shields (published to you by SAPIEN Press.

 

 

 

 

 

Part 1 – E-book Intro
Part 2 - Introduction to Windows Server 2008
Part 3 - Installing Windows Server 2008
Part 4 - Server Management
Part 5 - Group Policy
Part 6 - Server Core
Part 7- Windows Server Virtualization
Part 8 – Active Directory
Part 9 - Terminal Services
Part 10 - Security & the Windows Firewall with Advanced Security
Part 11 – IIS 7.0
Part 12 - Other MN&C Features (you might not know about)

Thursday, October 25, 2007

Listing enumeration type values

 

This post has an update here.

A little utility function I wrote to automate the process of listing enumeration type names and values:

 

function Get-EnumValues{
    [enum]::getvalues($args[0]) | select @{n="Name";e={$_}},@{n="Value";e={$_.value__}} | ft -auto
}

 

PS > Get-EnumValues System.ConsoleColor

       Name        Value
       ----           -----
            Black     0
       DarkBlue     1
    DarkGreen     2
      DarkCyan     3
       DarkRed     4
DarkMagenta     5
   DarkYellow     6
            Gray     7
     DarkGray     8
             Blue     9
          Green    10
            Cyan    11
             Red    12
      Magenta    13
         Yellow    14
         White    15

 

PS > Get-EnumValues System.IO.FileAttributes

             Name           Value
             ----               -----
               ReadOnly     1
                  Hidden     2
                  System     4
               Directory    16
                 Archive    32
                  Device    64
                 Normal   128
            Temporary   256
             SparseFile   512
        ReparsePoint  1024
          Compressed  2048
                  Offline  4096
NotContentIndexed  8192
              Encrypted 16384

 

 

PS > Get-EnumValues System.IO.FileAccess

     Name      Value
     ----          -----
        Read     1
       Write     2
ReadWrite     3

 

 

PS > Get-EnumValues System.IO.FileOptions

          Name         Value
          ----            -----
                None     0
         Encrypted    16384
   DeleteOnClose   67108864
  SequentialScan   134217728
  RandomAccess   268435456
    Asynchronous   1073741824
   WriteThrough   -2147483648

 

 

I used this code to test the Get-EnumValues function. It gets all enumeration types stored in the system namespace and list each one's values:

 

$e=[System.Reflection.Assembly]::LoadWithPartialName("System").gettypes() | where {$_.basetype.fullname -eq "system.enum"}

$e | foreach {
    write-host "` --- [$_] ---";
    Get-EnumValues $_;
    read-host "Press any key to continue";
}

Tuesday, October 23, 2007

Windows PowerShell and SQL Server 2005 SMO

 

Part 8 (By Muthusamy Anantha Kumar) of this series has published. It demonstrates number of methods on how to use PowerShell in conjunction with SMO to display object properties of all SQL Server Objects.

Create Setup Project to install PowerShell SnapIn

 

Shahar Gvirtz has published a new article on The Code Project website, for creating setup project to install new PowerShell SnapIn developed in Visual Studio. I tested it and it seems to work fine. The installer creates a new setup.exe/setup.msi file that includes both install/uninstall actions.

Monday, October 22, 2007

PowerShell Radio

 

If you've never heard of PowerShell podcast by http://powerscripting.net now is the time!
First founded by Jonathan Walz and now co-hosted with Hal Rottenberg, PowerShell podcast provide you with a comprehensive report on PowerShell news.

Each Podcast/Episode can be downloaded as an MP3 file for offline usage. The podcast is usually divided into sections, with cool distortion guitar transitions , like News, Cmdlet of the week, Resources, Tips, One-liners, Gotchas , challenges and more.

Thumbs UP guys, you're doing a great job!

So, fire up your browser, tune in and Get-Podcast -PowerScripting | Get-BodyPart -Ears!

 

Finally, here's a little (rough) piece of code to download the podcasts using PowerShell:

 

function Get-PowerScriptingPodcast{
    param($dlPath)
    $url = "http://powerscripting.net/feed";
    $wc= new-object net.webclient;
    $xml = [xml]$wc.DownloadString($url);

    $xml.rss.channel.item | foreach {
        $fname = [system.IO.path]::GetFileName($_.enclosure.url);
        $podcast = read-host "Download $fname [Yes] default, [N] No"
        if($podcast -ne "n" -and $fname -ne ""){
            $wc.DownloadFile($_.enclosure.url,$fname);
        }   
    }
}

Exchange Terminology Roadmap

 

If you're confused by the Exchange 2007 new features, The following link provides a roadmap for administrators to understand the various terminology changes in Exchange 2007 and Windows Server 2008. It lists several features that have been enhanced, renamed, or that no longer exist.

Monday, October 15, 2007

Microsoft Developer Academy 2007

This is more relevant to my Hebrew readers (if there are any :-)).

Microsoft's Annual Developer Academy conference is going to take place at 11/27/2007 (put a reminder in your Outlook) in Airport city, Avenue Hall. Now is the time to influence the conference content. You can fill in this short survey.

From a PoSH POV, Shahar Gvirtz is going to make a presentation on PowerShell (and if he doesn't, then I guess I won't go).  Anyway, I saw some snippets from Shahar's presentation and I can tell there is definitely what to wait for. So, fill the form and don't forget to vote for the PowerShell session, see you there.

 

DevAcademy

Monday, October 8, 2007

Back in Israel


After 10 days in the big apple I came back yesterday to Israel.
My tasks at my company web farm were completed successfully and I even had some time to tour the streets of NYC.

I want to thank Brandon shell for inviting me to lunch. It was a great opportunity for me to meet him, His a great guy, 10x Brandon!.

I also want to congratulate three new PowerShell MVPs: Brandon Shell, Marco Shaw and Dmitry Sotnikov, Well done guys!.