{"id":304672,"date":"2020-06-01T21:00:55","date_gmt":"2020-06-01T21:00:55","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=304672"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=304672","title":{"rendered":"\u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 powershell invoke-command \u0430\u0433\u0435\u043d\u0442\u0443 SQL-Server"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/504890\/\">\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u043c\u0438 \u043a\u043e\u043f\u0438\u044f\u043c\u0438 \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 MS-SQL \u044f \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043b \u043a\u0443\u0447\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 powershell \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0438\u0448\u0443 \u0441\u0430\u043c\u043e\u043c\u0443 \u0441\u0435\u0431\u0435 \u043f\u0430\u043c\u044f\u0442\u043a\u0443, \u0430 \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443-\u0442\u043e \u0435\u0449\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"powershell\">$exitcode = $args[0] Write-Host 'Out to host.' Write-Output 'Out to output.' Write-Host ('ExitCode: ' + $exitcode) Write-Output $exitcode $host.SetShouldExit($exitcode)<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c CMD-\u0444\u0430\u0439\u043b\u043e\u043c, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0435\u0433\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0441\u0442\u0430\u043d\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"dos\">@Echo OFF PowerShell .\\TestOutput1.ps1 1 ECHO ERRORLEVEL=%ERRORLEVEL%<\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"plaintext\">Out to host. Out to output. ExitCode: 1 1 ERRORLEVEL=1<\/code><\/pre>\n<p><a name=\"habracut\"><\/a><br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u044d\u0442\u043e\u0442 \u0436\u0435 \u0441\u043a\u0440\u0438\u043f\u0442 \u0447\u0435\u0440\u0435\u0437 WSMAN (\u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e):<\/p>\n<pre><code class=\"powershell\">Invoke-Command -ComputerName . -ScriptBlock { &'D:\\sqlagent\\TestOutput1.ps1' $args[0] } -ArgumentList $args[0]<\/code><\/pre>\n<p>  \u0418 \u0432\u043e\u0442 \u0432\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"plaintext\">Out to host. Out to output. ExitCode: 2 2 ERRORLEVEL=0<\/code><\/pre>\n<p>  \u0427\u0443\u0434\u0435\u0441\u043d\u043e, Errorlevel \u043a\u0443\u0434\u0430-\u0442\u043e \u043f\u0440\u043e\u043f\u0430\u043b, \u043d\u043e \u043d\u0430\u043c \u0432\u0435\u0434\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u043a\u0440\u0438\u043f\u0442\u0430! \u041f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"powershell\">$res=Invoke-Command -ComputerName . -ScriptBlock { &'D:\\sqlagent\\TestOutput1.ps1' $args[0] } -ArgumentList $args[0]<\/code><\/pre>\n<p>  \u0422\u0443\u0442 \u0435\u0449\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u0412\u0435\u0441\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u0432 Output \u043a\u0443\u0434\u0430-\u0442\u043e \u0438\u0441\u0447\u0435\u0437:<\/p>\n<pre><code class=\"plaintext\">Out to host. ExitCode: 2 ERRORLEVEL=0<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043b\u0438\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Powershell \u0412\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u0442\u0435 Write-Output \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f \u0435\u0433\u043e \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 (\u0430 \u044d\u0442\u043e \u043d\u0435\u044f\u0432\u043d\u043e \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u0432 \u043a\u0430\u043d\u0430\u043b Output), \u0442\u043e \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u043e! \u042d\u0442\u043e \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b powershell \u2014 \u043a\u0430\u0436\u0434\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 Output, \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432, \u0438 \u0432\u0441\u0435, \u0447\u0442\u043e \u0432 \u043d\u0435\u0433\u043e \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442, \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 Return \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u044d\u0442\u043e\u0442 \u0436\u0435 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0432\u044b\u0437\u0432\u0430\u0432\u0448\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0414\u043b\u044f \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442:<\/p>\n<pre><code class=\"powershell\">Function Write-Log {   Param( [Parameter(Mandatory=$false, ValueFromPipeline=$true)] [String[]] $OutString = &quot;`r`n&quot; )   Write-Output (&quot;Function: &quot;+$OutString)   Return &quot;ReturnValue&quot; } Write-Output (&quot;Main: &quot;+&quot;ParameterValue&quot;) $res = Write-Log &quot;ParameterValue&quot; $res.GetType() $res.Length $res | Foreach-Object { Write-Host (&quot;Main: &quot;+$_) } <\/code><\/pre>\n<p>  \u0418 \u0432\u043e\u0442 \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"plaintext\">Main: ParameterValue  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Object[]                                 System.Array 2 Main: Function: ParameterValue Main: ReturnValue<\/code><\/pre>\n<p>  \u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f (\u0442\u0435\u043b\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430) \u0442\u0430\u043a\u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u0439 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 Output, \u0438 \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438\u0437 CMD, \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0432 \u0432\u044b\u0432\u043e\u0434 \u0432 \u0444\u0430\u0439\u043b, <\/p>\n<pre><code class=\"dos\">PowerShell .\\TestOutput1.ps1 1 &gt; TestOutput1.txt <\/code><\/pre>\n<p>  \u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c <\/p>\n<pre><code class=\"plaintext\">ERRORLEVEL=1<\/code><\/pre>\n<p>  \u0430 \u0432 \u0444\u0430\u0439\u043b\u0435   <\/p>\n<pre><code class=\"plaintext\">Out to host. Out to output. ExitCode: 1 1 <\/code><\/pre>\n<p>  \u0435\u0441\u043b\u0438 \u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0438\u0437 powershell <\/p>\n<pre><code class=\"powershell\">PS D:\\sqlagent&gt; .\\TestOutput1.ps1 1 &gt; TestOutput1.txt<\/code><\/pre>\n<p>  \u0442\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435 \u0431\u0443\u0434\u0435\u0442 <\/p>\n<pre><code class=\"plaintext\">Out to host. ExitCode: 1<\/code><\/pre>\n<p>  \u0430 \u0432 \u0444\u0430\u0439\u043b\u0435 <\/p>\n<pre><code class=\"plaintext\">Out to output. 1<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e CMD \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 powershell, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0439 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 (Host \u0438 Output) \u0438 \u043e\u0442\u0434\u0430\u0435\u0442 \u0438\u0445 CMD, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u0444\u0430\u0439\u043b \u0432\u0441\u0435, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438\u0437 powershell \u044d\u0442\u0438 \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u0438 \u0441\u0438\u043c\u0432\u043e\u043b \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u043b\u0438\u044f\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Output.<\/p>\n<p>  \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0442\u0435\u043c\u0435, \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c .NET \u0432\u043d\u0443\u0442\u0440\u0438 powershell \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 (\u043e\u0434\u043d\u043e\u0439 \u041e\u0421), \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043a\u043e\u0434\u0430 \u0447\u0435\u0440\u0435\u0437 WSMAN \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 XML-\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0447\u0442\u043e \u0432\u043d\u043e\u0441\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u0432 \u043d\u0430\u0448\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"powershell\">$res=Invoke-Command -ComputerName . -ScriptBlock { &'D:\\sqlagent\\TestOutput1.ps1' $args[0] } -ArgumentList $args[0] $res.GetType() $host.SetShouldExit($res) <\/code><\/pre>\n<p>  \u0418 \u0432\u043e\u0442 \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435:<\/p>\n<pre><code class=\"plaintext\">Out to host.  ExitCode: 3  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Object[]                                 System.Array \u041d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 &quot;exitCode&quot;, \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c: &quot;System.Object[]&quot;, \u0434\u043b\u044f &quot;SetShouldExit&quot; \u0432 \u0442\u0438\u043f &quot;System.Int32&quot;: &quot;\u041d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 &quot;System.Object[]&quot; \u0442\u0438\u043f\u0430 &quot;System.Object[]&quot; \u0432 \u0442\u0438\u043f &quot;System .Int32&quot;.&quot; D:\\sqlagent\\TestOutput3.ps1:3 \u0437\u043d\u0430\u043a:1 + $host.SetShouldExit($res) + ~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : NotSpecified: (:) [], MethodException     + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument  ERRORLEVEL=0<\/code><\/pre>\n<p>  \u041f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442! \u041e\u043d \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 Invoke-Command \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u043e\u0432 \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 (Host \u0438 Output), \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u043d\u0430\u0434\u0435\u0436\u0434\u0443 \u043d\u0430 \u0443\u0441\u043f\u0435\u0445. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 Output \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e:<\/p>\n<pre><code class=\"powershell\">$exitcode = $args[0] Write-Host 'Out to host.' #Write-Output 'Out to output.' Write-Host ('ExitCode: ' + $exitcode) Write-Output $exitcode $host.SetShouldExit($exitcode) <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0433\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"powershell\">$res=Invoke-Command -ComputerName . -ScriptBlock { &'D:\\sqlagent\\TestOutput1.ps1' $args[0] } -ArgumentList $args[0] $host.SetShouldExit($res) <\/code><\/pre>\n<p>  \u0438\u2026 \u0414\u0410, \u043f\u043e\u0445\u043e\u0436\u0435, \u044d\u0442\u043e \u043f\u043e\u0431\u0435\u0434\u0430!<\/p>\n<pre><code class=\"plaintext\">Out to host. ExitCode: 4  IsPublic IsSerial Name                                     BaseType -------- -------- ----                                     -------- True     True     Int32                                    System.ValueType   ERRORLEVEL=4<\/code><\/pre>\n<p>  \u041f\u043e\u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e. \u041c\u044b \u0432\u044b\u0437\u0432\u0430\u043b\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e powershell, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u0432\u0430\u043b powershell \u043d\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b \u0442\u0430\u043c \u043d\u0430\u0448 \u0441\u043a\u0440\u0438\u043f\u0442. \u0414\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 (Host \u0438 Output) \u0441 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0439 \u043c\u0430\u0448\u0438\u043d\u044b \u0431\u044b\u043b\u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0442\u043e\u043a Output \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0432 \u043d\u0435\u043c \u043e\u0434\u043d\u043e\u0433\u043e \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0431\u044b\u043b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d \u043a \u0442\u0438\u043f\u0443 Int32 \u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435, \u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0430\u044f \u0441\u0442\u043e\u0440\u043e\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430 \u0435\u0433\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u0434\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e powershell-\u0430.<\/p>\n<p>  \u0418 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 SQL \u0437\u0430\u0434\u0430\u043d\u0438\u0435 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0441 \u0442\u0438\u043f\u043e\u043c \u00ab\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 (cmdexec)\u00bb \u0441 \u0442\u0430\u043a\u0438\u043c \u0442\u0435\u043a\u0441\u0442\u043e\u043c:<\/p>\n<pre><code class=\"dos\">PowerShell -NonInteractive -NoProfile &quot;$res=Invoke-Command -ComputerName BACKUPSERVER -ConfigurationName SQLAgent -ScriptBlock {&'D:\\sqlagent\\TestOutput1.ps1' 6}; $host.SetShouldExit($res)&quot;<\/code><\/pre>\n<p>  \u0423\u0420\u0410! \u0417\u0430\u0434\u0430\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u043e\u0441\u044c \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439, \u0442\u0435\u043a\u0441\u0442 \u0432 \u0436\u0443\u0440\u043d\u0430\u043b\u0435:<\/p>\n<pre><code class=\"plaintext\">\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 \u0438\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f: DOMAIN\\agentuser. Out to host. ExitCode: 6.  \u041a\u043e\u0434 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 6.  \u0428\u0430\u0433 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439. <\/code><\/pre>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434\u044b:<\/b><\/p>\n<ul>\n<li>\u0418\u0437\u0431\u0435\u0433\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Write-Output \u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0431\u0435\u0437 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c.<\/li>\n<li>\u0412 \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u0445, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0445 \u043d\u0435 \u0434\u043b\u044f \u0440\u0443\u0447\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430, \u0430 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0412\u0430\u0448\u0438\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430\u0445 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 WINRM, \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0440\u0443\u0447\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a \u0447\u0435\u0440\u0435\u0437 Try\/Catch, \u0438 \u0434\u043e\u0431\u0438\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u043c \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u044d\u0442\u043e\u0442 \u0441\u043a\u0440\u0438\u043f\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b \u0432 \u043f\u043e\u0442\u043e\u043a Output \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u0415\u0441\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 Errorlevel \u2014 \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c.<\/li>\n<\/ul>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/504890\/\"> https:\/\/habr.com\/ru\/post\/504890\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/504890\/\">\u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434\u0438\u043a\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0435\u0440\u0432\u043d\u044b\u043c\u0438 \u043a\u043e\u043f\u0438\u044f\u043c\u0438 \u043d\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 MS-SQL \u044f \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u043b \u043a\u0443\u0447\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 powershell \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0438\u0448\u0443 \u0441\u0430\u043c\u043e\u043c\u0443 \u0441\u0435\u0431\u0435 \u043f\u0430\u043c\u044f\u0442\u043a\u0443, \u0430 \u0432\u0434\u0440\u0443\u0433 \u043a\u043e\u043c\u0443-\u0442\u043e \u0435\u0449\u0435 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0435\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"powershell\">$exitcode = $args[0] Write-Host 'Out to host.' Write-Output 'Out to output.' Write-Host ('ExitCode: ' + $exitcode) Write-Output $exitcode $host.SetShouldExit($exitcode)<\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u044f \u0431\u0443\u0434\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c CMD-\u0444\u0430\u0439\u043b\u043e\u043c, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0435\u0433\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0441\u0442\u0430\u043d\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"dos\">@Echo OFF PowerShell .\\TestOutput1.ps1 1 ECHO ERRORLEVEL=%ERRORLEVEL%<\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u043a\u0440\u0430\u043d\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"plaintext\">Out to host. Out to output. ExitCode: 1 1 ERRORLEVEL=1<\/code><\/pre>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-304672","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304672"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304672\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}