LoginSignup
7
8

More than 5 years have passed since last update.

powershellの自作コマンドレットを使う

Posted at

はじめに

IT企業に入社しましたが運用系にいるため開発は趣味として行っています。
まだプログラミング経験も半年だけなので分からないこと、間違ったことたくさんあると思うので
ご指導、ご指摘の程よろしくお願いします。

Ubuntu18.04を使っています。

問題・やりたいこと

Powershellの自作コマンドレットを使う

ExchangeOnelineを使うためにコマンドコピペを繰り返したりしているのを一発でやりたい

解決策・実施したこと

・コマンドレットを作成する > psmファイル
コマンドレットを作成するときはpsmファイルに記述しこれをImport-Moduleする

OriginalCmd.psm1
function Setup-Exo {
     $Cred = import-Clixml PassFile.xml
     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Cred -Authentication Basic -AllowRedirection
     Import-PSSession $Session -AllowClobber
}

といった感じにコマンドレットを定義します。

import-Module ./OriginalCmd.psm1 -function *

コマンドレットをインポートした後に実行する

PS /home/Office/PowerShell> Setup-EXO  
 ModuleType Version    Name                                ExportedCommands                                               
  ---------- -------    ----                                ----------------                                                 
Script     1.0        tmp_4rbiyq0d.ytl                    {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, A...

OK!

おわりに

次は高度な関数の定義がしたい。。。

7
8
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
7
8