PowerShell – Calculated Property Example & Export-Csv Formatting


Import-Module MSOnline
Connect-MSOLService -Credential (Get-Credential)
$groupid = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' # A GroupID with a known single Error.
$groupdetails = Get-MSOLGroup -ObjectID $groupid -ErrorAction 'Stop'
$groupdetails | `
Select-Object ObjectID,DisplayName,GroupType,`
@{ Label='ExpandedErrorCode';Expression={$PSItem.Errors[0].ErrorDetail.ObjectErrors.ErrorRecord.ErrorCode} },`
@{ Label='ExpandedErrorDescription';Expression={$PSItem.Errors[0].ErrorDetail.ObjectErrors.ErrorRecord.ErrorDescription} }
| `
Export-Csv -LiteralFile "$env:USERPROFILE\Desktop\MyGroupFile.csv" -NoTypeInformation
Remove-Variable groupid

Running $groupdetails | Get-Member shows Name of ‘Errors’ as a MemberType of: ‘Property’, with a Definition of: ‘System.Collections.Generic.List[Microsoft.Online.Administration.ValidationError] Errors {get;set;} ‘

Advertisements

PowerShell – Array vs. ArrayList Performance

Let’s take a look at performance when dealing with looping over an array variable; adding a new item and overall performance.
A standard array object in PowerShell is of a fixed size when created and cannot expand automatically. When you use the “+=” operator to append an entry, a new array object is created in memory with the additional overall previous array object. This leads to a large performance hit when dealing with a large number of items and increased memory consumption. For example:


$ArrayList = New-Object -TypeName 'System.Collections.ArrayList';
$Array = @();


Measure-Command
{
for($i = 0; $i -lt 10000; $i++)
{
$null = $ArrayList.Add("Adding item $i")
}
};
Measure-Command
{
for($i = 0; $i -lt 10000; $i++)
{
$Array += "Adding item $i"
}
};

Recommendation: use generics!

WMIC, WmiObject, CimInstance Formatting

wmic bios get serialnumber

Reference: http://support.microsoft.com/kb/558124

New formats:

Get-WmiObject -Class "Win32_SystemEnclosure" | Select-Object SerialNumber
# OR
Get-CimInstance -Class "Win32_SystemEnclosure" | Select-Object SerialNumber

Another type of Query:

Get-WmiObject -Query "SELECT * FROM Win32_ComputerSystem" -ComputerName .
# OR
Get-CimInstance -Query "SELECT * FROM Win32_ComputerSystem" -ComputerName .

PowerShell – Dissecting Parameters and Enumeration

Get-Command Get-Mailbox | Get-Member Param*
(Get-Command Get-Mailbox).Parameters | Format-List
(Get-Command Get-Mailbox).ParameterSets | Format-List name # Returns: __AllParameterSets
$m = (Get-Command Get-Mailbox).ParameterSets[0].Parameters | ?{$_.Name -eq "Filter"}

References:

http://blogs.msdn.com/b/powershell/archive/2006/05/10/594175.aspx

 

 

Enumeration:

function Select-Color
{
  param(
  [System.ConsoleColor]
  $Color
  )

  "You chose $Color"
}

Then enter: Select-Color -Color
Snippet from:
http://powershell.com/cs/blogs/tips/archive/2012/11/16/using-enumeration-types-for-parameter-intellisense.aspx
References:
http://blogs.msdn.com/b/powershell/archive/2007/01/23/how-to-create-enum-in-powershell.aspx

PowerShell – Dispose()

You may be asking yourself “Why is that one script keeping all my RAMs?!”.

Maybe you should call the .Dispose() method on the variable to clean-up.

Another option is to use Remove-Variable .

References:
http://technet.microsoft.com/en-us/library/ff730962.aspx
http://msdn.microsoft.com/en-us/library/system.management.automation.powershell.dispose(v=vs.85).aspx

Linux Adventures With Debian

1 – Install your distro, I am using Debian NetInst and then adding in some XFCE. A nice Terminal command for some more info:

uname -a && cat /etc/*release

2 – Patch!:

apt-get update && apt-get upgrade -y

3 – Customize your Terminal (bash) prompt:

a. Yours:

/home/<username>/.bashrc

(Hidden Path) a.k.a.

~/.bashrc

b. The “root” account’s is located here:

/root/.bashrc

I used this: http://ezprompt.net/

4. Install some themes:

/usr/share/themes

5. More visuals: conky :

apt-get install conky-all

a. Custom conky files –

Your conky preferences are stored here:

/home/<username>/.conkyrc

Additional add-on files/scripts can be placed here (?):

/home/<username>/.conky

6. Adding/editing repos:

/etc/apt/sources.list

PowerShell – Various Annoyances

Terminating errors vs. Non-terminating errors.
This will “fail” (a.k.a. worked as designed) and skip over the catch block because it is a non-terminating error.

try
{
Import-Module BogusModule
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

This will work:

try
{
Import-Module BogusModule -ErrorAction 'Stop'
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

Reference function:
http://stackoverflow.com/questions/10498433/how-to-correctly-ignore-import-module-errors-in-powershell

Annoyance:
PowerShell deciding how it will format (“rolling” and “unrolling” objects) an item on output.
An intermediary function for CSV as an example:
http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/