Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Amazon Kindle Paperwhiteが4G回線で見られるのはWikipediaだけ

Amazon Kindle Paperwhite (4th Gen)は

  • 内蔵のブラウザでWebサイトが(いちおう)見られる
  • 4Gモデルの通信料はAmazon持ち

と聞いていたので、「TwitterとかをKindleで見るようにすれば通信料が節約できるかも!」と思ったのですが、そうではありませんでした。

Wikipediaだけは、本文中の単語を調べるときにも使うため、4G回線でも見られます。ですが、その他のサイト(Twitterとか)は、Wi-Fi接続しているときしか参照できません。

Wikipediaを一日中見てる!みたいな場合を除いて、通信料の節約には使えなさそうです。

Change culture (locale) of current PowerShell process

You can change culture (locale) into English with chcp 437 in Windows 7.

In contrast, You cannot change culture with chcp in Windows 10.

In Windows 10, if you want to execute single PowerShell script in another culture, you can execute the script like this:

[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'; .\myscript.ps1

If you want to change culture throughout every command in a PowerShell process, you have to change the culture setting cached in PowerShell runtime with reflection like this:

# example: Set-PowerShellUICulture -Name "en-US"

function Set-PowerShellUICulture {
    param([Parameter(Mandatory=$true)]
          [string]$Name)

    process {
        $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture($Name)
       
        $assembly = [System.Reflection.Assembly]::Load("System.Management.Automation")
        $type = $assembly.GetType("Microsoft.PowerShell.NativeCultureResolver")
        $field = $type.GetField("m_uiCulture", [Reflection.BindingFlags]::NonPublic -bor [Reflection.BindingFlags]::Static)
        $field.SetValue($null, $culture)
    }
}

Note: Some cmdlets like Add-Type are not affected this setting.

(from https://gist.github.com/sunnyone/7486486 via https://twitter.com/altrive/status/401349992536756224?s=20)

Import assemblies for C# embedded in PowerShell

You can import assemblies (.NET DLLs) in PowerShell like this:

[void][reflection.assembly]::LoadWithPartialName("System.Drawing")
New-Object System.Drawing.Drawing2D.GraphicsPath

But you will get an error when you tring to use these assemblies in C# source embedded in PowerShell script. Assemblies loaded with LoadWithPartialName aren't referenced from embedded source.

[void][reflection.assembly]::LoadWithPartialName("System.Drawing")
Add-Type @"
using System.Drawing.Drawing2D;
"@
Add-Type : c:\tmp\_System\ama4povk.0.cs(1) : The type or namespace name 'Drawing2D' could not be found (are you missing a using directive or an assembly reference?)
c:\tmp\_System\ama4povk.0.cs(1) : >>> using System.Drawing.Drawing2D;
At line:1 char:1
+ Add-Type @"
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
    + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type @"
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
    + FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

With Add-Type, you have to specify assemblies with -ReferencedAssemblies option like this:

Add-Type -ReferencedAssemblies ("System.Drawing") @"
using System.Drawing.Drawing2D;
"@

mvn command results in error in Windows

You will get error when you run following Maven command from Windows PowerShell:

PS C:\workspace> mvn -B archetype:generate -DgroupId=com.example -DartifactId=hajiboot -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.069 s
[INFO] Finished at: 2019-11-14T23:51:13+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

You will see the following message with -e -X options:

PS C:\workspace> mvn -B archetype:generate -DgroupId=com.example -DartifactId=hajiboot -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -e -X
Apache Maven 3.6.2 (40f52333136460af0dc0d7232c0dc0bcf0d9e117; 2019-08-28T00:06:16+09:00)
Maven home: C:\opt\maven\bin\..
Java version: 1.8.0_212, vendor: AdoptOpenJDK, runtime: C:\opt\java\AdoptOpenJDK\8\jre
(snip)
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory. -> [Help 1]
org.apache.maven.lifecycle.MissingProjectException: The goal you specified requires a project to execute but there is no POM in this directory (C:\home\satob\workspace). Please verify you invoked Maven from the correct directory.
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:85)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

To solve this problem, quote all options like:

PS C:\workspace> mvn -B archetype:generate "-DgroupId=com.example" "-DartifactId=hajiboot" "-Dversion=1.0.0-SNAPSHOT" "-DarchetypeArtifactId=maven-archetype-quickstart"

cf. https://stackoverflow.com/a/11199865/3902663