Hi ,
I am appending a sample script of what we are trying to do.
1.we are using powershell 2.0 new remoting feature with fan-out model.
2.The issue we are facing is how to check if the remote command (invoke-command) has successfully executed the function on the remote computers. we want to check each of the computer independently and then log the error(including the machine name it failed on and error message) ,stop the script etc....
3.The function we have written for this purpose(checkforerrors) in the apended file is randomly working sometimes and not sometimes.
I would like to know if their is anyway we can achieve this.(it can be even without using that function). If you can get me a suggestion on this, that would be great.
Thanks in advance.
function CreateAppPool {
param (
[string]$domain,
[string]$poolName
)
Add-PSSnapIn webAdministration;
if (-not (test-path IIS:\AppPools\$poolName)) {
New-WebAppPool -name $poolName
Set-ItemProperty IIS:\AppPools\$poolName -name managedPipelineMode -value 0 ### 0=Integrated, 1=Classic
Set-ItemProperty IIS:\AppPools\$poolName -name processModel -value @{userName="$domain\xxxxx";password="yyyyyy";identitytype=3}
}
}
function checkforErrors {
param (
[string]$allsession,
[string]$errorMsg
)
foreach ($session in $allsession)
{
if($errorMsg -ne $null)
{
$Machineinfo = Get-PSSession ($machine)
#$errorlist = $Machineinfo += $errorMsg
tee-object -file "e:\temp\debug.ps1" -InputObject $errorlist
Remove-PSSession *
write-host("exited out because of error")
exit 1
}
else{
Write-Host("Success")
}
}
}
################Start place for execution of script##################
##############
### Main#####
##############
#These will be the machines on which "CreateAppPool" function will be executed remotely
$allmachines = "computer1,computer2"
#----Here we are adding the sessions created for each machine in allsession variable
foreach ($machine in $allmachines) {
# creating the persistent sessions for all the machines
#$allsession = New-PSSession -ComputerName $target -Name $target
$allsession = New-PSSession -Name $target -ComputerName $target
}
#####IMPORTANT#######
#Powershell remoting using fan-out model
Invoke-Command -Session $allsession -ScriptBlock $function:CreateAppPool -ArgumentList $domain,$poolName -ErrorVariable errorMsg
##This is the function we are plannig to use if the execution of the above function errored out while execution on remote machines
checkforErrors $allsession $errorMsg