0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

powershell 実行したらMethod invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.というエラーが出た

Last updated at Posted at 2023-04-02

事象

乱数を10個出力する内容を実行したところ以下のエラーが発生しました。

$r-1..10 | %{get-random}
$r
InvalidOperation: /(フォルダパス)/new.ps1:1
Line |
   1 |  $r-1..10 | %{get-random}
     |  ~~~~~~~~~~~~~~~~~~~~~~~~
     | Method invocation failed because [System.Object[]] does not
     | contain a method named 'op_Subtraction'.

うーんエラーの内容を直訳すると「
[System.Object] がないため、メソッドの呼び出しに失敗しました」という内容です。
なんじゃそりゃ。。

対処策

「r-1..10」の「-」を「=」に修正したら解決しました。

$r=1..10 | %{get-random}
$r

xxx
~
xxxxx
計10個の乱数が出力

雑感

他のプログラミング言語も同様なのですが、エラー文の内容を馬鹿正直に解読しようとするのではなく、
そもそも文法的におかしい点がある、何かファイルが作成されていないという観点を常に持ち合わせておくことが
大切だと改めて感じました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?