English 中文(简体)
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 "\$MachineNamec$" -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.

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.

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!

最佳回答

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).

$? automatic variable contains the execution status of the last operation. 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).

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...

Concerning the second part of your question : $LASTEXITCODE contains the return code of your last executable.

Have you considered using the remoting capabilities of Powershell 2 for more control ?

Hope it helps

Cédric

问题回答

Use Try and catch as follow

$ErrorActionPreference= Stop 
Try{
Write-Host "Finished OK"
}
Catch [system.exception]
{
    Write-Host "Finished with Error"
    Write-Host $_.Exception.ToString()
    exit -1
}

There s a lot to your questions.

Here s something I ve done to read results from console apps like 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.

I use a powershell script to delete old files. 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. Good details about that on MSDN and TechNet. Loads of information comes up in the search engines for this too.





相关问题
Correct place to install demostration projects?

With the new Windows 7 restrictions (well, new to Windows Vista anyways), we can no longer install demo projects to %ProgramFilesFolder%OurApplicationdemo since restricted users will not be able to ...

.deb package conffiles problem

I am distributing one of my applications using a .deb package, but have a problem relating to one of the files. The distribution includes a database file which is constantly updated by the app, on a ...

Closing an application using WiX

In creating my WiX installer I have run into an issue when trying to close an application before installing the upgrade. Below is an example of how I am attempting to do this. <util:...

VS 2005 Setup - HKCU

I am trying to fix an existing application that uses a Visual Studio 2005 setup project. We are requiring it to work on limited user accounts for XP, our app is written in C# for .Net 2.0. It writes ...

Do I want Publish or Release Build in VB.net?

I wrote a little (like 40 lines little) Winforms application with VB.net in Visual Studio 2010. Now I ve released the code as a Google Code project. It s easy for a developer to get the source but I d ...

configsource and installer

I have an project csproj, with app.config file, and logging Ent.Library section using configsource attribute. The logging section is in ahother file Configloggingconfiguration.config. I have a ...

热门标签