< Back

Set-DbaDbState

Mon Jan 13, 2020 5:37 pm

NAME Set-DbaDbState



SYNOPSIS

Sets various options for databases, hereby called "states"





SYNTAX

Set-DbaDbState [-SqlCredential <Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>]

[-AllDatabases <Switch>] [-ReadOnly <Switch>] [-ReadWrite <Switch>] [-Online <Switch>] [-Offline <Switch>]

[-Emergency <Switch>] [-Detached <Switch>] [-SingleUser <Switch>] [-RestrictedUser <Switch>] [-MultiUser <Switch>]

[-Force <Switch>] [-EnableException <Switch>] [<CommonParameters>]



Set-DbaDbState -SqlInstance <Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]> [-SqlCredential

<Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>] [-AllDatabases <Switch>]

[-ReadOnly <Switch>] [-ReadWrite <Switch>] [-Online <Switch>] [-Offline <Switch>] [-Emergency <Switch>] [-Detached

<Switch>] [-SingleUser <Switch>] [-RestrictedUser <Switch>] [-MultiUser <Switch>] [-Force <Switch>]

[-EnableException <Switch>] [<CommonParameters>]



Set-DbaDbState [-SqlCredential <Pscredential>] [-Database <System.Object[]>] [-ExcludeDatabase <System.Object[]>]

[-AllDatabases <Switch>] [-ReadOnly <Switch>] [-ReadWrite <Switch>] [-Online <Switch>] [-Offline <Switch>]

[-Emergency <Switch>] [-Detached <Switch>] [-SingleUser <Switch>] [-RestrictedUser <Switch>] [-MultiUser <Switch>]

[-Force <Switch>] [-EnableException <Switch>] -InputObject <System.Management.Automation.PSObject[]>

[<CommonParameters>]





DESCRIPTION

Sets some common "states" on databases:



- "RW" options (ReadOnly, ReadWrite)



- "Status" options (Online, Offline, Emergency, plus a special "Detached")



- "Access" options (SingleUser, RestrictedUser, MultiUser)



Returns an object with SqlInstance, Database, RW, Status, Access, Notes



Notes gets filled when something went wrong setting the state





PARAMETERS

-AllDatabases [<Switch>]

This is a parameter that was included for safety, so you don't accidentally set options on all databases

without specifying



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Database [<System.Object[]>]

The database(s) to process - this list is auto-populated from the server. if unspecified, all databases will

be processed.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Detached [<Switch>]

Status Option : Detaches the database



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Emergency [<Switch>]

Status Option : Sets the database as EMERGENCY



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-EnableException [<Switch>]

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.

This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables

advanced scripting.

Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own

try/catch.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ExcludeDatabase [<System.Object[]>]

The database(s) to exclude - this list is auto-populated from the server



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Force [<Switch>]

For most options, this translates to instantly rolling back any open transactions

that may be stopping the process.

For -Detached it is required to break mirroring and Availability Groups



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-InputObject [<System.Management.Automation.PSObject[]>]

Accepts piped database objects



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-MultiUser [<Switch>]

Access Option : Sets the database as MULTI_USER



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Offline [<Switch>]

Status Option : Sets the database as OFFLINE



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-Online [<Switch>]

Status Option : Sets the database as ONLINE



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ReadOnly [<Switch>]

RW Option : Sets the database as READ_ONLY



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-ReadWrite [<Switch>]

RW Option : Sets the database as READ_WRITE



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-RestrictedUser [<Switch>]

Access Option : Sets the database as RESTRICTED_USER



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SingleUser [<Switch>]

Access Option : Sets the database as SINGLE_USER



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlCredential [<Pscredential>]

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).



Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory -

Integrated are all supported.



For MFA support, please use Connect-DbaInstance.



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



-SqlInstance [<Sqlcollaborative.Dbatools.Parameter.DbaInstanceParameter[]>]

The target SQL Server instance or instances



Required? false

Position? named

Default value

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



NOTES





Tags: Database, State

Author: Simone Bizzotto (@niphold)



Website: https://dbatools.io

Copyright: (c) 2018 by dbatools, licensed under MIT

License: MIT https://opensource.org/licenses/MIT



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -Offline



Sets the HR database as OFFLINE

-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Set-DbaDbState -SqlInstance sqlserver2014a -AllDatabases -Exclude HR -ReadOnly -Force



Sets all databases of the sqlserver2014a instance, except for HR, as READ_ONLY

-------------------------- EXAMPLE 3 --------------------------



PS C:\\>Get-DbaDbState -SqlInstance sql2016 | Where-Object Status -eq 'Offline' | Set-DbaDbState -Online



Finds all offline databases and sets them to online

-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -SingleUser



Sets the HR database as SINGLE_USER

-------------------------- EXAMPLE 5 --------------------------



PS C:\\>Set-DbaDbState -SqlInstance sqlserver2014a -Database HR -SingleUser -Force



Sets the HR database as SINGLE_USER, dropping all other connections (and rolling back open transactions)

-------------------------- EXAMPLE 6 --------------------------



PS C:\\>Get-DbaDatabase -SqlInstance sqlserver2014a -Database HR | Set-DbaDbState -SingleUser -Force



Gets the databases from Get-DbaDatabase, and sets them as SINGLE_USER, dropping all other connections (and rolling

back open transactions)



RELATED LINKS

https://dbatools.io/Set-DbaDbState