Powershell to query current hibernate status of all Windows Server in the domain ?

Hello All,

I'd like to know if anyone can modify this script so that it can query multiple servers for hibernate files ?

I've come accros into this URL (http://powershell.com/cs/media/p/3849.aspx) but somehow it always failed when I supplied the array from
Get-QADComputer -OSName "Windows Server*" | Select-Object -expand name

Open in new window


somehow it can only display "does not exist" eventhough the file DOES exist in the C Drive

Checking  for: C:\hiberfil.sys Server01-vm
WARNING: File/folder does not exist on !!

Open in new window


I tried to change the hidden file settings but still the same result ?

Any kind of help would be greatly appreciated.

Thanks.
$OnlineServers = @()
Get-QADComputer -SearchRoot 'domain.com/domain servers/Windows 2008 Servers/DMZ Prod' -OSName "Windows*Server*" | %{$PingResult = Get-WmiObject -Query "SELECT * FROM win32_PingStatus WHERE address='$($_.Name)'"
  If ($PingResult.StatusCode -eq 0) {
    $OnlineServers += "$($_.Name)"
  }
}


function Check-FileExists ([string]$Path, [switch]$Output) {
	BEGIN { 
		# Display help content, if -? is specified at command line
		if ($args -contains '-?') {
			Write-Host -ForegroundColor Yellow "`nPURPOSE:`nPipeline function!  Accepts computer names from pipeline & checks to see whether the provided path exists on the machine. If the Output switch is provided then it outputs the computers that the file exists on to the pipeline."
			Write-Host -ForegroundColor Green "`nSYNTAX:`n`$computername_array | Check-FileExists [[-Path] <String>] [-Output]`n"
			break
		}
		
		$Computers_FileExists = @()
	}
	PROCESS {
		Test-Pipeline
		$Computer = $_
		
		Write-Host "Checking $Computer for: $Path"
		if (!($Path -like "HK*")) {
			# Test that the file/folder/registry key exists at the specified path/location
			if (Test-Path -Path $Path) {
				Write-Host -ForegroundColor Green "File/folder exists on $Computer!`n"
				$Computers_FileExists += $Computer
			} else { Write-Warning "File/folder does not exist on $Computer!!`n" }
		} else {
			[string]$RegistryBaseKey = $Path.Split(':')[0]
			[string]$SubKey = $Path.Split(':')[1].SubString(1)
			
			switch ($RegistryBaseKey) {
				"HKLM" { $HKey = 'LocalMachine' }
				"HKCU" { $HKey = 'CurrentUser' }
				Default { Write-Warning "Registry path must start with 'HKLM:\' or 'HKCU:\'" }
			}
			
			# Test to see if the supplied registry key exist on the current machine
			$RegistryPath = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($HKey,$Computer).OpenSubKey($SubKey)
			if ($RegistryPath -ne $null) {
				Write-Host -ForegroundColor Green "Registry key exists on $Computer!`n"
				$Computers_FileExists += $Computer
			} else { Write-Warning "Registry key does not exist on $Computer!!`n" }
		}
	}
	END { if ($Output.IsPresent) { Write-Output $Computers_FileExists } }
}


foreach($Server in $OnlineServers) { 
	Check-FileExists ("C:\hiberfil.sys", "$Server")
}

Open in new window

LVL 1
jjozAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jjozAuthor Commented:
the problem with the above scipt is that:

1. It cannot convert the System.object array into the Function (because the function can only accept one string object)
2. eventhough the file C:\hiberfil.sys exist on the server it always says it doesn't exist. but when I remove the server / computername parameter it works as my lcoal laptop got that file?
0
Chris DentPowerShell DeveloperCommented:
How odd, despite its claim it has no means of checking whether or not a file is present on a remote system.

You might find you're better off with this. We check for the file via the administrative share.
Get-QADComputer -SearchRoot 'domain.com/domain servers/Windows 2008 Servers/DMZ Prod' -OSName "Windows*Server*" |
  Where-Object { Test-Connection $_.Name -Count 1 -Quiet } | ForEach-Object {
    If (Test-Path "\\$_.Name\c$\hiberfil.sys") {
      # Do stuff
    }
  }

Open in new window

Depending on what you want to do in either case you may not need the If statement, another pass through Where-Object may suffice.

HTH

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjozAuthor Commented:
Thanks Chris, here's mine that worked but quite slow as my servers in OU is around 500+

somehow the \\ UNC doesn't work Chris in some of the servers ?
$OnlineServers = @()
$OnlineServerCount = 0
Get-QADComputer -SearchRoot 'domain.com/domain servers/' -OSName "Windows*Server*" | %{$PingResult = Get-WmiObject -Query "SELECT * FROM win32_PingStatus WHERE address='$($_.Name)'"
  If ($PingResult.StatusCode -eq 0) {
    $OnlineServers += "$($_.Name)"
	$OnlineServerCount ++
  }
}

$ServerCount = 0
foreach($Server in $OnlineServers) { 
	$FileResult = gwmi cim_datafile -ComputerName $Server -filter "path='\\'" | ? {$_.name -match 'hiberfil.sys'}
	if ( $FileResult -ne $null ) {
		Write-Output "$Server"
		$ServerCount ++
	}
}

Write-Output "Total server with hibernate feature enable is $ServerCount out of $OnlineServerCount total online servers."

Open in new window

0
Chris DentPowerShell DeveloperCommented:
If you end up using CIM_DataFile you want the local path:

C:\\hiberfil.sys

The double \ there is important in the Filter.

Chris
0
jjozAuthor Commented:
Thanks Chris !
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.