More powershell madness

I keep all my small utilities (gnu stuff, tcpview, process explorer and more) in a separated folder, so I can sync them a cloud service. The problem is that every time I want to access any of these tools, I need to navigate to that folder. I made this script in Powershell to collect all *.exe filenames and create a shortcut to each of them in a destination folder, that way we can keep all shortcuts at once in the start menu 🙂

 

$destDir="$($env:APPDATA)\Microsoft\Windows\Start Menu\Programs\MyUtilities"
$executablesRootPath="c:\MyAppsPath"
New-Item -ItemType Directory -Force -Path $destDir
$executablesDir = get-childitem $executablesRootPath -recurse
$executables = $executablesDir | where {$_.extension -eq ".exe"}
Foreach ($executable in $executables) {
  $shortName=$executable.Name.Replace(".exe", ".lnk")
  $shortcutPath="$($destDir)\$($shortName)"
  $ws = New-Object -ComObject WScript.Shell
  $newShortCut = $ws.CreateShortcut($shortcutPath)
  $newShortCut.TargetPath = $executable.FullName
  $newShortCut.Save()
}

 

MacOS High Sierra and “Too Many Open Files”

While preparing next version of Dinamica EGO for Mac, I was facing the “Too Many Open files” error with a couple of boost::filesystem::stream‘s. Turns out, Apple limits the number of handles you can open in every application (in my installation, up to 256 handles). You can check how many handles your process support by using:

ulimit -a

and checking the “open files” line. To overcome this limitation, I found this awesome blog post, which is still valid on MacOS High Sierra.

To prevent myself (and readers) from losing the details, I am copying the important bits from Dejan Kitic blog:

Create the file /Library/LaunchDaemons/limit.maxfiles.plist with the contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>limit.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

and the file /Library/LaunchDaemons/limit.maxproc.plist with the contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>Label</key>
        <string>limit.maxproc</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxproc</string>
          <string>2048</string>
          <string>2048</string>
        </array>
      <key>RunAtLoad</key>
        <true />
      <key>ServiceIPC</key>
        <false />
    </dict>
  </plist>

Then, change the ownership of both files:

# sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
# sudo launchctl load -w /Library/LaunchDaemons/limit.maxproc.plist

And restart. Credit goes to Dejan Kitic and Gilles Celli for the solution.

Enabling and Disabling Windows Services automagically.

I got tired of enabling/disabling VMware services every time, so I wrote a little PowerShell script for this:

Change services mode to manual initialization and start them:

$services="vmauthd*","VMnetDHCP*","vmware*","VMUSBArbService*"
foreach ($service in $services) {
    gwmi win32_service|?{$_.name -like $service}|%{$_.changestartmode("Manual")}
    gwmi win32_service|?{$_.name -like $service}|%{$_.startservice()}
}

Stop services and disable their initialization settings:

$services="vmauthd*","VMnetDHCP*","vmware*","VMUSBArbService*"
foreach ($service in $services) {
    gwmi win32_service|?{$_.name -like $service}|%{$_.stopservice()}
    gwmi win32_service|?{$_.name -like $service}|%{$_.changestartmode("Disabled")}
}

Attention: You need to run the scripts in administrator mode.

ANSI colors in Windows terminal

In case you are wondering how to get your ANSI colored strings into windows terminal, the following snippet might help you (from C):

#include <stdio.h>
#include <windows.h>

#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif

int main(int argc, char** argv) {
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  DWORD dwMode = 0;

  GetConsoleMode(hOut, &dwMode);
  dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

  SetConsoleMode(hOut, dwMode);

  printf("\x1b[101;93m Testing\x1b[0m\n");
  return 0;
}

More info on escape sequences can be found on https://en.wikipedia.org/wiki/ANSI_escape_code