header image

Archive for PSAM

Hex Addition

Posted by: | May 11, 2010 | No Comment |

We’ve looked at the binary functions – now its time to look at the hex functions starting with addition.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
function Get-HexSum {
param(
    [string]$inputvalue1,
    [string]$inputvalue2
)
## check valid hex numbers
    Test-Hex $inputvalue1
    Test-Hex $inputvalue2

    $sum = (ConvertTo-Decimal -inputvalue $inputvalue1 -hex) `
    + (ConvertTo-Decimal -inputvalue $inputvalue2 -hex)

    ConvertTo-Hex -inputvalue $sum
}

This is a direct copy of the binary addition function – just changed to accept hex input instead.

Again we test the hex number is actually hex – convert to decimal, add, and convert back to hex

Establishing a pattern helps make things easier for producing related functions.

under: Modules, PowerShellV2, PSAM

Bitwise Operations

Posted by: | May 9, 2010 | No Comment |

Continuing our look at binary operations I decided I wanted to be able to perform the bitwise operations – band, bor, bxor and get the results back as binary.  We already have operators for these actions in PowerShell so its just a matter of wrapping the appropriate conversion code around them.  I’ll show the code for band here.  The other two are very similar and will be available in the module when I post it on codeplex.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
function Get-BinaryAND {
param(
    [string]$inputvalue1,
    [string]$inputvalue2,
    [switch]$showsum
)
    if ($inputvalue1.length -ne $inputvalue2.length){Throw "Input values must be same length"}

## check valid binary numbers
## move this to a validation test
    Test-Binary $inputvalue1
    Test-Binary $inputvalue2

    $res = (ConvertTo-Decimal -inputvalue $inputvalue1 -binary) `
         -band (ConvertTo-Decimal -inputvalue $inputvalue2 -binary)
    $ans = (ConvertTo-Binary -inputvalue $res).PadLeft($inputvalue1.length, "0")
    if ($showsum) {
        $inputvalue1
        $inputvalue2
        "-" * $inputvalue1.length
        $ans
    }
    else {
        $ans
    }
}

 

Two binary numbers as input as we saw in the addition\subtraction functions.  I have changed the names to be consistent across the module. Perform the standard checks and then convert them to decimal and perform the band. The result is converted back to binary – padding left with zeros if required.

I decided to show the inputs and output if required – may be a good learning tool to understand this.

 

usage is straight forward

PS> Get-BinaryAND -inputvalue1 1010 -inputvalue2 0011
0010
PS> Get-BinaryAND -inputvalue1 10111010 -inputvalue2 01110011 -showsum
10111010
01110011
——–
00110010

The module contents stand at

ConvertTo-Binary
ConvertTo-Decimal
ConvertTo-Hex
Get-BinaryAND
Get-BinaryDifference
Get-BinaryOR
Get-BinarySum
Get-BinaryXOR
Test-Binary
Test-Hex

I need to add a couple of functions to do hex addition and subtraction and then its done

under: PowerShellV2, PSAM

Binary Addition

Posted by: | May 6, 2010 | No Comment |

We recently looked at converting numbers to binary and hex.  I then thought that I sometimes need to perform simple arithmetic in these bases so – binary addition.

We can use the functions we already have.  Convert a couple of numbers to binary

PS> $a = Convert-ToBinary -inputvalue 51
PS> $b = Convert-ToBinary -inputvalue 39
PS> $a
110011
PS> $b
100111

sum them by converting the binary back to decimal, performing the sum and converting back to binary.

PS> $c = Convert-ToBinary -inputvalue ((Convert-ToDecimal -inputvalue $a -binary) + (Convert-ToDecimal -inputvalue $b -binary))
PS> $c
1011010

A quick check on the result

PS> Convert-ToDecimal -inputvalue $c -binary
90

That works OK  but its a bit messy – too much typing. So I wrote Get-BinarySum

001
002
003
004
005
006
007
008
009
010
011
012
013
014
function Get-BinarySum {
param(
    [string]$value1,
    [string]$value2
)
## check valid binary numbers
## move this to a validation test
Test-Binary $value1
Test-Binary $value2

$sum = (Convert-ToDecimal -inputvalue $value1 -binary) + (Convert-ToDecimal -inputvalue $value2 -binary)

Convert-ToBinary -inputvalue $sum
}

 

Get the two binary numbers – test that they are binary.  Test-Binary is a helper function created from the code used in Convert-ToDecimal

The sum is performed by converting each binary to a decimal, summing them and converting the result back to a binary.

Bingo.  This forms the framework for other functions.

under: Modules, PowerShellV2, PSAM

PowerShell Admin Modules

Posted by: | April 25, 2010 | No Comment |

I have created the first download for the PowerShell Admin Modules. This will be a set of of modules including code previously published on my blogs at:

http://msmvps.com/blogs/RichardSiddaway/Default.aspx

http://itknowledgeexchange.techtarget.com/powershell/

http://richardsiddaway.spaces.live.com/

 

The first release comprises a module for working with shares and can be found at:

http://psam.codeplex.com/

 

It has the following functions:

  • Get-Share
  • Get-ShareAccessMask
  • Get-ShareSecurity
  • New-Share
  • Remove-Share
  • Set-Share

There have been a series of posts on my IT Knowledge Exchange blog (see above) about the functions.  Select Shares from the

Browse This Blog’s Tags box

More to follow soon. Enjoy

Technorati Tags: PowerShell,Modules,Shares,PSAM
under: Modules, PowerShellV2, PSAM

« Newer Posts

Categories