[英]PowerShell script - How to process result of executed command
I have a small script that copies files to a list of remote machines. 我有一个小文件,可以将文件复制到远程计算机列表中。 In this script I use:
在这个脚本中我使用:
Copy-Item "$AppLocation\\$AppName" -destination "\\\\$MachineName\\c$\\" -force
This can throw various types of errors. 这可能会引发各种类型的错误。 If this throws an error, I want to log the error to a file and then continue.
如果这会引发错误,我想将错误记录到文件然后继续。 My question is that I'd like to know what would be the right way to find out if the Copy-Item command was successful.
我的问题是,我想知道什么是找出Copy-Item命令是否成功的正确方法。
Next question is related: 下一个问题是相关的:
psexec \\\\$MachineName -u $RemoteLogin -p $Remotepassword -s -i -d C:\\$AppName
What would be a good way to find out how this command executed? 找出这个命令是如何执行的好方法是什么? I get a message in the console that it exited with 0 but I have no idea how I could get the return code into a local variable.
我在控制台中收到一条消息,它以0退出,但我不知道如何将返回码转换为局部变量。
I can also use this: 我也可以用这个:
(Get-WMIObject -ComputerName $MachineName -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install("C:\\$AppName","","false")
which works fine, but still, no idea how to find out if it succeeded unless I read the output. 哪个工作正常,但仍然不知道如何成功,除非我读取输出。
Thanks! 谢谢!
Use Try and catch as follow 使用Try和catch如下
$ErrorActionPreference='Stop'
Try{
Write-Host "Finished OK"
}
Catch [system.exception]
{
Write-Host "Finished with Error"
Write-Host $_.Exception.ToString()
exit -1
}
Concerning the first part of your question, you can use 关于问题的第一部分,您可以使用
Copy-Item ... -ErrorAction Continue -ErrorVariable yourVariable
Error action tells the cmdlet what to do if case of error , ErrorVariable will put any error in the variable of your choice ($yourVariable in the exemple). 错误操作告诉cmdlet如果出现错误,该怎么办,ErrorVariable会在您选择的变量中放入任何错误(例如$ yourVariable)。
$? $? automatic variable contains the execution status of the last operation.
automatic变量包含上一次操作的执行状态。 If it's false, just check the content of $yourVariable to know what went wrong and do whatever you want with it (like write it in a file in your case).
如果它是假的,只需检查$ yourVariable的内容,知道出了什么问题,并用它做任何你想做的事情(比如在你的情况下把它写在一个文件中)。
You can also use 你也可以使用
Copy-Item ... -ErrorAction Continue 2> [pathToLogFile]
This will redirect the error stream of the cmdlet to a file of your choice... 这会将cmdlet的错误流重定向到您选择的文件...
Concerning the second part of your question : $LASTEXITCODE contains the return code of your last executable. 关于问题的第二部分:$ LASTEXITCODE包含上一个可执行文件的返回码。
Have you considered using the remoting capabilities of Powershell 2 for more control ? 您是否考虑过使用Powershell 2的远程处理功能来进行更多控制?
Hope it helps 希望能帮助到你
Cédric 塞德里克
There's a lot to your questions. 你的问题有很多。
Here's something I've done to read results from console apps like psexec: 这是我从psexec等控制台应用程序中读取结果时所做的一切:
[System.Diagnostics.ProcessStartInfo]$info = New-Object System.Diagnostics.ProcessStartInfo $info.WorkingDirectory = "C:\" $info.UseShellExecute = $false $info.RedirectStandardOutput = $true $info.CreateNoWindow = $true [System.Diagnostics.Process]$proc = [System.Diagnostics.Process]::Start($info) if (($proc -ne $null) -and ($proc.id -ne 0)) { [void]$proc.WaitForExit($timeOutMs); } $proc.StandardOutput.ReadToEnd();
Note, that that's untested code, but it came from something that works and I just did a little editing for simplicity. 请注意,这是未经测试的代码,但它来自可行的东西,我只是为了简单而做了一些编辑。
Now, you can process the text output of psexec and handle accordingly. 现在,您可以处理psexec的文本输出并相应地处理。
I use a powershell script to delete old files. 我使用powershell脚本删除旧文件。 This outputs a message to the screen, which could just as easily write a message to the event log.
这会向屏幕输出一条消息,这可以很容易地将消息写入事件日志。 Details here .
细节在这里 。
Writing to the event log can be done with the Write-EventLog command. 可以使用Write-EventLog命令写入事件日志。 Good details about that on MSDN and TechNet .
关于MSDN和TechNet的详细信息。 Loads of information comes up in the search engines for this too.
搜索引擎中也出现了大量信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.