サーバー上でリモート セッションを作成し、WMI を使用してサーバーの IIS アプリケーション プールの一覧を取得し、それらの名前を一覧表示するサンプル スクリプトを次に示します。
function Test-Remoting
{
[CmdletBinding()]
param
(
)
begin
{
Enter-PSSession TestServer
$appPools = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" -Authentication 6
$appPools | ForEach-Object {
$appPool = $_;
$appPool.Name
}
Exit-PSSession
}
}
この関数は、「Test-Remoting.ps1」というファイルに含まれています。PowerShell を開き、CD からこのファイルを含むディレクトリに移動し、ファイルをドット ソース形式で読み込んで、関数を呼び出します。
PS C:\Users\moskie> . .\Test-Remoting.ps1
PS C:\Users\moskie> Test-Remoting
しかし、このスクリプトの結果は、 TestServerではなく、ローカルマシン上のアプリケーション プールのリストです。
または、PowerShell プロンプトで次の行 (関数内の行と同じ) を手動で実行すると、リモート サーバー上のアプリ プールのリストが取得されます。
PS C:\Users\moskie> Enter-PSSession TestServer
[TestServer]: PS C:\> $appPools = Get-WmiObject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" -Authentication 6
[TestServer]: PS C:\> $appPools | ForEach-Object { $appPool = $_; $appPools.Name }
<a list of the names of the application pools on TestServer>
[TestServer]: PS C:\>
PowerShell のリモート処理とスコープに関して、私が忘れている概念があると思います。誰でもこの動作を説明できますか?