RSS All Posts

RSS PowerShell Posts

Tags

2142 Active Directory Administrativia Announcements Battlefield Blogging Cricket Deployment Deployment4 Get-PSUGUK Group Policy HowTo Linux MDT MDT 2010 Microsoft Deployment Toolkit MSDN Music Permissions Personal PowerGui Power Lines PowerShell PowerShell Groups PowerShell Support PowerShell Tools PowerShell V2 Presentations PSUGAU Quick Tips Scripting SDDL Security Tech Talk Ubuntu User Groups Virtualisation VMware Infrastructure Client WAIK Weekly Poll Windows 7 Windows Automation Installation Kit Windows Server 2003 Windows Server 2008 XML

Archives

Meta


« | Main | »

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

Topics: PowerShell | No Comments »

Comments