Skip to content

Commit 8fc1439

Browse files
author
Mike Fal
committed
Merge branch 'master' of github.com:MikeFal/PowerShell
2 parents b3feb5f + 8861de6 commit 8fc1439

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Set-SqlServiceAccount.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#HT http://blogs.technet.com/b/heyscriptingguy/archive/2010/11/03/use-powershell-to-change-sql-server-s-service-accounts.aspx
2+
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SqlWmiManagement')| Out-Null
3+
function Set-SqlServiceAccount{
4+
param([string[]] $Instance
5+
,[System.Management.Automation.PSCredential]$ServiceAccount
6+
,[ValidateSet('SqlServer','SqlAgent')] $service = 'SqlServer'
7+
)
8+
9+
#Get Service Account name from credential object
10+
$account =$ServiceAccount.GetNetworkCredential().Domain +'\'+ $ServiceAccount.GetNetworkCredential().UserName
11+
12+
#Loop through and change instances
13+
foreach($i in $Instance){
14+
#Parse host and instance names
15+
$HostName = ($i.Split('\'))[0]
16+
$InstanceName = ($i.Split('\'))[1]
17+
18+
#Get service account names, set service account for change
19+
$sqlsvc = if($InstanceName){"MSSQL`$$InstanceName"}else{'MSSQLSERVER'}
20+
$agtsvc = if($InstanceName){"SQLAGENT`$$InstanceName"}else{'SQLSERVERAGENT'}
21+
22+
$ServiceName = switch($service){
23+
'SqlServer'{$sqlsvc}
24+
'SqlAgent'{$agtsvc}
25+
}
26+
27+
#Use wmi to change account
28+
$smowmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $HostName
29+
$wmisvc = $smowmi.Services | Where-Object {$_.Name -eq $ServiceName}
30+
$wmisvc.SetServiceAccount($account,$ServiceAccount.GetNetworkCredential().Password)
31+
32+
#If Agent service isn't running (happens if you reset SQL Service), start it
33+
$wmiagt = $smowmi.Services | Where-Object {$_.Name -eq $agtsvc}
34+
if($wmiagt.ServiceSatus -ne 'Running'){$wmiagt.Start()}
35+
}
36+
}
37+
38+
39+
<#
40+
$cred = Get-Credential 'Enter Service Account'
41+
Set-SqlServiceAccount -Instance PICARD -ServiceAccount $cred -Service SqlServer
42+
#>

Set-SqlStartupParameters.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SqlWmiManagement')| Out-Null
2+
3+
function Set-SQLStartupParameters{
4+
param([string[]] $Instance
5+
,[string[]] $StartupParameters
6+
)
7+
[bool]$SystemPaths = $false
8+
9+
#Loop through and change instances
10+
foreach($i in $Instance){
11+
#Parse host and instance names
12+
$HostName = ($i.Split('\'))[0]
13+
$InstanceName = ($i.Split('\'))[1]
14+
15+
#Get service account names, set service account for change
16+
$ServiceName = if($InstanceName){"MSSQL`$$InstanceName"}else{'MSSQLSERVER'}
17+
18+
#Use wmi to change account
19+
$smowmi = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $HostName
20+
$wmisvc = $smowmi.Services | Where-Object {$_.Name -eq $ServiceName}
21+
22+
$oldparams = $wmisvc.StartupParameters -split ';'
23+
$newparams = @()
24+
foreach($param in $StartupParameters){
25+
if($param.Substring(0,2) -match '-d|-e|-l'){
26+
$SystemPaths = $true
27+
$newparams += $param
28+
$oldparams = $oldparams | Where-Object {$_.Substring(0,2) -ne $param.Substring(0,2)}
29+
}
30+
else{
31+
$newparams += $param
32+
}
33+
}
34+
35+
$newparams += $oldparams | Where-Object {$_.Substring(0,2) -match '-d|-e|-l'}
36+
$wmisvc.StartupParameters = ($newparams | Sort-Object) -join ';'
37+
$wmisvc.Alter()
38+
39+
Write-Warning "Startup Parameters for $i updated. You will need to restart the service for these changes to take effect."
40+
If($SystemPaths){Write-Warning "You have changed the system paths for $i. Please make sure the paths are valid before restarting the service"}
41+
42+
}
43+
}

0 commit comments

Comments
 (0)