posting code

What are good ideas and techniques for posting code here?

I usually try to use the toolbar button that has the tool tip text "Insert Code Block" but I'm not always happy at what gets displayed.

I see others getting better results.

September 10th, 2015 10:47am

That's basically all I do too.

I write my code in the ISE and make sure that the lines don't extend too far to the right to keep up readability.

The only real tweak I ever use is to add a <br/> tag via the HTML editor view if I'm posting a short oneliner to force the code to appear in the middle of the codeblock.

Want to see some fun you can have with it though?

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/0c49752d-6666-45b6-a224-f98a17d00c40/opinions-please-line-numbers-in-codeblocks?forum=ITCG

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 10:51am

I find it best to post text only in the code block.  Since I format in a consistent way the post is normally good.  DO not mix tabs and spaces for indenting.  Use either all tabs or al spaces. When mixed the result is inconsistent.
September 10th, 2015 10:58am

Lately I have used the "Insert Code Block" more often, but in the past I found it unacceptable. It messed up spacing and indenting and used a proportional font. I coded scripts to convert code into HTML for the forums and posted them here:

http://www.rlmueller.net/ConvertCodeToHTML.htm

I used these scripts (one for generic code, one colorized for VBScript, and another colorized for PowerShell) many times in the forums. However, the forum editor has been modified since I last updated these. When I get a chance I will test again in the Sandbox forum to see if more modifications might be needed. The colorization I used was the standard in 2011, but has now changed. Also, I now use different versions of these scripts in the TechNet Wiki, because the <br> tags cause major problems in that editor. I also added scroll bars in my Wiki versions, which help in the Wiki. I will test in the Sandbox to see if they do any good in the forums, although I generally try to keep code lines short enough to prevent the need to scroll. An example PowerShell script converted into HTML using my PsToHtml.vbs and pasted into "Insert HTML".

Function OctetToGUID ($Octet)
{
    # Function to convert Octet value (byte array) into string GUID value.
    $GUID = [GUID]$Octet
    Return $GUID.ToString("B")
}

$User = Get-ADUser -Identity "jsmith"

$Value = $User.ms-DS-ConsistencyGUID
$UserGUID = OctetToGUID($Value)
$UserGUID

-----

The dashes at the end are enclosed in <pre> and </pre> tags so the post is flagged as having code. Otherwise, you won't get credit for "Included a code snippet when answering" in the recognition system.

Edit: After testing in the  TechNet Sandbox forum, I prefer the versions of the scripts I linked above. I like the fixed width font and spacing much b

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 11:40am

This is just from a PowerShell Studio "Copy as HTML"/paste

$Groupname = 'Office365E1'

if (($GroupID = Get-MsolGroup -Search $Groupname).ObjectId) {
    
    Write-Host "Checking for unlicensed users in group Office365E1 with ObjectGuid $GroupID... to enable remote mailbox"
    $GroupMembers = Get-MsolGroupMember -GroupObjectId $GroupID -All | Where-Object { $_.IsLicensed -eq $false }
    
    foreach ($User in $GroupMembers) {
        Try {
            $alias = $User.EmailAddress.Split("@")[0]
            $remoteaddress = "$alias@tenant.mail.onmicrosoft.com"
            Enable-RemoteMailbox $User -PrimarySmtpAddress $User.EmailAddress -RemoteRoutingAddress $remoteaddress -ea stop
            Set-RemoteMailbox -Identity $User -EmailAddresses @{ add = $remoteaddress } -ea stop
            Write-Host "Successfully enabled remotemailbox for $User"
        } catch {
            Write-Warning "Error when enabling remotemailbox for $User"
        }
    }
    
} else {
    Write-Error 'Goupd not found'
}
		
September 10th, 2015 12:08pm

Thanks for the information about <pre> tags.
I like your technique to get non-proportional fonts used to display code.

Free Windows Admin Tool Kit Click here and download it now
September 10th, 2015 12:55pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics