I am having problem listing server name after using the invoke command and going to ‘else’. I am trying to get a list of servers tyhat do not have do not have the invoke-command applied.
#This is a list of computers that need appending of the nsclient.ini file
$computers = gc ‘C:\powershell\List.txt’ #This is the append file that will be added at the end of nsclient.ini
$source = ‘C:\powershell\content.txt’ #This is the destination where the content.txt will be copied
$destination = 'C$\Program Files\NSClient++'
foreach ($computer in $computers) {
if ((Test-Path -Path \$computer$destination)) { #If the destination path is present, it will copy the content.txt file
Copy-Item $source -Destination \$computer$destination -Recurse #This invokes the actual append of the file using anothe script
Invoke-Command -ComputerName $computers -FilePath ./append_ini.ps1
} else {
‘-computername $computer.columnname is not reachable or does not exist’ | out-file -literalpath ‘c:\powershell$server-results.txt’
}
Restart-Service -InputObject $(Get-Service -Computer $computer -Name nscp) out-file -literalpath ‘c:\powershell$server-results.txt’
}
Please be sure to format your code using the provided tools. This encourages folks to want to help you more and understand what you are saying / after.
You also have some things in here that are really kind of odd, syntax-wise not correct / valid, operationally questionable.
What’s in that source file?
How is it formatted?
Is it a real CSV file or just string text?
Put a sanitized version on the post. You cannot use what you are not passing.
Based on what you posted, maybe this is what you are after.
#This is the append file that will be added at the end of nsclient.ini
$source = 'C:\powershell\content.txt'
#This is the destination where the content.txt will be copied
$destination = 'C$\Program Files\NSClient++\'
# This is a list of computers that need appending of the nsclient.ini file
# gc 'C:\powershell\List.txt'
(Get-ADComputer -Filter '*').Name|
foreach {
if ((Test-Path -Path "\\$PSItem\$destination"))
{
#If the destination path is present, it will copy the content.txt file
Copy-Item $source -Destination "\\$PSItem\$destination" -Recurse
#This invokes the actual append of the file using anothe script
Invoke-Command -PSItemName $PSItems -FilePath './append_ini.ps1'
}
else
{
"-computername $PSItem is not reachable or does not exist" |
out-file -literalpath "c:\powershell\$PSItem -results.txt"
}
Restart-Service -InputObject $(Get-Service -Computer $PSItem -Name nscp) |
out-file -literalpath "c:\powershell\$PSItem-results.txt"
}