ध्यान दें:
निम्न समाधान किसी भी बाहरी प्रोग्राम के साथ काम करता है , और आउटपुट को टेक्स्ट के रूप में हमेशा कैप्चर करता है ।
किसी अन्य PowerShell उदाहरण को आमंत्रित करने के लिए और इसके आउटपुट को रिच ऑब्जेक्ट्स (सीमाओं के साथ) के रूप में कैप्चर करने के लिए , नीचे अनुभाग में वेरिएंट समाधान देखें या Mathias R. Jessen के सहायक उत्तर पर विचार करें , जो PowerShell SDK का उपयोग करता है ।
स्मृति में प्रक्रिया आउटपुट को कैप्चर करने के लिए .NET और .NET प्रकार के प्रत्यक्ष उपयोग केSystem.Diagnostics.Process
System.Diagnostics.ProcessStartInfo
आधार पर यहां एक अवधारणा है (जैसा कि आपके प्रश्न में कहा गया है, Start-Process
विकल्प नहीं है, क्योंकि यह केवल फाइलों में आउटपुट कैप्चर करने का समर्थन करता है , जैसा कि इस उत्तर में दिखाया गया है ) :
ध्यान दें:
एक अलग उपयोगकर्ता के रूप में चलने के कारण, यह केवल विंडोज़ पर समर्थित है (.NET कोर 3.1 के रूप में), लेकिन पावरशेल संस्करणों में दोनों।
एक अलग उपयोगकर्ता के रूप में चलाने के लिए और आउटपुट पर कब्जा करने की आवश्यकता के कारण, छिपी हुई.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
# (user@doamin.com), 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
उपरोक्त कुछ निम्न की तरह देता है, जिसमें दिखाया गया है कि यह प्रक्रिया दी गई उपयोगकर्ता पहचान के साथ सफलतापूर्वक चलती है:
Running `cmd /c echo %USERNAME%` as user jdoe yielded:
jdoe
जब से आप एक और PowerShell उदाहरण को कॉल कर रहे हैं , आप CLIXML प्रारूप में आउटपुट का प्रतिनिधित्व करने के लिए PowerShell CLI की क्षमता का लाभ उठाना चाह सकते हैं , जो आउटपुट को समृद्ध वस्तुओं में निष्क्रिय करने की अनुमति देता है , सीमित प्रकार की निष्ठा के साथ , जैसा कि इस संबंधित उत्तर में बताया गया है। ।
# 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
# (user@doamin.com), 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
को इस तरह से deserialized किया गया था:
Running `Get-Date` as user jdoe yielded:
Friday, March 27, 2020 6:26:49 PM
as data type:
System.DateTime