« Extended Rights in Active Directory | Main | Control Access Rights in Active Directory »
Static Members and PowerShell obscurity
By Adam Bell | March 29, 2007
I recently posted a question on the Microsoft PowerShell NG on how to use a .Net function in PowerShell.
It might be easier if I put this in context. I had a requirement to take a string containing a mixture of upper and lowercase letters, and rewrite it with a hyphen preceding the uppercase letters. This didn’t seem like a particularly daunting task. As MSDN is my new best friend I thought I’d have a browse and see what functions may be able to help.
I found the IsUpper function and then went about trying to use Get-Member to try and see if I could expose this within PowerShell. No dice.
Duncan Smith was kind enough to provide some light on the subject.
So within PowerShell we can access this as a Static Member. Now I’ve seen a few static members used in PowerShell scripts so far, but as a non-coder being able to uncover these to access functions and methods within .Net still seems something of a black art to me. I’m hoping that as time goes by, and my exposure to more and more programming concepts these things will start to become more apparent. I’ll let you know ;)
And below is my script to give you another example:
IsUpper.ps1
$MyString = "ThisIsMyString"
$NuString = ""
for ($i=0; $i -lt $MyString.length; $i++)
{
If ([char]::IsUpper($MyString.chars($i)))
{
$NuString = $NuString+"-"+$MyString.Chars($i)
}
Else
{
$NuString = $NuString+$MyString.Chars($i)
}
}
# trim the leading "-" if the string starts with a capital.
$NuString = $NuString.substring(1,$NuString.length-1)
write-host $NuString
- Converting a GUID string to octet string
- Power Lines
- Utilising CMD behaviour in PowerShell
- Error checking in PowerShell
Topics: PowerShell | No Comments »