Welcome › Forums › General PowerShell Q&A › NewBee with powershell - need help !!!
-
AuthorPosts
-
February 8, 2017 at 11:48 am #63736
here is a function I have created to check the backup state
**********************************************************************************function check-backup
{
[CmdletBinding()]Param
([Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[String[]]$computername
)Begin
{
}
Process
{foreach ($server in $computername){
$reply = get-wmiobject -class win32_PingStatus -Filter "Address = '$server'"
if (($reply.statusCode -eq "0") -and (Test-Path -path "\\$server\c$\program files\tivoli\tsm\logs") -eq $true)
{
$folder = Get-ChildItem -Path "\\$server\c`$\program files\Tivoli\tsm\logs" -Filter "*D_SOL.inc"
$content = $folder | Get-Content
[String[]]$Match = $content | select-string -Pattern "Successful incremental backup"
$final = $match.Contains("Successful incremental backup of '\\$server\c$'")
}
if($final){Write-Output "Backup is successful for server:$server"
}
else{
Write-Output "backup was not successful for server: $server"
}
}
}
End
{
}
}
******************************************************************************************************
I am able to get the desired result if I run this function in this way ::check-backup -computername WDCSLVBP01,WDCSLVBP02,WDCBIAP01,WDCBIAP02,WDCBIFP02,WDCBIFP01,WDCDYAP22,WDCIRAP21,WDCIRAP22,WDCTDAP21,WDCTDAP22,WDCTDSP21,WDCDYAP23
***************************************************************
Problem is that I am not able to get the desired result if I run the same function this way ::
check-backup -computername (get-content "c:\temp\server.txt")
Result :
Bbackup was not successful for server: $server (All the servers get listed with failed backup result) -
February 8, 2017 at 1:49 pm #63742
Can you show the content of the file?
-
February 8, 2017 at 3:04 pm #63752
WDCRHBD01
WDCIST06
WDCWUP01
WDCSLVBV01
WDCACAS01
WDCACAS02
WDCACAS03***********************************
These are the list of servers that I am feeding to server.txt file.
-
-
February 8, 2017 at 5:36 pm #63758
Try this:
(get-content "c:\temp\server.txt").Count
Make sure its being read as an array. Count should be the number of lines.
If it is an array, perhaps the way you are calling it is forcing it to convert to a single string. If that's the case, maybe make it 2 lines.
$ary = get-content "c:\temp\server.txt"
Confirm that it is an array.
$ary.Count
check-backup -computername $ary
-
February 8, 2017 at 5:37 pm #63761
This should work for you
get-content "c:\temp\server.txt"|check-backup
-
February 9, 2017 at 5:06 pm #63866
Thanks Ron for your response !!
But the problem remains the same even after storing the get-content result in a variable. I did confirm that it is being treated as an array using the count method.
***************************************
Still giving me wrong results as seen below :: Piping $ary to the check-backup function is also giving me same outputPS C:\Windows\system32> check-backup -computername $ary
backup was not successful for server: WDCSLVBP01
backup was not successful for server: WDCSLVBP02
backup was not successful for server: WDCBIAP01
backup was not successful for server: WDCBIAP02
backup was not successful for server: WDCBIFP02
backup was not successful for server: WDCBIFP01
backup was not successful for server: WDCDYAP22
backup was not successful for server: WDCIRAP21*****************************************************
Correct result using a the computer name with the parameter :PS C:\Windows\system32> check-backup -computername wdcirap21
Backup is successful for server:wdcirap21 -
February 9, 2017 at 5:56 pm #63869
Is there white space after your server names? Grasping at straws now.
-
February 10, 2017 at 10:15 am #63904
No spaces as verified.
-
-
February 10, 2017 at 11:37 am #63908
check your line break characters in file.
may be you can try to save other file and test with it
like'server1','server2' | set-content servers.txt
and another test suggestion check if output is valid:
foreach ($server in $computername){ Write-Host ("-{0}- ({1})" -f $server, $server.length) #[...]
btw, I see that you set $final variable only
if $reply.statusCode -eq "0" ...
somewhere around can be logic error
btw, why you do not use Test-Connection ?-
February 10, 2017 at 12:49 pm #63919
Thanks Max for your response !!
I have managed to fix the problem by making a small change to my script :
***************************************************
function check-dailybackup
{
[CmdletBinding()]Param
([Parameter(Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[String[]]$computername
)Begin
{
}
Process
{foreach ($server in $computername){
$reply = get-wmiobject -class win32_PingStatus -Filter "Address = '$server'"
if (($reply.statusCode -eq "0") -and (Test-Path -path "\\$server\c$\program files\tivoli\tsm\logs") -eq $true)
{
$folder = Get-ChildItem -Path "\\$server\c`$\program files\Tivoli\tsm\logs" -Filter "*D_SOL.inc"
$lstwritetime = $folder.LastWriteTime
if($lstwritetime -gt (Get-Date).AddDays(-2))
{
$content = $folder | Get-Content
[String[]]$Match = $content | select-string -Pattern "Successful incremental backup"
[boolean]$final = ($match -eq "Successful incremental backup of '\\$server\c$'")
}
}
if($final){Write-Output "Daily Backup is successful for server: $server"
}
else{
Write-Output "Daily Backup was not successful for server: $server"
}
}
}
End
{
}
}
-
-
February 10, 2017 at 12:50 pm #63920
I only made the change in the $final variable making it a boolean expression:
[boolean]$final = ($match -eq "Successful incremental backup of '\\$server\c$'")
*******************************************
So the the content of the server.txt file was not in question – Problem was something else .Still, not able to able to make out why my previous script didnt give the correct results.
-
February 10, 2017 at 1:30 pm #63925
I'm insist that you have error with $final 🙂
let see example:
you have server1 and server2
server1 is ok and backup succeeded and server2 is offline
what your script do:check server1 - OK check backup - OK, set $final=$true if ($final) write OK check server2 - ERROR if ($final) write OK
you see : the final stay $true because you doesnt set it to $false right after foreach !
-
AuthorPosts
The topic ‘NewBee with powershell - need help !!!’ is closed to new replies.