Archive for the ‘CommandLine’ Category
Posted by jpluimers on 2018/12/25
Works on translated systems:
PowerShell
Get-EventLog System | Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} | ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
Or on one line:
Get-EventLog System ^| Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} ^| ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
I’ve adapted the custom view to include all these event IDs above:
- 12: The operating system started at system time <iso8601utc>.
- 13: The operating system is shutting down at system time <iso8601utc>.
- 109: The kernel power manager has initiated a shutdown transition.
- 1074: [WayBack] The process <process> has initiated the restart of <computer name> for the following reason: No title for this reason could be found.
Minor Reason: <reason>
Shutdown Type: <type>
- 1076: [WayBack] The reason supplied by user <user name> for the last unexpected shutdown of this computer is: <error description>
Reason Code: <error code>
Bug ID: <bug id>
Bugcheck String: <string>
Comment: <comment>
- 6008: [WayBack] The previous system shutdown at <time> on <date> was unexpected.
Steps for the custom view:
Open Event Viewer then
- Right click Custom Views
- Click Create Custom View
- Under the Filter tab
- Keep Logged as Any time
- Select all the Event level types (Critical, Warning, etc.)
- Choose by source = Windows Logs > System
- For Event ID under the Includes/Excludes Event IDs section enter 12,13,1074,1076,6008 for the Event ID
- Click Ok
- Enter a name like Shutdown Events and any description then
- Click Ok again to complete the custom event log.
Your new custom view should show up in the list of custom views with the correct filter applied.
Source: [WayBack] eventviewer – View Shutdown Event Tracker logs under Windows Server 2008 R2 – Server Fault
–jeroen
Posted in CommandLine, Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2018/10/31
Due some issues in Windows, every now and then the Windows TEMP directory gets huge.
This script helps measuring the recursive size of that folder:
$WindowPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Windows)
$WindowTempPath = Join-Path -Path $WindowPath -ChildPath "TEMP"
$Result = Get-ChildItem $WindowTempPath -Recurse | Measure-Object -Property Length -Sum
$RecursiveSumInBytes = $Result.Sum
Write-Host "$RecursiveSumInBytes"
It uses these tricks:
- Accessing native .NET types; in this case [WayBack] Environment.SpecialFolder Enumeration (System) to get the “The Windows directory or SYSROOT. This corresponds to the %windir% or %SYSTEMROOT% environment variables. Added in the .NET Framework 4.”
- Assuming the Windows TEMP directory is always named that way.
- Using [WayBack] Join-Path to combine a base path with a child path without worrying about the path delimiter.
- Recursively enumerating all items in that folder using [WayBack] Get-ChildItem.
- Aggregating with [WayBack] Measure-Object over the
Length property of each Child-Item to determine their Sum.
After this, cleaning up uses two more tricks:
$WindowPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Windows)
$WindowTempPath = Join-Path -Path $WindowPath -ChildPath "TEMP"
Get-ChildItem $WindowTempPath -Recurse | foreach { Remove-Item $_.FullName -Recurse }
Inspired by:
–jeroen
Posted in CommandLine, Development, PowerShell, Software Development | Leave a Comment »
Posted by jpluimers on 2018/08/28
Since it was not possible to install PowerShell 3 on ancient Windows Server 2003 and Windows Server 2003 R2 machines, I opted for this workaround during the time they were being retired:
I’ve investigating how much work it will be to migrate the machine, as opposed to adapting the scripts with Poshcode/Jaykul modules (of which many have external dependencies that I’d need to check first). It’s about the same order of magnitude, so I’ll be migrating the machine earlier. In the mean time, a different machine will run the scripts and access the required data over a network share.
Source: [WayBack] Is it possible to install PowerShell 3 on a Windows Server 2003 or 2003 R2? – Super User
Posted in CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2018/07/30
I needed all .dproj files in all subdirectories, but only their filenames without any directory names.
With directory names, this is easy:
dir /s /b *.dproj
The answers at [WayBack] windows – How to list all files in directory/subdirectory without path name CMD? – Stack Overflow give the below kind of output.
[WayBack] forfiles embeds all filenames within quotes:
forfiles /m *.dproj /s
"Foo.dproj"
"Bar.dproj"
A more convoluted [WayBack] for loop gives them without quotes where n stands for name and x for extension including .:
for /r %a in (*.dproj) do @echo %~nxa
Foo.dproj
Bar.dproj
–jeroen
Posted in Batch-Files, CommandLine, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2018/07/17
I wasn’t expecting it to be so easy to install PowerShell on Mac OS X:
brew install Caskroom/cask/powershell
In the background it executes this script: https://github.com/caskroom/homebrew-cask/blob/master/Casks/powershell.rb. which indirectly goes through the URL template https://github.com/PowerShell/PowerShell/releases/download/v#{version}/powershell-#{version}.pkg.
On other non-Windows systems, you have to go through GitHub yourself: https://github.com/powershell/PowerShell. The PowerShell team at Microsoft has many more repositories including the Win32-OpenSSH port which you can find through https://github.com/PowerShell.
At the time of writing, PowerShell was available for these platforms:
The first version I installed on Mac OS X was this: ==> Downloading https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.17/powershell-6.0.0-alpha.17.pkg
By now I really hope it is out of Alpha state.
–jeroen
via:
Posted in *nix, Apple, CommandLine, Development, iMac, Linux, Mac, Mac OS X / OS X / MacOS, MacBook, MacBook Retina, MacBook-Air, MacBook-Pro, MacMini, openSuSE, Power User, PowerShell, PowerShell, Scripting, Software Development, SuSE Linux, Ubuntu | Leave a Comment »
Posted by jpluimers on 2018/06/19
Thanks [WayBack] gbabu for the below PowerShell ide
As PowerShell command:
Get-EventLog System | Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} | ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
Based on it and my own experience, thse Event IDs can be interesting:
- 41 – The system has rebooted without cleanly shutting down first
- 109 – The kernel power manager has initiated a shutdown transition.
- 1073 – The attempt by user [domain]\[username] to restart/shutdown computer [computername] failed.
- 1074 – The process [filename].[extension] has initiated the restart of computer [computername] on behalf of user [domain]\[username\ for the
- 1076 – ???
- 6008 – The previous system shutdown at [time-in-local-format] on [date-in-local-format] was unexpected.
You can also run this as a batch file, but not you need to escape the pipe | into ^| like this:
PowerShell Get-EventLog System ^| Where-Object {$_.EventID -eq "1074" -or $_.EventID -eq "6008" -or $_.EventID -eq "1076"} ^| ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
If you have PowerShell 3.0 or greater, then you can use the [Archive.is] -In operator:
PowerShell Get-EventLog System ^| Where-Object {$_.EventID -in "41", "109", "1074", "6008", "1076"} ^| ft Machinename, TimeWritten, UserName, EventID, Message -AutoSize -Wrap
–jeroen
Posted in Batch-Files, CommandLine, Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2018/03/26
I forgot to blog about this before, but 2 months ago PowerShell core came available: [WayBack] PowerShell Core 6.0: Generally Available (GA) and Supported! | PowerShell Team Blog.
[WayBack] Installing PowerShell Core on macOS and Linux | Microsoft Docs is easy (one way is through homebrew:
$ brew tap caskroom/cask
$ brew cask install powershell
If you already installed a beta, then the steps are these:
$ brew update
$ brew cask reinstall powershell
Note that after installation, it is known as pwsh (at least one of the betas named it powershell) to set PowerShell Core apart from PowerShell*:
$ pwsh --version
PowerShell v6.0.2
Via: [WayBack] PowerShell Core 6.0 is a new edition of PowerShell that is cross-platform (Windows, macOS, and Linux), open-source, and built for heterogeneous environm… – Lars Fosdal – Google+
*pwsh versus powershell
There has been quite a discussion on the PowerShell Core repository on the rename, but I think it is for a good reason.
Too bad that during part of the beta, the old name powershell was used, but beta-time means things break every now and then.
PowerShell Core is sufficiently different from prior PowerShell versions to warrant a name change. This also makes it a lot easier to use them side-by-side.
Many other names (like posh, pcsh or psh) were considered, usually because of naming conflicts with existing tools (like posh) or easy confusion with existing shells (like pcsh and csh). A benefit on Linux/macOS is that it now ends with sh like virtually all other shells.
More background information is at:
–jeroen
Posted in Apple, CommandLine, Development, Home brew / homebrew, Mac, Mac OS X / OS X / MacOS, MacBook, MacBook Retina, MacBook-Air, MacBook-Pro, MacMini, Power User, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2018/03/21
About a year ago, [WayBack] Rumors of Cmd’s death have been greatly exaggerated – Windows Command Line Tools For Developers got published as a response to confusing posts like these:
But I still think it’s a wise idea to switch away from the Cmd and to PowerShell as with PowerShell you get way more consistent language features, far better documentation, truckloads of new features (of which I like the object pipeline and .NET interoperability most) and far fewer quirks.
It’s time as well, as by now, Windows 7 has been EOL for a while, and Windows 8.x is in extended support: [WayBack] Windows lifecycle fact sheet – Windows Help:
| Client operating systems |
Latest update or service pack |
End of mainstream support |
End of extended support |
| Windows XP |
Service Pack 3 |
April 14, 2009 |
April 8, 2014 |
| Windows Vista |
Service Pack 2 |
April 10, 2012 |
April 11, 2017 |
| Windows 7* |
Service Pack 1 |
January 13, 2015 |
January 14, 2020 |
| Windows 8 |
Windows 8.1 |
January 9, 2018 |
January 10, 2023 |
| Windows 10, released in July 2015** |
N/A |
October 13, 2020 |
October 14, 2025 |
Which means the PowerShell version baseline on supported Windows versions is at least 4.0: [Archive.is] windows 10 powershell version – Google Search and [WayBack] PowerShell versions and their Windows version – 4sysops
PowerShell and Windows versions ^
| PowerShell Version |
Release Date |
Default Windows Versions |
| PowerShell 2.0 |
October 2009 |
Windows 7 Windows Server 2008 R2 (**) |
| PowerShell 3.0 |
September 2012 |
Windows 8 Windows Server 2012 |
| PowerShell 4.0 |
October 2013 |
Windows 8.1 Windows Server 2012 R2 |
| PowerShell 5.0 |
April 2014 (***) |
Windows 10 |
So try PowerShell now. You won’t regret it.
–jeroen
via: [WayBack] Very interesting clear-up post and comments on CMD, command.com, PowerShell in past and future DOS/Windows versions and Unix shells altogether. – Ilya S – Google+
Posted in Batch-Files, CommandLine, Development, Power User, PowerShell, Scripting, Software Development, Windows, Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016 | Leave a Comment »