NewBee with powershell - need help !!!

This topic contains 11 replies, has 5 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 1 month, 2 weeks ago.

  • Author
    Posts
  • #63736
    Profile photo of Karan
    Karan
    Participant

    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)

  • #63742
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    Can you show the content of the file?

    • #63752
      Profile photo of Karan
      Karan
      Participant

      WDCRHBD01
      WDCIST06
      WDCWUP01
      WDCSLVBV01
      WDCACAS01
      WDCACAS02
      WDCACAS03

      ***********************************

      These are the list of servers that I am feeding to server.txt file.

  • #63758
    Profile photo of Ron
    Ron
    Participant

    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

  • #63761
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    This should work for you

    get-content "c:\temp\server.txt"|check-backup
  • #63866
    Profile photo of Karan
    Karan
    Participant

    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 output

    PS 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

  • #63869
    Profile photo of Ron
    Ron
    Participant

    Is there white space after your server names? Grasping at straws now.

    • #63904
      Profile photo of Karan
      Karan
      Participant

      No spaces as verified.

  • #63908
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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 ?

    • #63919
      Profile photo of Karan
      Karan
      Participant

      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
      {
      }
      }

  • #63920
    Profile photo of Karan
    Karan
    Participant

    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.

  • #63925
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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 !

You must be logged in to reply to this topic.