This post is where I want to put how to do things from .NET in powershell.
There are some automatic conversions, for instance, that makes code different.
Using string.Split()
[char]$domsep = '\' [string]$userid = "CONTOSO\XANDH" #Split will in .NET accept a char[], but here it is OK to give the char directly without the array $userid = $userid.Split($domsep)[1] #XANDH # #In most cases you can use the powershell way to do split as below, but that does not work, when the split char is '\' #$userid = ($userid -split "\")[1] #XANDH #In case of split char is '\' you need to escape it with an extra '\' as follows: $userid = ($userid -split "\\")[1] #XANDH
Using String.IsNullOrEmpty()
if (-not [System.string]::IsNullOrEmpty($someString))
{
}
Add CRLF to strings
$crlf = "`n`r" $Body = "Testing CRLF:" + $crlf $Body += "Bla-bla"
Links:
- More on arrays: http://ss64.com/ps/syntax-arrays.html
- WebRequest: http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/powershell.html
- Send email: http://www.iislogs.com/steveschofield/send-email-with-powershell-script-schedule-script-with-windows-task-scheduler
- CRLF: http://stackoverflow.com/questions/325953/how-can-i-replace-newlines-using-powershell
Advertisement