The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,769 other followers

Archive for the ‘PowerShell’ Category

PowerShell: fixing `Get-HotFix` having empty `InstalledOn` entries

Posted by jpluimers on 2019/11/12

On some systems, Get-HotFix has many entries with an empty InstalledOn column.

This at least shows there is a date-format difference, but now the Source column is empty.:

Get-HotFix | Select-Object Source,Description,HotfixID,InstalledBy,InstalledOn,@{Name="InstalledOnValue";Expression={$_.psbase.properties["InstalledOn"].Value}} | Out-GridView

I contemplated using Microsoft.Update.Session in the scripts below, but it requires WinRM, the server side implementation of WS-Management – Wikipedia:

[ERROR] [DevMachine] Connecting to remote server DevMachine failed with the following
[ERROR] error message : The client cannot connect to the destination specified in the
[ERROR] request. Verify that the service on the destination is running and is accepting
[ERROR] requests. Consult the logs and documentation for the WS-Management service run
[ERROR] ning on the destination, most commonly IIS or WinRM. If the destination is the
[ERROR] WinRM service, run the following command on the destination to analyze and conf
[ERROR] igure the WinRM service: "winrm quickconfig". For more information, see the abo
[ERROR] ut_Remote_Troubleshooting Help topic.
[ERROR] + CategoryInfo : OpenError: (DevMachine:String) [], PSRemotingTr
[ERROR] ansportException
[ERROR] + FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

This is a reminder to find and document a proper fix for this.

This at least works:

Get-HotFix | Select-Object Source,Description,HotfixID,InstalledBy,InstalledOn,@{Name="InstalledOnDateTime";Expression={[System.DateTime]::Parse($_.PSBase.Properties["InstalledOn"].Value,[System.Globalization.CultureInfo]::GetCultureInfo("en-US"))}} | Out-GridView

Some links that hopefully help with proper documenting it:

–jeroen

Read the rest of this entry »

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

Boolean Values and Operators

Posted by jpluimers on 2019/09/26

TL;DR from [WayBackAutomating the world one-liner at a time… Boolean Values and Operators:

In PowerShell use the built-in constants $false and $true, as strings will be converted to booleans with results you don’t like

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

PowerShell: count the Windows EventLog entries for Applications over the last 10 minutes

Posted by jpluimers on 2019/09/25

In production, somehow an application started to misbehave, so would spit out a lot of Windows EventLog entries for Applications you can see in the EventViewer. This small script helped counting it (it takes about 10 seconds on a log having a total of 77k entries):

$tenMinutes = New-TimeSpan -Minutes 10
$now = Get-Date
$tenMinutesAgo = $now - $tenMinutes
$eventLogEntries = Get-EventLog -After $tenMinutesAgo -LogName "Application"
$count = ($eventLogEntries | Measure-Object).Count
Write-Host $count

Related:

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | 1 Comment »

how to filter name/value pairs under a registry key by name and value in PowerShell?

Posted by jpluimers on 2019/09/03

A very concise way for [WayBackhow to filter name/value pairs under a registry key by name and value in PowerShell?:

$path = 'hkcu:\Software\Microsoft\Windows\CurrentVersion\Extensions'
(Get-ItemProperty $path).PSObject.Properties |
  Where-Object { $_.Name -match '^xls' ` -or $_.Value -match 'msaccess.exe$' } |
  Select-Object Name, Value

Thanks montonero for getting me on that path and pointing me to the hidden PSObject property which by itself has Properties, and making me find these links with background information:

More in-depth information:

  • [WayBack] Get-Member (Microsoft.PowerShell.Utility)
    • The Get-Member cmdlet gets the members, the properties and methods, of objects. To specify the object, use the InputObject parameter or pipe an object to Get-Member. To get information about static members, the members of the class, not of the instance, use the Static parameter. To get only certain types of members, such as NoteProperties, use the MemberType parameter.
    • -Force

      Adds the intrinsic members (PSBase, PSAdapted, PSObject, PSTypeNames) and the compiler-generated get_ and set_ methods to the display. By default, Get-Member gets these properties in all views other than Base and Adapted, but it does not display them.

      The following list describes the properties that are added when you use the Force parameter:

      • PSBase: The original properties of the .NET Framework object without extension or adaptation. These are the properties defined for the object class and listed in MSDN.
      • PSAdapted. The properties and methods defined in the Windows PowerShell extended type system.
      • PSExtended. The properties and methods that were added in the Types.ps1xml files or by using the Add-Member cmdlet.
      • PSObject. The adapter that converts the base object to a Windows PowerShell PSObject object.
      • PSTypeNames. A list of object types that describe the object, in order of specificity. When formatting the object, Windows PowerShell searches for the types in the Format.ps1xml files in the Windows PowerShell installation directory ($pshome). It uses the formatting definition for the first type that it finds.
  • [WayBack] PSObject Class (System.Management.Automation)
    • Wraps an object providing alternate views of the available members and ways to extend them. Members can be methods, properties, parameterized properties, etc.
  • [WayBack] PSObject.Properties Property (System.Management.Automation)
    • Gets the Property collection, or the members that are actually properties.
      Is of type PSMemberInfoCollection<PSPropertyInfo>
  • [WayBack] PSMemberInfoCollection<T> Class
    • Serves as the collection of members in an PSObject or MemberSet
  • [WayBack] PSPropertyInfo Class (System.Management.Automation)
    • Serves as a base class for all members that behave like properties.
  • [WayBack] Difference between PSObject, Hashtable and PSCustomObject
  • [WayBack] Combining Objects Efficiently – Use a Hash Table to Index a Collection of Objects
    • With objects objects everywhere it may not seem apparent, but hash tables are still needed.  When the PowerShell mind sets to work it can be very easy to use where and selects everywhere to get you…
  • [Archive.isCustom objects default display in PowerShell 3.0
  • [WayBack] Using PSObject to store data in PowerShell | 9to5IT
    • PowerShell’s PSObject is a powerful tool which is used to store, retrieve, sort and export data. Here is how to use PSObject to store data in PowerShell.

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

When Powershell function won’t work: you define them with commas and parentheses, but call them with spaces and no parentheses

Posted by jpluimers on 2019/08/15

The function or command was called as if it were a method. 
Parameters should be separated by spaces. For information about 
parameters, see the about_Parameters Help topic.

Every now and then I bump into the above error. The reason is this:

  1. Functions are defined with commas between parameters and parentheses around them
  2. One-parameter functions can be called with one parameter surrounded by parentheses
  3. Multi-parameter functions need to be called with spaces between parameters and no parentheses surrounding them

Confused? #MeToo

The problem: [WayBackabout_Parameters_Default_Values | Microsoft Docs

Based on [WayBack] Powershell function won’t work.

–jeroen

Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

 
%d bloggers like this: