Skip to content

Instantly share code, notes, and snippets.

@dotps1
Last active January 5, 2024 08:22
Show Gist options
  • Select an option

  • Save dotps1/10469594 to your computer and use it in GitHub Desktop.

Select an option

Save dotps1/10469594 to your computer and use it in GitHub Desktop.
GetSet-DiskTimeoutValue
<#
.SYNOPSIS
Gets the current value for the Disk TimeoutValue from the registry of a machine.
.DESCRIPTION
Gets the value that is currently set in the registry for the time out a disk waits before timing out and shuting down a system.
.EXAMPLE
Get-DiskTimeoutValue
.EXAMPLE
Get-DiskTimeoutValue -ComputerName MyServer.mydomain.org
.EXAMPLE
@("MyComputer","MyServer","MyLaptop") | %{ Get-DiskTimeoutValue -ComputerName $_ }
.NOTES
Remote access to the registry is need if using on remote machine.
Elevated rights maybe needed to read this registry key value.
.LINK
http://dotps1.github.io
#>
function Get-DiskTimeoutValue
{
[CmdletBinding()]
[OutputType([PSObject])]
param
(
# ComputerName, Type String, System to enumerate SQL Edition aginst.
[Alias("Server")]
[ValidateScript({ if (Test-Connection $_ -Count 2){ return $true } else { throw "Failed to ping $_. Please ensure the system is available." } })]
[Parameter(ValueFromPipeline = $true,
Position = 0)]
[String[]]
$ComputerName = $env:COMPUTERNAME
)
try
{
$hive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',"$ComputerName")
$value = ($hive.OpenSubKey("System\CurrentControlSet\Services\Disk")).GetValue("TimeoutValue")
$hive.Close()
}
catch
{
$value = $Error[0].Exception.Message
}
return $value | Select-Object @{ Name = "ComputerName"; Expression = { $ComputerName } },
@{ Name = "TimeoutValue"; Expression = { $_ } }
}
<#
.SYNOPSIS
Sets the current value for the Disk TimeoutValue in the registry of a machine.
.DESCRIPTION
Sets the value for the disk timeout in the registry on a machine. This value is used to dertimine the amount of time in seconds to wait before the system hard shutsdown.
.EXAMPLE
Set-DiskTimeoutValue -ComputerName MyServer.mydomain.org -Seconds 60
.EXAMPLE
@("MyComputer","MyServer","MyLaptop") | %{ Set-DiskTimeoutValue -ComputerName $_ -Seconds 90 }
.NOTES
Remote access to the registry is need if using on remote machine.
Elevated rights maybe needed to set this registry key value.
.LINK
http://dotps1.github.io
#>
function Set-DiskTimeoutValue
{
[CmdletBinding()]
[OutputType([Void])]
param
(
# ComputerName, Type String, System to enumerate SQL Edition aginst.
[Alias("Server")]
[ValidateScript({ if (Test-Connection $_ -Count 2){ return $true } else { throw "Failed to ping $_. Please ensure the system is available." } })]
[Parameter(ValueFromPipeline = $true,
Position = 0)]
[String[]]
$ComputerName = $env:COMPUTERNAME,
# Seconds, Type Int, The ammount of time in seconds to set the value to.
[Parameter(Position = 1)]
[Int]
$Seconds = 60
)
try
{
$hive = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',"$ComputerName")
$key = $hive.OpenSubKey("System\CurrentControlSet\Services\Disk",$true)
if ($key.GetValue("TimeoutValue") -ne $Seconds)
{
$key.SetValue("TimeoutValue", $Seconds)
}
Get-DiskTimeoutValue -ComputerName $ComputerName
$key.Close()
}
catch
{
$Error[0].Exception.Message
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment