スタックコミュニティこんにちは:)
私はシンプルな目標を持っています。私は別のPowerShellのスクリプトからいくつかのPowerShellスクリプトを起動したいのですが、3つの条件があります。
- 私は(実行が特定のユーザを持つデータベースに接続)の資格情報を渡す必要が
- これは、いくつかのパラメータを取るために持っています
- 私は変数に出力を渡したいのですが
同様の質問がありますリンク。しかし、答えは2つのPSスクリプト間で通信するための方法として、ファイルを使用することです。私は、アクセスの競合を回避したいと思います。@Update:メインスクリプトは、いくつかの他のスクリプトを開始する予定です。実行は、同時に複数のユーザーから実行される場合はそのファイルを持つソリューションは、注意が必要です。
Script1.ps1は、出力として文字列を持っている必要がありますスクリプトです。(私は例を作りたかったので、ちょうど、それは本当の1が150行を持っている、架空のスクリプトですが、明確にするために)
param(
[String]$DeviceName
)
#Some code that needs special credentials
$a = "Device is: " + $DeviceName
$a
ExecuteScripts.ps1は、これら3つの条件は、上記とそのいずれかを呼び出す必要があり
私は、複数の解決策を試してみました。examplteのためのこの1:
$arguments = "C:\..\script1.ps1" + " -ClientName" + $DeviceName
$output = Start-Process powershell -ArgumentList $arguments -Credential $credentials
$output
私はそれから任意の出力を得ることはありませんし、私はちょうどでスクリプトを呼び出すことはできません
&C:\..\script1.ps1 -ClientName PCPC
私は渡すことはできませんので-Credential
、それにパラメータ..
前もって感謝します!
注意:
次のソリューションは、で動作する任意の外部プログラム、およびとして常に出力をキャプチャしたテキスト。
するために呼び出す別のPowerShellのインスタンスをし、その出力をキャプチャする豊富なオブジェクトとして(制限付き)、下部にバリアントのソリューションを参照するかを検討マティアスR.ジェッセンの役に立つ答え使用して、PowerShellのSDKを。
ここだ概念実証の直接使用に基づくSystem.Diagnostics.Process
とSystem.Diagnostics.ProcessStartInfo
.NETの種類:
注意:
原因別のユーザーとして実行するには、これは上でサポートされているだけのWindows(.NETのコア3.1のように、両方のPowerShellのエディションであり)。
別のユーザーとして実行する必要と出力をキャプチャする必要に起因
.WindowStyle
コマンドを実行するために使用することができない隠れを(使用するため.WindowStyle
必要.UseShellExecute
であることが$true
、これらの要件と互換性があります)しかしながら、すべての出力がされている捕捉設定する、.CreateNoNewWindow
ために$true
効果的に隠された実行をもたらします。
# Get the target user's name and password.
$cred = Get-Credential
# Create a ProcessStartInfo instance
# with the relevant properties.
$psi = [System.Diagnostics.ProcessStartInfo] @{
# For demo purposes, use a simple `cmd.exe` command that echoes the username.
# See the bottom section for a call to `powershell.exe`.
FileName = 'cmd.exe'
Arguments = '/c echo %USERNAME%'
# Set this to a directory that the target user
# is permitted to access.
WorkingDirectory = 'C:\' #'
# Ask that output be captured in the
# .StandardOutput / .StandardError properties of
# the Process object created later.
UseShellExecute = $false # must be $false
RedirectStandardOutput = $true
RedirectStandardError = $true
# Uncomment this line if you want the process to run effectively hidden.
# CreateNoNewWindow = $true
# Specify the user identity.
# Note: If you specify a UPN in .UserName
# ([email protected]), set .Domain to $null
Domain = $env:USERDOMAIN
UserName = $cred.UserName
Password = $cred.Password
}
# Create (launch) the process...
$ps = [System.Diagnostics.Process]::Start($psi)
# Read the captured standard output.
# By reading to the *end*, this implicitly waits for (near) termination
# of the process.
# Do NOT use $ps.WaitForExit() first, as that can result in a deadlock.
$stdout = $ps.StandardOutput.ReadToEnd()
# Uncomment the following lines to report the process' exit code.
# $ps.WaitForExit()
# "Process exit code: $($ps.ExitCode)"
"Running ``cmd /c echo %USERNAME%`` as user $($cred.UserName) yielded:"
$stdout
プロセスが正常に与えられたユーザーIDで実行されたことを示す次のような上記の利回り何か、:
Running `cmd /c echo %USERNAME%` as user jdoe yielded:
jdoe
以来、あなたが別の呼び出しているPowerShellのインスタンスを、あなたがしたいことの利点を活用PowerShellのCLIへの出力をデシリアライズできますCLIXML形式で出力を表現するための能力、豊富なオブジェクト限られた種類の忠実性はあるものの、などで説明し、この関連の回答。
# Get the target user's name and password.
$cred = Get-Credential
# Create a ProcessStartInfo instance
# with the relevant properties.
$psi = [System.Diagnostics.ProcessStartInfo] @{
# Invoke the PowerShell CLI with a simple sample command
# that calls `Get-Date` to output the current date as a [datetime] instance.
FileName = 'powershell.exe'
# `-of xml` asks that the output be returned as CLIXML,
# a serialization format that allows deserialization into
# rich objects.
Arguments = '-of xml -noprofile -c Get-Date'
# Set this to a directory that the target user
# is permitted to access.
WorkingDirectory = 'C:\' #'
# Ask that output be captured in the
# .StandardOutput / .StandardError properties of
# the Process object created later.
UseShellExecute = $false # must be $false
RedirectStandardOutput = $true
RedirectStandardError = $true
# Uncomment this line if you want the process to run effectively hidden.
# CreateNoNewWindow = $true
# Specify the user identity.
# Note: If you specify a UPN in .UserName
# ([email protected]), set .Domain to $null
Domain = $env:USERDOMAIN
UserName = $cred.UserName
Password = $cred.Password
}
# Create (launch) the process...
$ps = [System.Diagnostics.Process]::Start($psi)
# Read the captured standard output, in CLIXML format,
# stripping the `#` comment line at the top (`#< CLIXML`)
# which the deserializer doesn't know how to handle.
$stdoutCliXml = $ps.StandardOutput.ReadToEnd() -replace '^#.*\r?\n'
# Uncomment the following lines to report the process' exit code.
# $ps.WaitForExit()
# "Process exit code: $($ps.ExitCode)"
# Use PowerShell's deserialization API to
# "rehydrate" the objects.
$stdoutObjects = [Management.Automation.PSSerializer]::Deserialize($stdoutCliXml)
"Running ``Get-Date`` as user $($cred.UserName) yielded:"
$stdoutObjects
"`nas data type:"
$stdoutObjects.GetType().FullName
ことを示す次のような出力何か、上記[datetime]
インスタンス(System.DateTime
)出力によっては、Get-Date
のようなデシリアライズされました。
Running `Get-Date` as user jdoe yielded:
Friday, March 27, 2020 6:26:49 PM
as data type:
System.DateTime